首页 文章

如何在android中为每个不同的屏幕大小定义dimens.xml?

提问于
浏览
106

在_2575709中支持不同的屏幕尺寸(密度)时,通常关注的是为每个可能的屏幕创建不同的布局 . I.E.

  • ldpi

  • mdpi

  • hdpi

  • xhdpi

  • xxhdpi

  • xxxhdpi

我为 xhdpi 屏幕设计了一个布局作为参考,并在 dimens.xml 中定义了 dimensions . 现在 I want to give support it to every possible screen size. How can I do that?

据我所知,我可以使用this工具为其他屏幕尺寸找出合适的 dimens.xml 并将其添加到我的项目中 . 这是在我的情况下这样做的正确方法吗?

另一个问题, do I only need to create dimens.xml for above screen dimensions? If yes then what is w820dp?

谢谢你的帮助 . 我需要支持 phones only (不是平板电脑或其他设备) .

9 回答

  • 6

    你必须创建 Different values folder for different screens . 喜欢

    values-sw720dp          10.1” tablet 1280x800 mdpi
    
    values-sw600dp          7.0”  tablet 1024x600 mdpi
    
    values-sw480dp          5.4”  480x854 mdpi 
    values-sw480dp          5.1”  480x800 mdpi 
    
    values-xxhdpi           5.5"  1080x1920 xxhdpi
    values-xxxhdpi           5.5" 1440x2560 xxxhdpi
    
    values-xhdpi            4.7”   1280x720 xhdpi 
    values-xhdpi            4.65”  720x1280 xhdpi 
    
    values-hdpi             4.0” 480x800 hdpi
    values-hdpi             3.7” 480x854 hdpi
    
    values-mdpi             3.2” 320x480 mdpi
    
    values-ldpi             3.4” 240x432 ldpi
    values-ldpi             3.3” 240x400 ldpi
    values-ldpi             2.7” 240x320 ldpi
    

    enter image description here

    有关更多信息,请访问此处

    android中的不同值文件夹http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html

    Edited@humblerookie

    您可以使用名为Dimenify的Android Studio插件,根据自定义比例因子自动为其他像素存储桶生成维度值 . 它仍处于测试阶段,请务必通知您遇到的任何问题/建议 .

  • 16

    我们希望在不同的屏幕上看到所需视图大小的变化 .

    我们需要为不同的屏幕创建不同的值文件夹,并根据屏幕密度放置dimens.xml文件 .

    当我在不同的值文件夹中更改dimens.xml时,我已经使用了一个TextView并观察了更改 .

    请按照流程进行操作

    normal - xhdpi \ dimens.xml

    当我们更改normal - xhdpi \ dimens.xml时,以下设备可以更改屏幕的大小

    nexus 5X(5.2“* 1080 * 1920:420dpi)

    nexus 6P(5.7“* 1440 * 2560:560dpi)

    nexus 6(6.0“* 1440 * 2560:560dpi)

    nexus 5(5.0“,1080 1920:xxhdpi)

    nexus 4(4.7“,768 * 1280:xhdpi)

    Galaxy nexus(4.7“,720 * 1280:xhdpi)

    4.65“720p(720 * 1280:xhdpi)

    4.7“WXGA(1280 * 720:Xhdpi)

    Xlarge - xhdpi \ dimens.xml

    当我们更改Xlarge-xhdpi \ dimens.xml时,以下设备可以更改屏幕的大小

    nexus 9(8.9“,2048 * 1556:xhdpi)

    nexus 10(10.1“,2560 * 1600:xhdpi)

    large - xhdpi \ dimens.xml

    当我们更改large-xhdpi \ dimens.xml时,以下设备可以更改屏幕的大小

    nexus 7(7.0“,1200 * 1920:xhdpi)

    nexus 7(2012)(7.0“,800 * 1280:tvdpi)

    以下屏幕显示在“搜索通用手机和平板电脑”中

    large - mdpi \ dimens.xml

    当我们更改large-mdpi \ dimens.xml时,以下设备可以更改屏幕的大小

    5.1“WVGA(480 * 800:mdpi)

    5.4“FWVGA(480 * 854:mdpi)

    7.0“WSVGA(平板电脑)(1024 * 600:mdpi)

    normal - hdpi \ dimens.xml

    当我们更改normal-hdpi \ dimens.xml时,以下设备可以更改屏幕的大小

    nexus s(4.0“,480 * 800:hdpi)

    nexus one(3.7“,480 * 800:hdpi)

    small - ldpi \ dimens.xml

    当我们更改small-ldpi \ dimens.xml时,以下设备可以更改屏幕的大小

    2.7英寸QVGA滑块(240 * 320:ldpi)

    2.7“QVGA(240 * 320:ldpi)

    xlarge - mdpi \ dimens.xml

    当我们更改xlarge -mdpi \ dimens.xml时,以下设备可以更改屏幕的大小

    10.1“WXGA(平板电脑)(1280 * 800:MDPI)

    normal - ldpi \ dimens.xml

    当我们更改normal - ldpi \ dimens.xml时,以下设备可以更改屏幕的大小

    3.3“WQVGA(240 * 400:LDPI)

    3.4“WQVGA(240 * 432:LDPI)

    normal - hdpi \ dimens.xml

    当我们更改normal-hdpi \ dimens.xml时,以下设备可以更改屏幕的大小

    4.0“WVGA(480 * 800:hdpi)

    3.7“WVGA(480 * 800:hdpi)

    3.7“FWVGA滑块(480 * 854:hdpi)

    normal - mdpi \ dimens.xml

    当我们更改normal - mdpi \ dimens.xml时,以下设备可以更改屏幕的大小

    3.2“HVGA滑块(ADP1)(320 * 480:MDPI)

    3.2“QVGA(ADP2)(320 * 480:MDPI)

  • 228

    使用可扩展DP

    虽然为不同的屏幕尺寸制作不同的布局在理论上是一个好主意,但是它很难适应屏幕尺寸和像素密度 . 如上面的答案所示,拥有超过20个不同的 dimens.xml 文件根本不容易管理 .

    How To Use:

    要使用 sdp

    • build.gradle 中包含 implementation 'com.intuit.sdp:sdp-android:1.0.5'

    • @dimen/50_sdp 替换任何 dp 值,例如 50dp 所以:

    <TextView
     android:layout_width="@dimen/_50sdp"
     android:layout_height="@dimen/_50sdp"
     android:text="Hello World!" />
    

    How It Works:

    sdp 与屏幕大小进行比例缩放,因为它基本上是每个可能的 dp 值的不同 dimens.xml 的巨大列表 .

    enter image description here

    See It In Action:

    这里有三种设备,屏幕尺寸和密度差异很大:

    enter image description here

    请注意,由于某些性能和可用性限制, sdp size单位计算包括一些近似值 .

  • 4

    您必须为不同的屏幕创建不同的 values 文件夹,并根据密度放置 dimens.xml 文件 .

    1) values
    
    2) values-hdpi (320x480 ,480x800)
    
    3) values-large-hdpi (600x1024)
    
    4) values-xlarge (720x1280 ,768x1280 ,800x1280 ,Nexus7 ,Nexus10)
    
    5) values-sw480dp (5.1' WVGA screen)
    
    6) values-xhdpi (Nexus4 , Galaxy Nexus)
    
  • 6

    我上传了一个简单的java程序,它将您想要的项目位置和维度文件作为输入 . 基于此,它将在控制台中输出相应的维度文件 . 这是它的链接:

    https://github.com/akeshwar/Dimens-for-different-screens-in-Android/blob/master/Main.java

    这是参考的完整代码:

    public class Main {
    
    
        /**
         * You can change your factors here. The current factors are in accordance with the official documentation.
         */
        private static final double LDPI_FACTOR = 0.375;
        private static final double MDPI_FACTOR = 0.5;
        private static final double HDPI_FACTOR = 0.75;
        private static final double XHDPI_FACTOR = 1.0;
        private static final double XXHDPI_FACTOR = 1.5;
        private static final double XXXHDPI_FACTOR = 2.0;
    
        private static double factor;
    
        public static void main(String[] args) throws IOException {
    
    
            Scanner in = new Scanner(System.in);
            System.out.println("Enter the location of the project/module");
            String projectPath = in.nextLine();
    
            System.out.println("Which of the following dimension file do you want?\n1. ldpi \n2. mdpi \n3. hdpi \n4. xhdpi \n5. xxhdpi \n6. xxxhdpi");
    
            int dimenType = in.nextInt();
    
            switch (dimenType) {
                case 1: factor = LDPI_FACTOR;
                    break;
                case 2: factor = MDPI_FACTOR;
                    break;
                case 3: factor = HDPI_FACTOR;
                    break;
                case 4: factor = XHDPI_FACTOR;
                    break;
                case 5: factor = XXHDPI_FACTOR;
                    break;
                case 6: factor = XXXHDPI_FACTOR;
                    break;
                default:
                    factor = 1.0;
            }
    
            //full path = "/home/akeshwar/android-sat-bothIncluded-notintegrated/code/tpr-5-5-9/princetonReview/src/main/res/values/dimens.xml"
            //location of the project or module = "/home/akeshwar/android-sat-bothIncluded-notintegrated/code/tpr-5-5-9/princetonReview/"
    
    
            /**
             * In case there is some I/O exception with the file, you can directly copy-paste the full path to the file here:
             */
            String fullPath = projectPath + "/src/main/res/values/dimens.xml";
    
            FileInputStream fstream = new FileInputStream(fullPath);
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    
            String strLine;
    
            while ((strLine = br.readLine()) != null)   {
                modifyLine(strLine);
            }
            br.close();
    
        }
    
        private static void modifyLine(String line) {
    
            /**
             * Well, this is how I'm detecting if the line has some dimension value or not.
             */
            if(line.contains("p</")) {
                int endIndex = line.indexOf("p</");
    
                //since indexOf returns the first instance of the occurring string. And, the actual dimension would follow after the first ">" in the screen
                int begIndex = line.indexOf(">");
    
                String prefix = line.substring(0, begIndex+1);
                String root = line.substring(begIndex+1, endIndex-1);
                String suffix = line.substring(endIndex-1,line.length());
    
    
                /**
                 * Now, we have the root. We can use it to create different dimensions. Root is simply the dimension number.
                 */
    
                double dimens = Double.parseDouble(root);
                dimens = dimens*factor*1000;
                dimens = (double)((int)dimens);
                dimens = dimens/1000;
                root = dimens + "";
    
                System.out.println(prefix + " " +  root + " " + suffix );
    
            }
    
            System.out.println(line);
        }
    }
    
  • 0

    你可以把 dimens.xml 放进去

    1) values

    2) values-hdpi

    3) values-xhdpi

    4) values-xxhdpi

    并根据密度在相应文件夹中的 dimens.xml 中给出不同的大小 .

  • 1

    如果你想查看更多:这是一个link的设备列表(平板电脑,手机, Watch ),包括 watchchromebookwindowsmac . 在这里你可以找到密度,尺寸等 . 只是在这里,如果你也使用模拟器,这是一个很好的资源 .

    如果单击特定项目,它将在右侧显示更多详细信息 .
    sample

    因为它是 Android ,我将发布与之相关的信息 .
    sample1

    sample2

    〜如果你保存一份网页,那就更好了 . 要离线查看 .

  • 23

    Android 3.2引入了一种新的屏幕尺寸方法,描述屏幕尺寸的数字都是“dp”单位 . 我们几乎可以使用

    smallest width dp: “dp”单位可用于应用程序布局的最小宽度;这是您在显示器的任何旋转中遇到的最小宽度dp .

    要创建一个,请右键单击 res >>> new >>> Android resource directory

    Available qualifiers 窗口移动 Smallest Screen WidthChosen qualifiers

    Screen width 窗口中,只需编写"dp"值,您希望Android Studio使用该维度 .

    然后更改为 Project 视图,右键单击新创建的资源目录

    new >>> Values resource file 输入新文件名 dimens.xml ,您就完成了 .

  • 0

    有一个很好的图书馆,可以处理一切,减少你的痛苦 . 要使用它,只需在gradle中添加两个依赖项 -

    implementation 'com.intuit.ssp:ssp-android:1.0.5'
     implementation 'com.intuit.sdp:sdp-android:1.0.5'
    

    在那之后使用这样的尺寸 -

    android:layout_marginTop="@dimen/_80sdp"
    

相关问题