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

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

推荐阅读更多精彩内容

  • Google开始支持百分比的方式布局了已经大半年了吧(7个月??)https://github.com/Julie...
    CoderBigBear阅读 2,584评论 2 2
  • 一概述安卓开发过程中一直比较无赖适配的问题,感到比较迷茫,一般的手机开发布局只是做到简单的适配,有的时候我们觉得网...
    雾里看花六月天阅读 785评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,595评论 25 708
  • 本文贴出顺序表、链表 的例子代码,以及实例通讯录的代码。文中代码均已在VS2015上测试,空指针均为nullptr...
    静候那一米阳光阅读 384评论 0 0
  • 今天8月7日,距离你离开还有六天。 今天8月8日,距离你离开还有五天。 今天8月9日,你说你19号才离开,又多出了...
    Apologize_阅读 83评论 0 0