天天看点

屏幕适配--安卓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)

继续阅读