Android_AutoLayout全新适配方式原理分析

版权声明:本文为原创文章,未经博主允许不得转载. https://blog.csdn.net/strawberry2013/article/details/51123528

1.前言

具体过程,参考文章http://blog.csdn.net/lmj623565791/article/details/49990941


2.实现步骤


(1).初始化

AndroidManifest.xml设置design_width和design_height

AutoLayoutConifg.getInstance().useDeviceSize().init(this);//初始化屏幕信息,得到屏幕宽高和设计宽高


(2).布局文件

//以下布局为例,即可实现自动布局
<com.zhy.autolayout.AutoRelativeLayout style="@style/immersion_status"
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical">

    <TextView
        android:layout_width="920px"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20px"
        android:gravity="left"
        android:text="title高度130\nmarginTop=1658\n自身高度=132\n130+1658+132 = 1920\n用于测试底部操作栏是否影响UI。"
        android:textSize="49px"/>

    <!--title高度130,marginTop=1658,自身高度=132; 130+1658+132 = 1920;-->
    <TextView
        android:layout_width="920px"
        android:layout_height="132px"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="1658px"
        android:background="#76ff5a"
        android:clickable="true"
        android:gravity="center"
        android:text="完成注册"
        android:textColor="@android:color/white"
        android:textSize="49px"/>
</com.zhy.autolayout.AutoRelativeLayout>

即可根据上面简单步骤,即可实现自动适配布局。特殊的ListView可以参照库Demo说明


3.原理分析

//以AutoRelativeLayout源码为例
public class AutoRelativeLayout extends RelativeLayout
{
    private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);
    //...
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        if (!isInEditMode())
            mHelper.adjustChildren();//调整属性值,根据不同设备类型的比例值
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    //...
}
public class AutoLayoutHelper {
    private final ViewGroup mHost;

    public AutoLayoutHelper(ViewGroup host) {
        mHost = host;
        if (mAutoLayoutConifg == null) {
            initAutoLayoutConfig(host);
        }
    }
    ////调整属性值,根据不同设备类型的比例值
    public void adjustChildren() {
        AutoLayoutConifg.getInstance().checkParams();

        for (int i = 0, n = mHost.getChildCount(); i < n; i++) {
            View view = mHost.getChildAt(i);
            ViewGroup.LayoutParams params = view.getLayoutParams();

            if (params instanceof AutoLayoutParams) {
                //遍历子控件的大小相关属性(字号、内外边距、宽高等)
                AutoLayoutInfo info = ((AutoLayoutParams) params).getAutoLayoutInfo();
                if (info != null) {
                    //根据设备类型设置属性
                    info.fillAttrs(view);
                }
            }
        }

    }
}
//以调整字号大小为例
public abstract class AutoAttr {
    public void apply(View view) {
        int val;
        if (useDefault()) {
            val = defaultBaseWidth() ? getPercentWidthSize() : getPercentHeightSize();
        	//调整字号大小,getPercentHeightSize()将被执行,执行AutoUtils.getPercentHeightSizeBigger(pxVal)方法
        } else {
            //val = ...
        }

        if (val > 0)
            val = Math.max(val, 1);//val值将被设置
        execute(view, val);//根据属性,设置属性值
    }

    protected int getPercentWidthSize() {
        return AutoUtils.getPercentWidthSizeBigger(pxVal);
    }

    protected int getPercentHeightSize() {
        return AutoUtils.getPercentHeightSizeBigger(pxVal);
    }
}
public class AutoUtils {
    public static int getPercentWidthSizeBigger(int val) {
        int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
        int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();

        int res = val * screenWidth;
        if (res % designWidth == 0) {
            return res / designWidth;
        } else {
            return res / designWidth + 1;
        }
    }
}
阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页