2012-01-27 13 views
0

私のアプリは16x16ボタンのグリッドを作成します。これらはすべて幅と高さが30dpに設定されています。それは480x480dpになります。 800x480の画面でテストすると、アプリは正常に表示されます。しかし、エミュレータを400x240に設定すると、アプリは実際には醜く、ボタンのグリッドは画面より広くなります。Android:ボタンのグリッドの幅画面が異なる画面解像度で変更されない

この問題を解決するために、ボタンの幅と高さを "knopAfmeting"という名前の変数に設定しました。 400x240px画面でテストするとき、アプリがまだのグリッドを表示

Button button = new Button (this); 
    button.setHeight(knopAfmeting); 
    button.setWidth(knopAfmeting); 

をしかし:

Display display = getWindowManager().getDefaultDisplay(); 
    int schermBreedte = display.getWidth(); 
    int knopAfmeting = schermBreedte/16; 

そしてボタンは次のように設定されています。その変数は、コードのこの部分で設定されているように見えますボタンが間違っている。私は間違って何をしていますか?

答えて

0

は== =========第二の提案

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 
    <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 

</LinearLayout> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 
    <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 

</LinearLayout> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 
    <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 

</LinearLayout> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal"> 
    <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 
     <Button 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" /> 

</LinearLayout> 

</LinearLayout> 

..あなたはボタンのheigh /幅を設定する必要はありません、彼らは比較的に設定されます ...これを試してみてください======

私はあなたの質問を間違った方法で理解していました。とにかく私はあなたのコードをチェックしました。あなたはいつもknopAfmetingを幅に基づいて計算しています。しかし、高さが低い場合、高さに基づいてknopAfmetingを計算する必要があります。言い換えれば、より小さい次元を取得します。

ので

Display display = getWindowManager().getDefaultDisplay(); 
int schermBreedte =0; 
if(display.getWidth()>display.getHeight()) 
    schermBreedte=display.getHeight() 
else 
    schermBreedte=display.getWidth(); 
int knopAfmeting = schermBreedte/16; 

ちょうどあなたには、いくつかのスペースを一の右側を取得します..あなたはそれがよりよい作るためにそれらのすべてをセンタリングすることができます。..

幸運...

+0

私は何もできませんフィールドがOnCreate()のコードループで生成されているため.xmlにあります。 – Mavix

+0

なぜピクセル単位で画面の寸法を取得せず、高さと幅を4で割ったのですか?正しい値を取得します。そして、onconfigurationchange()で方向を変更すると再計算されます。 –

+0

なぜ4でそれらを分けるべきですか? – Mavix

関連する問題