2

Lets say I wanted to make a relative view with two columns of buttons, left and right.

Where each button takes up exactly 20% of the screen, separated by 10%, with the remaining border on the sides. But to do this for any screen size. DP I think won't work because how do you know the DP pixel count on each different screen?

Basically how do you keep UI's relatively the same and fitting between all devices?

4

1 に答える 1

1

短い答え: LinearLayout.

長い答えはここにあります:

LinearLayout のパーセンテージ幅を定義していますか?

実行時に密度を取得する方法もありますが、長期的には上記の方が簡単です。

いくつかの疑似 XML の準備をしましょう!

<LinearLayout double fill parent, vertical>
    <RelativeLayout w = 0, h = 0, weight smallish>
         <Button A />
    </RelativeLayout>
    <LinearLayout w = 0, h = 0, weight largish. horizontal>
         <!-- maybe you want these to be RelativeLayout and set the buttons
         as centerHorizontalInParent="true" -->
         <LinearLayout w = 0, h = 0, weight whatever 20% is>
              <Buttons with some padding/margins>
         </LinearLayout>
         <Some kind of spacer, or just pad the LLs />
         <LinearLayout repeat above />
     </LL>
</LL>

編集:

動的なサイズ変更を頻繁に行う場合は、画面サイズを取得するための次のカスタム クラスがあります。

public class ScreenGetter {
    private float pixHeight;
    private float pixWidth;
    private float dpHeight;
    private float dpWidth;
    private float density;

public ScreenGetter (Context context){
    Display display = ((WindowManager)
                context.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics ();
    display.getMetrics(metrics);

    pixHeight = metrics.heightPixels;
    pixWidth = metrics.widthPixels;
    density  = context.getResources().getDisplayMetrics().density;
    dpHeight = pixHeight / density;
    dpWidth  = pixWidth / density;
}

public float getPixHeight(){return pixHeight;}
public float getPixWidth(){return pixWidth;}
public float getDpHeight(){return dpHeight;}
public float getDpWidth(){return dpWidth;}
public float getDensity(){return density;}

}

明らかに、さまざまなサイズと密度で何が必要かを把握するために、いくつかの計算を行う必要があります. また、使用していない画面の部分 (ActionBar など) も考慮する必要があります。

于 2013-01-29T02:54:28.150 に答える