Easiest way to know (programmatically) which resources folder is used based on screen density/size

后端 未结 4 1581
遇见更好的自我
遇见更好的自我 2021-01-02 17:27

Is there a way to know exactly which layout (and other resources) are chosen by Android during runtime based on screen density and size when supporting mult

4条回答
  •  滥情空心
    2021-01-02 17:45

    Imagine you have a structure like this:

    res/
        drawable-mdpi/image.png
        drawable-hdpi/image.png
        drawable-xhdpi/image.png
        layout/main.xml
        layout-land/main.xml
        layout-xlarge/main.xml
        layout-sw600dp/main.xml
    

    and you want to know, which layout is used.

    One way would be to put a value in resources:

    res/
        values-mdpi/strange_info.xml with:
            drawable-mdpi
        values-hdpi/strange_info.xml with:
            drawable-hdpi
        values-xhdpi/strange_info.xml with:
            drawable-xhdpi
    
        values/strange_info.xml with:
            layout
        values-land/strange_info.xml with:
            layout-land
        values-xlarge/strange_info.xml with:
            layout-xlarge
        values-sw600dp/strange_info.xml with:
            layout-sw600dp
    

    In code you just do

    String mainFolderName = context.getResources().getString(R.string.main_is_from_folder);
    

    I have some doubts regarding -Xdpi qualifier, but it should work (not tested). Note that with the above structure you will get "drawable-hdpi" on ldpi devices for image.png.

提交回复
热议问题