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条回答
Rolldiameter
2楼-- · 2020-05-28 19:40

If you provide text size as sp and other dimensions as dp. Android will automatically adjust for different layouts based on density of devices. If you want to control the values, you can put values in dimens.xml in each of values folders. The values folder inside res for different density devices will be following :

values-ldpi\dimens.xml
values-mdpi\dimens.xml
values-hdpi\dimens.xml
values-xhdpi\dimens.xml
values-xxhdpi\dimens.xml
values-xxxhdpi\dimens.xml
查看更多
够拽才男人
3楼-- · 2020-05-28 19:41

Create three different Layouts Folder in your res folder for all devices and use the dimensions accordingly.

Generic Layout Folders

res/layout-small
res/layout-normal
res/layout-large
res/layout-xlarge

After you are done with making your Normal/Medium Layouts follow these steps:

  1. Convert the Normal Dimensions for other Screen Sizes.
  2. Copy your Normal Layout xml files in to other Folders.
  3. Change the suffix of the dimensions used according to the folder that you are in
  4. Resize the Image Resources in your drawable folder (Width and Height - Same technique as we used for converting the dimens) and put them in their respective drawable folder (drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xdpi and so on).
  5. Then your Layouts should work on every device with correct positioning.

For converting Values

0.75 - ldpi  (small)   //mdpi dimens *0.75
1.0  - mdpi  (normal)  //First create these dimensions
1.5  - hdpi  (large)   //mdpi dimens *1.5
2.0  - xhdpi (xLarge)  //mdpi dimens *2.0

For Example

android:layout_width="66dip" //in normal
android:layout_width="100dip"//in large 66*1.5=100(approx)
android:layout_width="52dip" //in small 66*0.75=52(approx)

Also new Qualifier has been introduced - SmallestWidth - AvailableScreenWidth - AvailableScreenHeight

read more about it here https://developer.android.com/guide/practices/screens_support.html

I hope this helps.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-05-28 19:43

If you want to use single layout and that should support all the screens like ldpi, , mdpi, hdpi, x-hdpi, xx-hdpi then you have to use weight in your layout that will handle screen size for all the screens.

Here’re a few things:

If you want to divide the space equally between Views with the same weight – set 0dp as Views’ width.
If you set View width to wrap_content – size of the Views will depend on weight and content inside each View.
查看更多
登录 后发表回答