Analysis of application scheme of building Android Adaptive layout

Source: Internet
Author: User

as a result of the currentappneed 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 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.
In the
Android, it is the 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-SIZE:14PX;" ><span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><style name= "Container" >     <item name= "Android:layout_margin ">0dp</item>     <item name=" >16dp</item> "android:padding"      <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> 

        for flat panel (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-FAMILY:ARIAL;FONT-SIZE:14PX;" ><style name= "Container" >     <item name= "Android:layout_margin ">0dp</item>     <item name=" >32dp</item> "android:padding"      <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> 

        The biggest difference between vertical and horizontal on a tablet is that we add a margin value so the content doesn't fill the screen, 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-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> 


Then we can use the style file on different screens, like this:

<span style= "FONT-SIZE:14PX;" ><span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><linearlayout style= "@style/container" > ... buttons, edit texts, text views, etc ... </linearlayout&g T;</span></span>

This is the effect on the four-inch screen phone:


650) this.width=650; "src=" http://img.blog.csdn.net/20141119193637760 "/>


This is the effect on the seven-inch tablet:


650) this.width=650; "src=" http://img.blog.csdn.net/20141119193659506 "/>

This is the horizontal screen effect on the seven-inch flat panel:


650) this.width=650; "src=" http://img.blog.csdn.net/20141119193717587 "/>


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-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> 


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-FAMILY:ARIAL;FONT-SIZE:14PX;" ><linearlayout android:id= "@+id/column_one" > ... some content ... </LinearLayout> <linearl  Ayout android:id= "@+id/column_two" style= "@style/tabletlandscapeonly" > some extra content since we have Space ... </LinearLayout></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.