Introduction to the screen fitting method

Source: Internet
Author: User


1. Picture adaptation (load images from different folders according to the pixel density of the phone screen)
Pixel density of your phone's screen: The number of pixels that are contained in an inch
For example: pixel density of 480x800 = 480^2+800^2 root = 932.95/4 = 233.23dpi
In the development of the general does not place a set of pictures in each folder, usually to determine a resolution to make a set of diagrams, and then follow the Android development rules, so that Android automatically adapt to other resolution, if there is no picture adaptation, Then make the corresponding picture to the different resolution of the corresponding directory for picture adaptation
2.dimens.xml file Adaptation (load Dimens.xml in different folders based on the pixel density of the phone screen)
Values--Dimens.xml
It is mainly used for fitting, to fit the fixed width height control in the app, also can set the fixed distance operation
Res-> values-1280x720-Dimens.xml
<resources>
<dimen name= "Viewpagerheight" >200dp</dimen>
</resources>
Layout files Use
<com.itheima.zhbj98.view.roolviewpager
Android:id= "@+id/menunewsitempager_vp_viewpager"
Android:layout_width= "Match_parent"
android:layout_height= "@dimen/viewpagerheight"
></com.itheima.zhbj98.view.RoolViewPager>
3.layout layout file adaptation (load layout files in different layout folders based on the pixel density of the phone screen)
Load layout files in different layout folders based on the pixel density of your phone's screen
Layout files, layout-1280x720, RES----consistent names
4. Code adaptation
1. Proportional adaptation
Sets the width of the scale displayed at different resolutions, depending on the scale (width is not the same in each resolution, but occupies the same scale)
First request 200 in 320 resolution of the proportion, and then to the 1280x720 resolution to find the corresponding proportion of the width of 720 occupied
Figure out the ratio of 200 to 320, then the width in 720.
200/320 * Width of the phone interface
WindowManager WindowManager = (windowmanager) getsystemservice (Window_service);
int width = Windowmanager.getdefaultdisplay (). GetWidth ();
Slidingmenu.setbehindoffset (* width/320);//Unit is PX
2. DP and PX Mutual conversion
Creating a tool class
public class Densityutil {
/**
* Switch from Dip unit to PX (pixel) According to the resolution of the phone
*/
public static int dip2px (context context, float Dpvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density; Get the density of your phone's screen
return (int) (Dpvalue * scale + 0.5f);
}
/**
* According to the resolution of the phone from PX (pixel) to the unit to become DP
*/
public static int Px2dip (context context, float Pxvalue) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return (int) (Pxvalue/scale + 0.5f);
}
}
5. Weight Matching
<linearlayout 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"
tools:context= "${relativepackage}.${activityclass}"
android:orientation= "Horizontal"
Android:weightsum= "10"
>
<!--weightSum: Split the control into several--
<textview
Android:layout_width= "0DP"
android:layout_height= "Wrap_content"
android:layout_weight= "3"
android:background= "#FF0000"/>
<textview
Android:layout_width= "0DP"
android:layout_weight= "7"
android:layout_height= "Wrap_content"
android:background= "#00FF00"/>
</LinearLayout>

Introduction to the screen fitting method

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.