I'm working on how to build a layout that adapts to all screen sizes because the app you're currently working on needs to fit the phone and tablet.
I have a lot of experience in the adaptive layout of the Web, such as using a grid stream, the media queries attribute in CSS3, and so on, all of which can implement the adaptive layout on the Web, so I want to try it on Android.
On Android, there is a configuration qualifiers to load different resources, based on the screen size of different phones or the orientation of the screen (vertical or horizontal), and my biggest goal is to create a layout that can be automatically scaled, Instead of loading different layout files based on different screen sizes.
In addition to making separate layout files for each device size, I found an easier way to reload style.xml files for devices of different screen sizes.
You might think it's like a CSS style, you can first define a basic style file that represents the normal device size, its path is in Values/styles.xml, and then you can define a medium device size, in Values-sw600dp/styles.xml ( 7-inch tablet), Values-sw600dp-land/styles.xml represents a horizontal screen, Values-sw720dp/styles.xml represents a 10 inch tablet, and so on.
In the adaptive grid system in CSS, we can lay out a width of 960 pixels. Container Class (no margin), and on the phone, we can also layout a 100% wide. Container (also without margin).
We can use the same method on Android, first of all, we need to create a base class style.
Res/values/styles.xml
<span style="font-family:arial;font-size:14px;"><style name="Container"> <item name="Android:layout_margin">0dp</item> <item name="android:padding">16dp</item> <item name="Android:layout_width">match_parent</item> <item name="Android:layout_height">match_parent</item> <item name="android:orientation">vertical</item> <item name="Android:background"> @drawable/container_background</item> </style></span>
For flat (vertical), we can add some margins because the screen is large enough.
Res/values-sw600dp/styles.xml
<span style="font-family:arial;font-size:14px;"><style name="Container"> <item name="Android:layout_margin">0dp</item> <item name="android:padding">32dp</item> <item name="Android:layout_width">match_parent</item> <item name="Android:layout_height">match_parent</item> <item name="android:orientation">vertical</item> <item name="Android:background"> @drawable/container_background</item> </style></span>
The biggest difference between vertical and horizontal on a tablet is that we add a margin value so that the content doesn't fill the screen, and we can also add a background image to the parent view to fill in the empty area.
Res/values-sw600dp-land/styles.xml
<span style="font-family:arial;font-size:14px;"><style name="Container"> <item name="Android:layout_marginright">130dp</item> <item name="Android:layout_marginleft">130dp</item> <item name="android:padding">32dp</item> <item name="Android:layout_width">match_parent</item> <item name="Android:layout_height">match_parent</item> <item name="android:orientation">vertical</item> <item name="Android:background"> @drawable/container_background</item> </style></span>
Then we can use the style file on different screens, like this:
<span style="font-family:arial;font-size:14px; "><linearlayout style="@style/container"> ... buttons, edit Texts, text views , etc ... </LinearLayout></span>
This is the effect on the four-inch screen phone:
This is the effect on the seven-inch tablet:
This is the horizontal screen effect on the seven-inch flat panel:
There are also some properties that are very handy in CSS (such as Bootstrap), which are helper classes such as. Visible-phone,.hidden-phone,.visible-tablet and so on, which can be done on Android.
<span style="font-family:arial;font-size:14px;"><!--Device Visibility--<style name="phoneonly"> <item name="android:visibility">gone</item> </style> <style name="tabletonly"> <item name="android:visibility">visible</item> </style> <style name="tabletportraitonly"> <item name="android:visibility">gone</item> </style> <style name="tabletlandscapeonly"> <item name="android:visibility">visible</item> </style></span>
Put these styles in the corresponding configuration folder, then you can hide the corresponding controls when needed.
<span style="font-family:arial;font-size:14px; "><linearlayout android:id="@+id/column_one"> ... some Content ... </LinearLayout> <linearlayout android:id="@+id/column_two" style="@style/tabletlandscapeonly"> ... some extra Content since we have space ... </LinearLayout></span>
For flat (horizontal), this style displays two columns, but for most devices, the second column is not displayed.
With just a few lines of XML, we were able to create a mini layout framework, and we could extend the technique to implement a common style file that we could use later in the project.
Unfortunately, it's hard to pack Android resources into jars (not gradle words), so it's hard to build a framework like bootstrap on Android, but Android is migrating to Gradle, This makes it easier to create a layout framework for the Android front-end.
Analysis of application scheme of building Android Adaptive layout