Design an Android interface compatible with different screen sizes

Source: Internet
Author: User

The Android screen type is available in hundreds of different sizes, from small phones to large televisions. So to make our application compatible with different screen sizes, the same app can be used by more users.

One, support different screen size

1. Use "Wrap_content" and "Match_parent"

To ensure the flexibility of the layout to accommodate different sizes of screens, we should use "wrap_content" to match the minimum size of the component and use "Match_parent" to set some views to match the size of the parent view. The difference between setting and directly setting the view size (such as 48DIP) is that the view's space can be expanded arbitrarily as the screen size (the size of the parent view). For example, the following layout:

<linearlayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" an Droid:layout_width = "Match_parent" android:layout_height = "match_parent" > <linearlayout android:layout_w                   Idth = "Match_parent" Android:id = "@+id/linearlayout1" android:gravity = "center"                     Android:layout_height = "50DP" > <imageview android:id = "@+id/imageview1" Android:layout_height = "Wrap_content" android:layout_width = "Wrap_content" a  NDROID:SRC = "@drawable/logo" android:paddingright = "30DP" android:layout_gravity =                "Left" Android:layout_weight = "0"/> <view android:layout_height = "Wrap_content" Android:id = "@+id/view1" android:layout_width = "Wrap_content" Android:layout_wei        ght = "1"/> <button android:id = "@+id/categorybutton" Android:background = "@drawable/button_bg"  Android:layout_height = "Match_parent" android:layout_weight = "0" android:layout_width = "120DP" style = "@ style/categorybuttonstyle"/> </LinearLayout> <fragment Andro Id:id = "@+id/headlines" android:layout_height = "fill_parent" android:name = "com.example.an Droid.newsreader.HeadlinesFragment "android:layout_width =" match_parent "/> </LinearLayout>
When we flip the phone screen you can see the horizontal and vertical screens adapted as follows:


2. Using Relative_layout

We can use LinearLayout through "wrap_content" and "match_parent" to achieve a lot of complex layout, but LinearLayout is good at controlling the linear relationship, if you want to precisely control the plane relationship, using relative_ Layout is appropriate, and it can specify a spatial relationship between two components. For example, the following code:

<? Version of XML = "1.0" encoding = "Utf-8"?> <relativelayout xmlns:android = "Http://schemas.android.com/apk/res/andr OID "android:layout_width =" match_parent "android:layout_height =" Match_parent "> <textview an         Droid:id = "@+id/label" android:layout_width = "match_parent" android:layout_height = "Wrap_content" Android:text = "Type here:"/> <edittext android:id = "@+id/entry" android:layout_width = "Ma          Tch_parent "android:layout_height =" wrap_content "Android:layout_below =" @id/label "/> <button Android:id = "@+id/ok" android:layout_width = "wrap_content" android:layout_height = "Wrap_conten T "Android:layout_below =" @id/entry "android:layout_alignparentright =" true "Android:layout_marg Inleft = "10DP" Android:text = "OK"/> <button android:layout_width = "Wrap_content" an Droid:layout_height= "Wrap_content" android:layout_toleftof = "@id/ok" android:layout_aligntop = "@id/ok" Android:tex t = "Cancel"/> </RelativeLayout>
On the small screen size of the QVGA phone, the following is displayed:

The WSVGA screen on the big screen shows the following:

3. Use Dimension Qualifier

Above we through "wrap_content", "Match_parent", "relative_layout" method to achieve different screen size view stretching, effectively solve the screen size problem, In addition to these methods, we can use the dimension qualifier to make a special interface layout for different screens.

The first column is the feature of the screen, with size, density, direction, aspect ratio

The second column is the qualifier notation (such as large, we need to create a folder name of Layout-large)

The third column is the relevant explanation and description

For example, we now create two folders under our res to match different size screens with different qualifier folder names

Res/layout/main.xml

<linearlayout  xmlns:android = "Http://schemas.android.com/apk/res/android"     android:orientation = " Vertical "     android:layout_width =" Match_parent "     android:layout_height =" Match_parent ">     < Fragment  Android:id = "@+id/headlines"               android:layout_height = "Fill_parent"               android:name = " Com.example.android.newsreader.HeadlinesFragment "               android:layout_width =" match_parent "  /> </ Linearlayout>
Res/layout-large/main.xml

<linearlayout  xmlns:android = "Http://schemas.android.com/apk/res/android"     android:layout_width = "Fill _parent "     android:layout_height =" Fill_parent "     android:orientation =" Horizontal ">     <fragment  android:id = "@+id/headlines"               android:layout_height = "Fill_parent"               android:name = " Com.example.android.newsreader.HeadlinesFragment "               android:layout_width =" 400DP "               android:layout_ MarginRight = "10DP"/>     <fragment  android:id = "@+id/article" android:layout_height               = "Fill_ Parent "               android:name =" Com.example.android.newsreader.ArticleFragment "               android:layout_width =" Fill_ Parent "  /> </LinearLayout>
Above the layout in the big screen we put two large views on the entire screen, two small screens we only placed a view. Our device automatically looks for its layout based on the screen size.

4. Use the minimum width qualifier

The above is based on the screen size to match the different layout files, but many times we may need to display different layouts on different big screen phones (such as Galaxy Tab display 5 or 7), after Android3.2 has a minimum width qualifier to solve the problem.

Assuming SW600DP to represent our minimum width, the layout file can be represented as follows:

Layout res/layout/main.xml for small screen phones

