Android down screen fit < two >

Source: Internet
Author: User

Android under screen adaptation:

Fit: The current app shows the same effect on different phones. Before the adaptation needs to first determine the current cell phone pixel density type (such as: xhdpi, hdpi, MDPI, etc.),
The following is an example of Huawei G700, Simulator, to calculate its pixel density.

Case one:

Phone Model: G700
Cell Phone Resolution: 1280*720 (Note: Mobile phone two right-angled side of the 1280 and 720 pixels respectively placed)
Cell phone Size: 5 inches (Phone diagonal length)

Suppose that A, B is two right-angled edges, C is the hypotenuse, and the Pythagorean theorem can be calculated as follows: sqrt (a*a+b*b)/C
Calculation result: sqrt (1280*1280+720*720)/5≈293.72dpi
According to Google's official documentation, the current phone closest to 320dpi, it is summarized in the XHDPI phone range, that is, 1dp=2px;

Case TWO:
Model: Simulator
Cell Phone Resolution: 800*480 (Note: Mobile phone two right-angled side of the 800 and 480 pixels respectively placed)
Size: 3.7 inch (Phone bevel size)

Calculation result: sqrt (800*800+480*480)/3.7≈252.15dpi
According to Google's Official document (Figure 1-1), the current mobile phone close to 240dpi, it is summarized in the HDPI phone range, that is, 1dp=1.5px.

The most mobile phones in the market can be divided into 5 pixel density classes, respectively:
LDPI:120DPI, the relationship between pixel density and DP conversion is: 1DP = 0.75px
MDPI:160DPI, the relationship between pixel density and DP conversion is: 1DP = 1px
HDPI:240DPI, the relationship between pixel density and DP conversion is: 1DP = 1.5px
XHDPI:320DPI, the relationship between pixel density and DP conversion is: 1DP = 2px
XXHDPI:480DPI, the relationship between pixel density and DP conversion is: 1DP = 3px

(Note: The following case is a screen fit test for the current two phones)

Adaptation method one: Picture adaptation
Different pixel density of the mobile phone loading project resource file (res) in different resource images, the above two mobile phones as an example. The layout code is as follows:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " ><ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:background= "@drawable /A" /></Relativelayout>

Adaptation Method II: Dimens.xml file adaptation
Dimens.xml exists in the project resource (res) folder under different values (such as: value-1280x720, value-800x480) folders, which can be used to specify the size of the control, Different pixel density mobile phone load the Dimens.xml file under different values folder, using the following methods:

<xmlns:android= "http://schemas.android.com/apk/res/android"xmlns:tools  = "Http://schemas.android.com/tools"android:layout_width= "Match_parent"  Android:layout_height= "Match_parent"android:orientation= "vertical"  Tools:context= ". Mainactivity ">

<!--different phones to load different DP--

< TextView Android:background = "#987654" Android:layout_width = "@dimen/width" Android:layout_height = "Wrap_content" Android:text = "@string/hello_world" /> </ LinearLayout >

Simulator (HDPI): Load dimens.xml resource file, located under res/value-800x480 folder

< Resources > <  name= "width">160dp</dimen></  Resources>


According to the above hdpi DP and px conversion relationship 1DP = 1.5px, then 160DP = 240px, the current control width should be in the middle of the screen position.

G700 (xhdpi): Load dimens.xml resource file, located under res/value-1280x720 folder

< Resources > <  name= "width">180dp</dimen></  Resources>


According to the above xhdpi DP and px conversion relationship 1DP = 2px, then 180DP = 360px, the current control width should be in the middle of the screen position.

Adaptation Method III: Layout file adaptation

Different resolution of the phone, loading different layout files have achieved the adaptation effect. Create multiple layouts (such as: layout-1280x720, layout-800x480) folders to hold the layout files required for different pixel density phones.

Emulator (HDPI): Loads the Activity_main.xml layout file, located under the res/layout-800x480 folder:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " ><TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "800*480 phone will go to load the layout file" /></Relativelayout>G700 (xhdpi): Loads the Activity_main.xml layout file, located under the res/layout-1280x720 folder:<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " ><TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "1280*720 phone will go to load the layout file" /></Relativelayout>

Formula Four: Java code adaptation

Use the Android API to get the width and height of the current phone, and proportionally assign the width of the controls on the screen to fit the effect. The core code is as follows:
Layout file:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " ><TextViewAndroid:id= "@+id/tv"Android:background= "#000000"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /></Relativelayout>

OnCreate core code in activity:

TextView TV =(TextView) Findviewbyid (r.id.tv);//gets the encapsulated current phone screen information object for storing the height valueDisplaymetrics metrics =Newdisplaymetrics ();//set the current screen to a wide heightGetwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics);//Get HeightConstant.srceenheight =Metrics.heightpixels;//Get widthConstant.srceenwidth =Metrics.widthpixels; LOG.I (Tag,"Constant.srceenheight =" +constant.srceenheight); LOG.I (Tag,"Constant.srceenwidth =" +constant.srceenwidth);//the width of the height of each account 50%Relativelayout.layoutparams Layoutparams =NewRelativelayout.layoutparams ((int) (constant.srceenwidth*0.5+0.5), (int) (constant.srceenheight*0.5+0.5) ); Tv.setlayoutparams (layoutparams);


Fit Way Five: Weight adaptation
With the remaining space allocated by Android (weight), the adaptation effect has been achieved. The display interface loads the layout file as follows:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal"Tools:context=". Mainactivity " ><TextViewAndroid:background= "#000000"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Match_parent"/><TextViewAndroid:background= "#123456"Android:layout_width= "0DP"Android:layout_weight= "1"Android:layout_height= "Match_parent"/></LinearLayout>

Android down screen fit < two >

Related Article

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.