How to define dimens.xml for every different screen size in android?

后端 未结 9 1801
轮回少年
轮回少年 2020-11-22 00:43

When supporting different screen sizes (densities) in Android often the focus is on creating different layouts for every possible screen. I.E.

相关标签:
9条回答
  • 2020-11-22 01:01

    You have to create a different values folder for different screens and put dimens.xml file according to densities.

    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)
    
    0 讨论(0)
  • 2020-11-22 01:04

    Use Scalable DP

    Although making a different layout for different screen sizes is theoretically a good idea, it can get very difficult to accommodate for all screen dimensions, and pixel densities. Having over 20+ different dimens.xml files as suggested in the above answers, is not easy to manage at all.

    How To Use:

    To use sdp:

    1. Include implementation 'com.intuit.sdp:sdp-android:1.0.5' in your build.gradle,
    2. Replace any dp value such as 50dp with a @dimen/50_sdp like so:

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

    How It Works:

    sdp scales with the screen size because it is essentially a huge list of different dimens.xml for every possible dp value.

    See It In Action:

    Here it is on three devices with widely differing screen dimensions, and densities:

    Note that the sdp size unit calculation includes some approximation due to some performance and usability constraints.

    0 讨论(0)
  • 2020-11-22 01:06

    In case you want to view more: Here's a link for a list of devices (tablet, mobile, watches), including watch,chromebook, windows and mac. Here you can find the density, dimensions, and etc. Just based it in here, it's a good resource if your using an emulator too.

    If you click a specific item, it will show more details in the right side.

    Since it's Android , I will post related to it.

    ~ It's better if you save a copy of the web. To view it offline.

    0 讨论(0)
  • 2020-11-22 01:16

    Android 3.2 introduces a new approach to screen sizes,the numbers describing the screen size are all in “dp” units.Mostly we can use

    smallest width dp: the smallest width available for application layout in “dp” units; this is the smallest width dp that you will ever encounter in any rotation of the display.

    To create one right click on res >>> new >>> Android resource directory

    From Available qualifiers window move Smallest Screen Width to Chosen qualifiers

    In Screen width window just write the "dp" value starting from you would like Android Studio to use that dimens.

    Than change to Project view,right click on your new created resource directory

    new >>> Values resource file enter a new file name dimens.xml and you are done.

    0 讨论(0)
  • 2020-11-22 01:18

    You can put dimens.xml in

    1) values

    2) values-hdpi

    3) values-xhdpi

    4) values-xxhdpi

    And give different sizes in dimens.xml within corresponding folders according to densities.

    0 讨论(0)
  • 2020-11-22 01:19

    You have to create Different values folder for different screens . Like

    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
    

    For more information you may visit here

    Different values folders in android

    http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html

    Edited By @humblerookie

    You can make use of Android Studio plugin called Dimenify to auto generate dimension values for other pixel buckets based on custom scale factors. Its still in beta, be sure to notify any issues/suggestions you come across to the developer.

    0 讨论(0)
提交回复
热议问题