天天看點

螢幕适配--安卓app

給大家介紹個第三方架構autolayout,它的作用就是實作螢幕适配,我初次接觸也是朋友介紹下使用,再次感謝朋友的分享!

話不多說,

第一步:

首先studio依賴compile 'com.zhy:autolayout:1.4.3'

配置:

預設使用的高度是裝置的可用高度,也就是不包括狀态欄和底部的操作欄的,如果你希望拿裝置的實體高度進行百分比化:

可以在Application的onCreate方法中進行設定:

public class UseDeviceSizeApplication extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        AutoLayoutConifg.getInstance().useDeviceSize();
    }
}
           

第二步:

在AndroidManifest.xml檔案下添加設計尺寸

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.shipei.ui" >

    <application

        android:name="com.shipei.application.UseDeviceSizeApplication"

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:supportsRtl="true"

        android:theme="@style/AppTheme" >

        //設計稿尺寸

        <meta-data android:name="design_width" android:value="1200">

        </meta-data>

        <meta-data android:name="design_height" android:value="1920">

        </meta-data>

        <activity android:name=".MainActivity" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>

第三步:

讓你的Activity繼承自

AutoLayoutActivity

.

非常簡單的兩個步驟,你就可以開始愉快的編寫布局了,詳細可以參考sample。

其他用法

如果你不希望繼承

AutoLayoutActivity

,可以在編寫布局檔案時,将

  • LinearLayout -> AutoLinearLayout
  • RelativeLayout -> AutoRelativeLayout
  • FrameLayout -> AutoFrameLayout

這樣也可以完成适配

第四步:

将布局機關dp/dip改為px,朋友說dp/dip不可以用,我也沒試過,

大家也可以參考下這篇文章

http://www.open-open.com/lib/view/open1456840121843.html

項目位址:(http://download.csdn.net/detail/zhangzibin1992/9758527)

繼續閱讀