Android:布局在相同屏幕分辨率的设备上看起来不同

Android: Layout looks different on same screen resolution devices

我是 Android 编程新手。我有一个问答游戏。在主 activity 中,您可以点击 4 个答案之一来查看结果是否正确。我的问题如下:我在 S4 mini 上测试了这个 select 选项,它显示 select 选项大小合适,但在 Sony Xperia M2 上看起来大两倍!两种设备的屏幕分辨率相同(540 x 960 像素)。

这是我的主要代码 (M280) activity:

final Point dim = Utils.getScreenDimensions(MainActivity.this);
int M280 = 280;
            if (SplashActivity.bSmallScreen) {
                M280 = 60;
            }

            if (dim.y == 240 && dim.x == 320) {
                M280 = 60;
            }

            if (dim.y == 320 && dim.x == 480) {
                M280 = 80;
            }

            if (dim.y == 480 && dim.x == 800) {
                M280 = 140;
            }

            if (dim.y == 480 && dim.x == 854) {
                M280 = 140;
            }

            if (dim.y == 540 && dim.x == 960) {
                M280 = 160;
            }

            if (dim.y == 720 && dim.x == 1280) {
                M280 = 200;
            }

            if (dim.y == 768 && dim.x == 1280) {
                M280 = 200;
            }

            if (dim.y == 800 && dim.x == 1280) {
                M280 = 220;
            }

            if (dim.y == 1080 && dim.x == 1920) {
                M280 = 280;
            }

            if (dim.y == 1440 && dim.x == 2560) {
                M280 = 300;
            }

            if (Utils.isTablet(mContext)) {
                M280 = 300;
            }
            if (i==0) {
                final LinearLayout LL2Row = new LinearLayout(MainActivity.this);

                final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        M280);
                LL2Row.setLayoutParams(lp);
                LL2Row.setTag("LL21");
                LL2Row.setOrientation(LinearLayout.HORIZONTAL);
                LL2Row.setWeightSum(2);

                LLQuestionAnswers.addView(LL2Row);
            }

            //TODO new, 2-nal hozzaadunk egy ll-t ami 2 reszbol all
            if (i==2) {
                final LinearLayout LL2Row = new LinearLayout(MainActivity.this);

                final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        M280);

                LL2Row.setLayoutParams(lp);
                LL2Row.setTag("LL22");
                LL2Row.setOrientation(LinearLayout.HORIZONTAL);
                LL2Row.setWeightSum(2);

                LLQuestionAnswers.addView(LL2Row);
            }    

根据当前的设备尺寸、纵横比和密度碎片, 在设计界面时始终优先使用比率,无论是 Android 还是 iOS.

像这样:

对于 Android:

  • 尽可能多地使用 LinearLayouts,在父视图上保持权重和并将它们分布在子视图中,这样无论显示类型如何,视图都会拉伸到相同的比例,tutorial
  • 尽量使用wrap_content和match_parent/fill_parent,尽可能避免使用硬编码的"dp"值。

  • 如果需要使用其他视图组如RelativeLayouts

    1)保留父级 RelativeLayout
    2) 有子 LinearLayouts
    3) 将您的子视图保留在该 LinearLayout 中。

获得更多的额外时间来设计,但在针对许多设备时省去了很多麻烦

像这样:

对于 iOS:

使用类似于权重和权重和的自动布局,tutorial