Android5.X之PercentLayout

引入依赖库

compile 'com.android.support:percent:24.x.x'

主要类

  • PercentRelativeLayout(继承自RelativeLayout)
  • PercentFrameLayout(继承自FrameLayout )

属性

  • layout_widthPercent 设置控件宽度为父容器的宽的百分比
  • layout_heightPercent 设置控件高度为父容器的高的百分比
  • layout_marginPercent 设置控件外边距为父容器的宽度的百分比
  • layout_marginLeftPercent 设置控件与左边控件的距离为父容器的宽度的百分比
  • layout_marginTopPercent 设置控件与上方控件的距离为父容器的高度的百分比
  • layout_marginRightPercent 设置控件与右边控件的距离为父容器的宽度的百分比
  • layout_marginBottomPercent 设置控件与下方控件的距离为父容器的高度的百分比
  • layout_marginStartPercent
  • layout_marginEndPercent

原来用某些具体单位(如dp)的设置现在都可以用百分比的方式进行设置了,例如设置控件的宽度layout_width原来我们是这样玩的android:layout_width="match_parent"现在用了百分比的属性之后呢,可以这样玩了app:layout_widthPercent="50%",这里的百分比是相对于父容器而言的。

具体使用

xml文件加入命名空间

xmlns:app="http://schemas.android.com/apk/res-auto"
<android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>
 
     <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        app:layout_marginTopPercent="25%"
        app:layout_marginLeftPercent="25%"/>
 
 </android.support.percent.PercentRelativeLayout/>

自定义PecentLinearLayout

库中并没有提供PecentLinearLayout,想玩PecentLinearLayout就需要自定义了

public class PercentLinearLayout extends LinearLayout
{

    private PercentLayoutHelper mPercentLayoutHelper;

    public PercentLinearLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mPercentLayoutHelper = new PercentLayoutHelper(this);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        mPercentLayoutHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mPercentLayoutHelper.handleMeasuredStateTooSmall())
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);
        mPercentLayoutHelper.restoreOriginalParams();
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs)
    {
        return new LayoutParams(getContext(), attrs);
    }

    public static class LayoutParams extends LinearLayout.LayoutParams
            implements PercentLayoutHelper.PercentLayoutParams
    {
        private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;

        public LayoutParams(Context c, AttributeSet attrs)
        {
            super(c, attrs);
            mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs);
        }

        @Override
        public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo()
        {
            return mPercentLayoutInfo;
        }

        @Override
        protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr)
        {
            PercentLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }
    }
}

这样我们就有了PecentLinearLayout,原来的LinearLayout的现在都可以用百分比的方式进行设置了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,796评论 1 92
  • 翻译自“Auto Layout Guide”。 2 自动布局细则手册 2.1 堆栈视图 接下来的章节展示了如何使用...
    lakerszhy阅读 1,895评论 3 9
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,523评论 0 17
  • 中午,女儿睡着了。我睡不着,翻来翻去又怕影响孩子睡觉,索性去找老公聊天。 老公在平板上看电视剧吧。 “我们要不要选...
    阿布_771f阅读 198评论 0 0
  • 一直想看书,可是当书买回来后,放在桌子上,放在书架上,很长时间都不会翻开看。想学摄影,攒了三个月的工资,买了一台单...
    莫闲2016阅读 367评论 0 2