简体   繁体   中英

How to store 3 layout files for phone, small tablets and big tablets

I have an app with about 20 xml layout files. Altough having spent quite some time to make a generic constraint layout for all devices (which was not possible for me), I now want to create an individual layout file for phones, small tablets and big tabelts. So I will have have 20 layout files for phones (5' - 6.8' display), small tablets (7' - 9' display) and big tablets (more than 9' display).

My question is, where shall I store them such that Android automatically picks the correct layout depending on the used device? In this question Layout for tablets in Android it is said that I should store the xml layout files in

res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)

or

res/layout/my_layout.xml            // layout for normal screen size
res/layout-small/my_layout.xml      // layout for small screen size
res/layout-large/my_layout.xml      // layout for large screen size
res/layout-large-land/my_layout.xml // layout for large screen size in landscape mod

However, I can't distinguish the devices by their pixel density because different devices with the same display size can have different resolutions. Further, I would like to make the 20 xml layout files not for one specific size but for ranges of display sizes (otherwise I'd have to create too many xml layout files).

How can I define folders for storing xml layout files for ranges of display sizes: phones (5' - 6.8' display), small tablets (7' - 9' display) and big tablets (more than 9' display).

Reminder : I'll appreciate every furher comment as I still don't know what to do

Use this dependency.

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

Then apply

android:layout_width="@dimen/_24sdp"
android:layout_height="@dimen/_24sdp"

For your question and anybody with same issue,

  • Use Constraint layout to make flexible UIs and use qualifiers.
  • You will have to have multiple layout files to better support all screen sizes and orientations,
  • but no need to make those folders yourself - make a default xml layout for a small mobile screen first, then make an xml layout file with same name as the default one but add a qualifier sw-600dp (or as per use case) to it from 'Available qualifiers' list and adjust the layout. then make one with sw-700dp and adjust it.
  • Android will automatically detect which layout file to use based on the qualifiers provided.
  • For reference, please see this example from docs. Quoting from which Android follows an order of precedence when determining which resources to apply . This Order is the same in which qualifiers are given in 'Available qualifiers' list to choose from.

First of all, you wrote down that you've decided to apply changes in the display based on inch sizes, this is not suggested. As there are many different digital gadgets with varying pixels in each inch of the display screen, which means just having the upper production dimension doesn't result in better resolution quality; Android provided a unit to solve the problem, it is a ratio: dots per inch "dp". Manage a time to look at Android's official guidance on how measuring works:

https://developer.android.com/guide/topics/large-screens/support-different-screen-sizes

Here you can find suitable size dividers & definitions of why are they declared.

To give your App the ability to choose an appropriate layout to display, you must add new Activity layouts or Fragment layouts in directories based on resolution. Just create a layout with the same name as the currently existing one, but change its directory to the desired size category eg: layout sw-600dp, layout sw-840dp, etc. Then design the created layout to be responsible for the mentioned display.

This is how your res may look:

这是您的资源的外观:

I could provide more clear suggestions if you could express essential parts of your code.

At last, if your App is just Handled by Activities you may need to use fragment a tool that can hold & show different layouts. Take look at this:

https://developer.android.com/guide/fragments/fragmentmanager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM