How can i support multiple screen sizes in android

2020-05-28 19:17发布

I want to support my android screen in multiple screen sizes but i can do it with maintaining multiple xml layout file's

but according to requirement it i supposed to be done with the single XML layout in order to optimize the app usage.

so please can you help me i have gone through multiple tutorials multiple links but not able to get last option with stack overflow

标签: android xml
9条回答
女痞
2楼-- · 2020-05-28 19:30

Well that depends on your code

  1. Dont use Static values
  2. Try to use Wrap content
  3. Use Relative and Linear Layout Depends on your Requirement
  4. For Drawables Use every DPI Folder

Look at these Links

1)https://developer.android.com/training/multiscreen/screensizes.html

2)How to support different screen size in android

3)Supporting multiple screen size - Android

查看更多
狗以群分
3楼-- · 2020-05-28 19:31

May be you can try below library which manages all the screen size resolution automatically.

compile 'com.intuit.sdp:sdp-android:1.0.4'

You need to just add the dependency in your build.gradle file and you are done.

You need to specify like:

android:layout_height="@dimen/_10sdp"

Instead of usual:

android:layout_height="@dimen/10dp"
查看更多
时光不老,我们不散
4楼-- · 2020-05-28 19:32

drawable:

For images, you have to manage different drawable folders as per screen resolution:

drawable-ldpi        //240x320
drawable-mdpi        //320x480
drawable-hdpi        //480x800
drawable-xhdpi       //720x1280
drawable-xxhdpi      //1080X1920
drawable-xxxhdpi     //1440X2560
drawable-tvdpi       // nexus 7 etc 
drawable-xlarge-xhdpi //tablet like nexus 10  

dimes: For dimens, If you are using different static dimes as per your UI then you have to define them respected to their values... folders.

Values folder for different smart phones resolutions:

values-ldpi\dimens.xml
values-mdpi\dimens.xml
values-hdpi\dimens.xml
values-xhdpi\dimens.xml
values-xxhdpi\dimens.xml
values-xxxhdpi\dimens.xml

Note: If you are using sp/sip/dp/dip then these values will be adjusted based on the density of device. e.g Suppose you had set 10sp for TextView in mdpi(320X480) resolution device. Then this same value will be auto adjusted for other resolutions.

mdpi(10sp==10px)
hdpi(15px)
xhdpi(20px)
xxhdpi(30px)
xxxhdpi(40px)

These are the some general usage values folder which are used to manage dimens related to their screen resolutions.

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-xxxhdpi                 1440X2560 xxxhdpi

values-xxhdpi                  1080X1920 xxhdpi

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 knowledge in depth go with Support screen resolution

查看更多
We Are One
5楼-- · 2020-05-28 19:34

There are three ways to do this :

1) Put your images and icon in different drawable folder including drawable-mdpi , drawable-hdpi , drawable-xhdpi , drawable-xxhdpi , drawable-xxxhdpi which will be access image from particular folder based on screen resolutions. Don't apply fixed values everywhere for image. Fixed dimensions can be vary with different device and image will be looked stretch. So use wrap_content

2) Set dimensions in dimens.xml for different values folder.

values-mdpi\dimens.xml
values-hdpi\dimens.xml
values-xhdpi\dimens.xml
values-xxhdpi\dimens.xml
values-xxxhdpi\dimens.xml

3) Use LinearLayout with proper using of android:layout_weight.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-05-28 19:35

You can define multi dimen files, and add dimensions of each view according to screen size in there. You can find detailed answer here

A set of six generalized densities:

  • ldpi (low) ~120dpi
  • mdpi (medium) ~160dpi
  • hdpi (high) ~240dpi
  • xhdpi (extra-high) ~320dpi
  • xxhdpi (extra-extra-high) ~480dpi
  • xxxhdpi (extra-extra-extra-high) ~640dpi

Here is the official docs

查看更多
萌系小妹纸
7楼-- · 2020-05-28 19:37

try this https://developer.android.com/guide/topics/manifest/supports-screens-element.html

   <supports-screens android:resizeable=["true"| "false"]
                      android:smallScreens=["true" | "false"]
                      android:normalScreens=["true" | "false"]
                      android:largeScreens=["true" | "false"]
                      android:xlargeScreens=["true" | "false"]
                      android:anyDensity=["true" | "false"]
                      android:requiresSmallestWidthDp="integer"
                      android:compatibleWidthLimitDp="integer"
                      android:largestWidthLimitDp="integer"/>
查看更多
登录 后发表回答