Android百分比布局

基本介绍

Android提供了Android-percent-support这个库,支持百分比布局,在一定程度上可以解决屏幕适配的问题

他提供了:

  • 两种布局:
    PercentRelativeLayout和PercentFrameLayout
    PercentRelativeLayout继承RelativeLayout
    PercentFrameLayout继承FrameLayout
    对于线性布局,可以用权重来解决,也可以用大神写的PercentLinearLayout来代替

  • 支持的属性:
    layout_widthPercent
    layout_heightPercent
    layout_marginPercent
    layout_marginLeftPercent
    layout_marginTopPercent
    layout_marginRightPercent
    layout_marginBottomPercent
    layout_marginStartPercent
    layout_marginEndPercent

基本使用

github上,android-percent-support-lib-sample

build.gradle添加:

compile 'com.android.support:percent:25.2.0'

PercentFrameLayout



    
    
    
    
    


在480*800上的显示效果

a.png

在720*1280上的显示效果

b.png

PercentRelativeLayout和PercentLinearLayout

PercentLinearLayout.java

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);
        }

    }

}

布局文件




    

        


        

        

    

    

    



在480*800上的显示效果:

c.png

在720*1280上的显示效果:

d.png

你可能感兴趣的:(Android百分比布局)