Analysis of application scheme of building Android Adaptive layout

Source: Internet
Author: User

        due to the current app needs to fit the phone and tablet, so I'm studying how to build a layout that adapts to all screen sizes.
        I have a lot of experience on the adaptive layout of the Web, such as using a grid stream, media queries properties in CSS3, and so on, all of which enable adaptive layouts on the web, So I want to try it on Android.
        on android , the configuration Qualifiers the way 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 the 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, we need to create a base class style.

Res/values/styles.xml

<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" ><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:o Rientation ">vertical</item>     <item name=" Android:background "> @drawable/container_background </item>     </style></span></span></span>

For flat (vertical), we can add some margins because the screen is large enough.

Res/values-sw600dp/styles.xml
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" ><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:o Rientation ">vertical</item>     <item name=" Android:background "> @drawable/container_background </item>     </style></span></span></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-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" ><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> </span></span>

Then we can use the style file on different screens, like this:
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" ><span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><linearlayout style= "@style/container" > ...     buttons, edit texts, text views, etc     ... </LinearLayout></span></span></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-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" ><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></span></ Span>


Put these styles in the corresponding configuration folder, then you can hide the corresponding controls when needed.

<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" ><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></span></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 (don't be misled by these items, such as:http://www.ijiami.cn/ Appprotect, they are not as powerful as you might think, but Android is migrating to Gradle, which makes it easier to make a layout framework for the Android front-end.

Analysis of application scheme of building Android Adaptive layout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.