Android multi-screen adaptation: Part 1: android adaptation

Source: Internet
Author: User

Android multi-screen adaptation: Part 1: android adaptation

1. In the xml layout file, the width and height of the control are dp, And the font size is sp.

2. dynamically adapt to the screen width and height to obtain the screen width and height:

Method 1:

/*** Screen width * screen height * @ return */public void initPhone1 (Activity activity) {int phone_Width = activity. getWindowManager (). getdefadisplay display (). getWidth (); // unit: px int phone_Height = activity. getWindowManager (). getdefadisplay display (). getHeight (); // unit: px}

The method getWidth () from the type Display is deprecated

This method is outdated. The second method is recommended for all requests:

Method 2:

DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);int  Phone_width  = dm.widthPixels ;
int Phone_height = dm.heightPixels ;

 

Note: 1. the width and height obtained in Java code are measured in pixels. It is different from dp in xml files.

2. After the test, the width and height of the mobile phone screen are obtained in two ways, and the result is the same.

My mobile phone is Xiaomi 1,480x854 px

 

3. Normally, there are two ways to set the size of controls in a layout file.

One is to set in xml. If a control is defined in xml, the width and height of the control are dp, And the font size is sp.

The other is dynamic settings in java code.

TextView tv2 = (TextView) findViewById (R. id. tv2); LinearLayout. layoutParams params2 = (LayoutParams) tv2.getLayoutParams (); params2.width = 300; // here 300 stands for 300 px params2.height = 100; // here 100 stands for 100 px tv2.setLayoutParams (params2 );


4. in xml layout, the unit is dp, and in java code, the unit is px.

To keep the two sizes consistent, You need to convert them.

DensityUtil class

Package com. example. bb; import android. content. context; public class DensityUtil {/*** convert from the unit of dp to px (pixel) */public static int dip2px (Context context, float dpValue) based on the resolution of the mobile phone) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f);}/*** based on the resolution of the phone) to dp */public static int px2dip (Context context, float pxValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (pxValue/scale + 0.5f );}}

 

4. The screen size of android is large and the resolution is varied.

To accurately obtain the screen height and width, add the supports-screens node to AndroidManifest. xml.

? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. app01 "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 8 "android: targetSdkVersion =" 19 "/> <! -- Get the correct width and height of the phone --> <supports-screens android: anyDensity = "true" android: largeScreens = "true" android: normalScreens = "true" android: resizeable = "true" android: smallScreens = "true"/> <application android: allowBackup = "true" android: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name" android: theme = "@ style/AppTheme"> <activity android: name = "com. app01.MainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> </manifest>

 

 

 

 

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.