Android日记安卓控件系列我们就爱程序媛

Android百分比布局

2017-03-04  本文已影响7202人  蓝枫zeke

基本介绍

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

他提供了:

基本使用

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

build.gradle添加:

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

PercentFrameLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
    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_heightPercent="20%"
        app:layout_widthPercent="50%"
        android:layout_gravity="center"
        android:background="@mipmap/picture"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_gravity="center"
        android:text="孩子"
        android:gravity="center"/>
    
</android.support.percent.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);
        }

    }

}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<percentlib.zhoujian.com.percentlib.PercentLinearLayout
    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"
    android:orientation="vertical">

    <android.support.percent.PercentRelativeLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#2C9A2D"
        app:layout_heightPercent="9%"
        app:layout_widthPercent="100%">

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"

            app:layout_marginLeftPercent="5%"
            android:layout_marginLeft="20dp"
            android:background="@mipmap/itemback"
            app:layout_heightPercent="60%"
            app:layout_widthPercent="8%"/>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="我是标题"
            android:textSize="18sp"/>

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            app:layout_marginRightPercent="5%"
            android:background="@mipmap/share"
            app:layout_heightPercent="60%"
            app:layout_widthPercent="8%"/>

    </android.support.percent.PercentRelativeLayout>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@mipmap/picture"
        app:layout_heightPercent="30%"
        app:layout_widthPercent="50%"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="孩子"
        android:textSize="16sp"/>


</percentlib.zhoujian.com.percentlib.PercentLinearLayout>

在480*800上的显示效果:

c.png

在720*1280上的显示效果:

d.png
上一篇 下一篇

猜你喜欢

热点阅读