[英]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.