Android screen adaptation

Source: Internet
Author: User

Android screen adaptation

      • Android screen adaptation
        • Adaptation method a picture adaptation
        • Adaptation mode two Dimensxml file adaptation
        • Adaptation method three layout file adaptation
        • Adaptation mode four Java code adaptation
        • Adaptation Method five weights fit

fit : The current app shows the same effect on the same phone. Before the adaptation needs to first determine the current cell phone pixel density type (such as: xhdpi, hdpi, MDPI, etc.), the following has been Huawei G700, simulator, for example, calculate its pixel density.

Case one:

手机型号:G700手机分辨率:1280*720 (注:手机两个直角边上分别放置了1280及720个像素点)手机尺寸大小:5英寸(手机斜边长度)假设a,b分别为两个直角边,c为斜边,由勾股定理可得出计算方式:sqrt(a*a+b*b)/c计算结果:sqrt(1280*1280+720*720)/5 ≈ 293.72dpi根据google官方文档说明得出,当前手机最接近320dpi,则将其归纳在xhdpi手机范围内,即1dp=2px;

Case TWO:

手机型号:模拟器手机分辨率:800*480(注:手机两个直角边上分别放置了800及480个像素点)手机尺寸大小:3.7英寸(手机斜边大小)计算结果:sqrt(800*800+480*480)/3.7 ≈ 252.15dpi根据google官方文档(图1-1)得出,当前手机接近240dpi,则将其归纳在hdpi手机范围内,即1dp=1.5px。参照以上方式可将市场上大多数手机划分为5个像素密度等级,分别为:ldpi:120dpi,像素密度与dp转换关系为:1dp = 0.75pxmdpi:160dpi ,像素密度与dp转换关系为:1dp = 1pxhdpi:240dpi,像素密度与dp转换关系为:1dp = 1.5pxxhdpi:320dpi,像素密度与dp转换关系为:1dp = 2pxxxhdpi:480dpi,像素密度与dp转换关系为: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:

<RelativeLayout 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=".MainActivity" >    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/a" /></RelativeLayout>

G700 (XHDPI): Loads the a.jpg resource file, which is located under the Res/drawable-xhdpi folder and displays the following effects:

Emulator (HDPI): Loads the a.jpg resource file, located under the Res/drawable-hdpi folder, and displays the following effects:

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:

<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"    android:orientation="vertical"    tools:context=".MainActivity" >    <!-- 不同的手机加载不同的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>    <dimen name="width">160dp</dimen></resources>根据上述hdpi dp和px的转换关系1dp = 1.5px,则160dp = 240px,当前控件宽度应该位于屏幕中间位置。

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

<resources>    <dimen name="width">180dp</dimen></resources>根据上述xhdpi dp和px的转换关系1dp = 2px,则180dp = 360px,当前控件宽度应该位于屏幕中间位置。

G700 (XHDPI) shows the following results:

The simulator (hdpi) displays the following effects:

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:

    <RelativeLayout 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=".MainActivity" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="800*480手机会去加载的布局文件" />    </RelativeLayout>

G700 (XHDPI): Loads the Activity_main.xml layout file, located under the res/layout-1280x720 folder:

    <RelativeLayout 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=".MainActivity" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="1280*720手机会去加载的布局文件" />    </RelativeLayout>

G700 (XHDPI) shows the following results:

The simulator (hdpi) displays the following effects:

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 <relativelayout 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 =". Mainactivity "> <textview android:id=" @+id/tv "android:background=" #000000 "Android:layout_w Idth= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/hello_world"/></Relati    Velayout>activity oncreate Core code: TextView TV = (TextView) Findviewbyid (r.id.tv);    Gets the encapsulated current phone screen information object for storing a wide height value displaymetrics metrics = new Displaymetrics ();    Set the width height Getwindowmanager () to the current screen. Getdefaultdisplay (). Getmetrics (metrics);    Get height constant.srceenheight = metrics.heightpixels;    Get width constant.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 = new Relativelayout.layoutparams ((int) (constant.srceenwidth*0.5+0.5), (int) (Co    nstant.srceenheight*0.5+0.5)); Tv.setlayoutparams (Layoutparams);

G700 (XHDPI) shows the following results:

The simulator (hdpi) displays the following effects:

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:

<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"    android:orientation="horizontal"    tools:context=".MainActivity" >    <TextView         android:background="#000000"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="match_parent"/>    <TextView         android:background="#123456"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="match_parent"/></LinearLayout>

G700 (XHDPI) shows the following results:

The simulator (hdpi) displays the following effects:

Android Screen fit

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.