Android 百分比布局讲解

1 前沿

android 推出了百分比布局,有了这个布局我们按照百分比来分配屏幕。大家是不是听了很激动?下面我们来慢慢讲解。

2 控件介绍

2.1 控件介绍

我们来看一下这几个百分比控件:

1. PercentRelativeLayout
2. PercentFrameLayout.

它们分别继承了FrameLayout和RelativeLayout两个容器类。大家可能奇怪为什么没有PercentLinearLayout控件呢?这个android确实没给出,但是我们可以自己做一个嘛!

2.2 支持的属性

layout_widthPercent、
layout_heightPercent、 
layout_marginPercent、
layout_marginLeftPercent、 
layout_marginTopPercent、
layout_marginRightPercent、 
layout_marginBottomPercent、
layout_marginStartPercent、
layout_marginEndPercent

上面这些是百分比宽度和margin属性,具体使用请继续看。

3 使用步骤

1. 在build.gradle添加:

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

2. 使用控件

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
    <Button
        android:id="@+id/button1"
        android:text="左按钮"
        android:background="#FBAFB0"
        android:layout_alignParentLeft="true"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="100%"
        app:layout_widthPercent="70%"/>
         />
    <Button
        android:id="@+id/button2"
        android:text="右按钮"
        android:background="#A5F4AF"
        android:layout_alignParentRight="true"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="100%"
        app:layout_widthPercent="30%"
        />
</android.support.percent.PercentRelativeLayout>

3. 效果图
这里写图片描述

是不是很简单啊。下面我们再自己做一个PercentLinearLayout.

4 PercentLinearLayout代码


import android.content.Context;
import android.content.res.TypedArray;
import android.support.percent.PercentLayoutHelper;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * Created by Yyd on 2017-01-09.
 */
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);
        }

    }

}

5 结尾

注:本文参照鸿洋大神写的文章,因源码我还没有研究明白,所以只记录如何使用。等我研究明白再补充。

6 参考文档

1.Android 百分比布局库(percent-support-lib) 解析与扩展
2.PercentRelativeLayout 源码解析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序编织梦想

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值