<linearlayout  xmlns:android = "Http://schemas.android.com/apk/res/android"     android:orientation = " Vertical "     android:layout_width =" Match_parent "     android:layout_height =" Match_parent ">     < Fragment  Android:id = "@+id/headlines"               android:layout_height = "Fill_parent"               android:name = " Com.example.android.newsreader.HeadlinesFragment "               android:layout_width =" match_parent "  /> </ Linearlayout>
Layout with device width greater than or equal to 600DP res/layout/layout-sw600dp.xml

<linearlayout  xmlns:android = "Http://schemas.android.com/apk/res/android"     android:layout_width = "Fill _parent "     android:layout_height =" Fill_parent "     android:orientation =" Horizontal ">     <fragment  android:id = "@+id/headlines"               android:layout_height = "Fill_parent"               android:name = " Com.example.android.newsreader.HeadlinesFragment "               android:layout_width =" 400DP "               android:layout_ MarginRight = "10DP"/>     <fragment  android:id = "@+id/article" android:layout_height               = "Fill_ Parent "               android:name =" Com.example.android.newsreader.ArticleFragment "               android:layout_width =" Fill_ Parent "  /> </LinearLayout>
Note that this qualifier is not supported on phones older than 3.2. It must be replaced by the large of the dimension qualifier before 3.2.

5. Use layout aliases

Combined with the above analysis, if we are going to support a device that contains a large screen size, you must include several layouts such as the following to match

Res/layout/main.xml

Res/layout/layout-large

Res/layout/layout-sw600dp

The last two files are the same, in order to support the 3.2 and later versions, in order to avoid confusion and maintenance difficulties, we can give these layouts alias, such as the following layout name:

Res/layout/main.xml Single-panel layout

Res/layout/main_twopanes.xml Dual Panel layout

and add these two files

Res/values-large/layout.xml

<resources>    <item name= "main" type= "layout" > @layout/main_twopanes</item></resources>
Res/values-sw600dp/layout.xml

<resources>    <item name= "main" type= "layout" > @layout/main_twopanes</item></resources>
You can see that the contents of these two files are the same, they just establish a connection between main and Mian_twospanes, and can get the corresponding layout file alias under different value files by screen size.

6. Use Direction qualifier

Sometimes our application may need to flip the phone to achieve better results, the layout also needs to make corresponding changes, you can also make the corresponding layout files. Please refer to the table above (land and port).

7. Using Nine-patch diagram

For different screen sizes we usually need to use different sizes of picture resources, so when designing a variable-sized component, be sure to use the Nine-patch diagram.

For example, the Nine-patch chart has the following tensile effects on different size phones:


Please refer to my other blog post on how to make Nine-patch: http://blog.csdn.net/dawanganban/article/details/17379193

Second, support different screen density

We should not use absolute pixel size when designing layouts, but we should use DP and SP. Different devices have different screen densities, and the number of pixels on a 1-inch phone with different densities is different. To get people to understand these basic concepts, let me explain one by one:

dpi pixel density : pixel density, which is the number of pixels per inch of the screen, the greater the pixel density, the richer the display detail.

We can use this formula to represent 1DPI = 1pix/1in

DP (DIP) device independent pixels : On a screen density of 160 display, 1dip=1px

With these concepts, let's figure out how long 1dip is (how many inches).

result = In/dip

result = IN/PX (as defined by DP)

result = 1/(px/in)

result = 1/dpi

result = 1/160

From the above deduction can be seen in the screen density of 160 on the display 1dip is actually 1/160 inches, is a screen size absolute units.


Android gives us the ability to adapt to different resolutions of the resource pack, we only need to do a set of resources to automatically help us to convert to the corresponding DPI (resolution) of the size, zoom in and zoom out as shown.

three, different UI code logic adaptation

1. Determine the current layout

Public  class  newsreaderactivity  extends  fragmentactivity  {     Boolean misdualpane;     @Override     public  void OnCreate (Bundle savedinstancestate)  {         super. OnCreate ( Savedinstancestate);         Setcontentview (R. layout. main_layout);         View Articleview = Findviewbyid (R. Id. article);         Misdualpane = articleview! =  null  &&                          articleview. getvisibility ()  = =  View. VISIBLE;     } }
As shown in the above code, we can judge the layout file by the visibility of some view.

2, according to different layout, to make different logic

@Overridepublic void onheadlineselected (int index) {    martindex = index;    if (Misdualpane) {/        * display article on the right pane *        /marticlefragment.displayarticle ( Mcurrentcat.getarticle (index));    } else {        /* start a separate activity *        /Intent Intent = new Intent (this, articleactivity.class);        Intent.putextra ("Catindex", Mcatindex);        Intent.putextra ("Artindex", index);        StartActivity (intent);    }}
3. Use code Snippets for reuse

<linearlayout  xmlns:android = "Http://schemas.android.com/apk/res/android"     android:layout_width = "Fill _parent "     android:layout_height =" Fill_parent "     android:orientation =" Horizontal ">     <fragment  android:id = "@+id/headlines"               android:layout_height = "Fill_parent"               android:name = " Com.example.android.newsreader.HeadlinesFragment "               android:layout_width =" 400DP "               android:layout_ MarginRight = "10DP"/>     <fragment  android:id = "@+id/article" android:layout_height               = "Fill_ Parent "               android:name =" Com.example.android.newsreader.ArticleFragment "               android:layout_width =" Fill_ Parent "  /> </LinearLayout>

Design an Android interface compatible with different screen sizes

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.