簡體   English   中英

Android:在相同屏幕分辨率的設備上布局看起來有所不同

[英]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)時,始終優先使用比例。

像這樣的東西: 以相對比例設計的UI

對於Android:

  • 嘗試盡可能多地使用LinearLayouts,在父視圖上保持權重總和,然后在子視圖中分配它們,這樣無論顯示類型如何,視圖將以相同的比例拉伸,本教程
  • 嘗試盡可能多地使用wrap_content和match_parent / fill_parent,避免盡可能使用硬編碼的“ dp”值。

  • 如果您需要使用其他視圖組,例如RelativeLayouts

    1)保持父級相對布局
    2)哪個有子級LinearLayouts
    3)將子視圖保留在LinearLayout中。

獲得更多的額外設計時間,但在針對許多設備時省去了很多麻煩

像這樣的東西:

布局模擬

對於iOS:

使用類似於權重和權重總和的自動布局教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM