[英]Android: Layout looks different on same screen resolution devices
我是Android编程的新手。 我有一个问答游戏。 在主要活动中,您可以点击4个答案之一以查看结果是否正确。 我的问题如下:我已经在S4 mini上测试了此选择选项,该选择选项显示了合适的尺寸,但是在Sony Xperia M2上看起来大了两倍! 两种设备的屏幕分辨率相同(540 x 960像素)。
这是我主要活动中的代码(M280):
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:
尝试尽可能多地使用wrap_content和match_parent / fill_parent,避免尽可能使用硬编码的“ dp”值。
如果您需要使用其他视图组,例如RelativeLayouts
1)保持父级相对布局
2)哪个有子级LinearLayouts
3)将子视图保留在LinearLayout中。
获得更多的额外设计时间,但在针对许多设备时省去了很多麻烦
像这样的东西:
对于iOS:
使用类似于权重和权重总和的自动布局教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.