您的位置:首页 > 移动开发 > Android开发

关于android的屏幕适配

2016-03-11 16:32 615 查看
前言:

之前查阅了一些文档,比如:
http://blog.csdn.net/zhaokaiqiang1992/article/details/45419023
虽然是很详细,但是感觉好复杂,所以只是略微了解了一些概念性的东西

之后又看到了这篇博客:
http://blog.csdn.net/lmj623565791/article/details/46767825
虽然是在Google的基础上做了很大改善,我也期望能参考这个库,但是再导入库时,始终无法解决,

无奈之下,我就选择了直接使用Google的API,其用法总结如下:

正文:

1>导入API所需的库(仅需以下一步)

在build.gradle(Module:app)的


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile project(':PhysicaloidLibrary')
    
}

[code]

添加一行:[code]compile 'com.android.support:percent:23.0.0'
添加后的结果为:
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile project(':PhysicaloidLibrary')
    compile 'com.android.support:percent:23.0.0'
}

[/code]
2>用法
示例如下:
[code]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.dai.app2.ExtrudeActivity"
    tools:showIn="@layout/activity_extrude">

    <!--
         PercentRelativeLayout:
            其比例是根据子布局的尺寸和margin来计算的
            控件的宽是依据子布局的宽,亦即水平方向的子布局的长度
            控件的高是依据子布局的高,亦即竖直方向的子布局的长度
            参考链接:
            https://developer.android.com/intl/zh-cn/reference/             android/support/percent/package-summary.html

            注:屏幕尺寸的宽高是根据屏幕分辨率的属性来的,不是
            通过测试程序得出来的值;
            例如:我通过程序测试出来的屏幕的宽高为1024*552
            而在供应商那边得到的数据是:1024*600
            所以在percent中,我应该选择600,而不是552;
    -->

    <android.support.percent.PercentRelativeLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent">

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_widthPercent="53.515625%"
            app:layout_heightPercent="21.666667%"
            android:id="@+id/btnZUpExtrude"
            android:background="@drawable/extrudezupdis"
            android:enabled="false"

            android:layout_centerHorizontal="true"
            app:layout_marginTopPercent="23.8%"
            />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_widthPercent="53.515625%"
            app:layout_heightPercent="21.666667%"
            android:id="@+id/btnZDownExtrude"
            android:background="@drawable/extrudezdowndis"
            android:enabled="false"

            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            app:layout_marginBottomPercent="23.8%"
            />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_widthPercent="20.3125%"
            app:layout_heightPercent="26.811594%"
            android:id="@+id/btnXCenterExtrude"
            android:background="@drawable/extrudexdis"

            android:layout_centerVertical="true"
            app:layout_marginLeftPercent="18.75%"

            />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_widthPercent="20.3125%"
            app:layout_heightPercent="26.811594%"
            android:id="@+id/btnYCenterExtrude"
            android:background="@drawable/extrudeydis"

            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            app:layout_marginRightPercent="18.554688%"

            />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_widthPercent="19.53125%"
            app:layout_heightPercent="16.66666667%"
            android:id="@+id/btnZZeroExtrude"
            android:background="@drawable/extrudez0dis"
            android:onClick="zZero"

            android:layout_centerInParent="true"
            />
    </android.support.percent.PercentRelativeLayout>

    <android.support.percent.PercentRelativeLayout
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent">

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_widthPercent="51.757812%"
            app:layout_heightPercent="20.66667%"
            android:id="@+id/btnReturnWireExtrude"
            android:background="@drawable/extrudereturnwire"

            android:layout_centerHorizontal="true"
            app:layout_marginTopPercent="24.3%"
            />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_widthPercent="51.757812%"
            app:layout_heightPercent="20.66667%"
            android:id="@+id/btnExtrudeExtrude"
            android:background="@drawable/extrude"

            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            app:layout_marginBottomPercent="24%"
            />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_widthPercent="20.3125%"
            app:layout_heightPercent="24.666667%"
            android:id="@+id/btnCoolDownExtrude"
            android:background="@drawable/extrudecooldown"

            android:layout_centerVertical="true"
            app:layout_marginLeftPercent="18.945312%"

            />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_widthPercent="20.3125%"
            app:layout_heightPercent="24.666667%"
            android:id="@+id/btnHeatUpExtrude"
            android:background="@drawable/extrudeheatingup"

            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            app:layout_marginRightPercent="18.945312%"

            />

        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_widthPercent="17.578125%"
            app:layout_heightPercent="15%"
            android:id="@+id/btnTemperatureExtrude"
            android:background="@drawable/extrudetemperature"
            android:text="Temperature"
            android:textColor="@android:color/white"

            android:layout_centerInParent="true"
            />

    </android.support.percent.PercentRelativeLayout>

</LinearLayout>

[/code]
其效果图如下:
这样,当我换一种机型的时候,其会根据屏幕的大小而自动调整;若我在上述代码中,控件的宽高直接用具体的dp数值指定,
那么,当换一种尺寸的屏幕时,其效果图就会变形,用 % 就很好的解决了这个问题;


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: