Android 百分比布局库(percent-support-lib) 解析与扩展

在安卓5.0后,谷歌最新的百分比布局库(percent-support-lib)。事实上LinearLayout的layout_weight也能实现百分比效果,只是这个更直接

1.简单介绍

这个库提供了:两种布局供大家使用: 

PercentRelativeLayoutPercentFrameLayout,通过名字就能够看出,这是继承自FrameLayoutRelativeLayout两个容器类。

支持的属性有:

layout_widthPercentlayout_heightPercent、 
layout_marginPercentlayout_marginLeftPercent、 
layout_marginTopPercentlayout_marginRightPercent、 
layout_marginBottomPercentlayout_marginStartPercentlayout_marginEndPercent

能够看到支持宽高,以及margin。

也就是说。大家仅仅要在开发过程中使用PercentRelativeLayoutPercentFrameLayout替换FrameLayoutRelativeLayout就可以。

2.用法

下载地址:https://github.com/JulienGenoud/android-percent-support-lib-sample

将下载的类库拷到项目的对应位置

在build.gradle加入:

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

<h4 id="一percentframelayout" style="margin: 0.8em 0px; padding: 0px; box-sizing: border-box; font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px;"><span style="color:#ff0000;">(一)PercentFrameLayout</span></h4>
<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">

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="left|top"
        android:background="#44ff0000"
        android:text="30%,height:20%"
        app:layout_heightPercent="20%"
        android:gravity="center"
        app:layout_widthPercent="30%"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="right|top"
        android:gravity="center"
        android:background="#4400ff00"
        android:text="70%,height:20%"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="70%"/>


    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="bottom"
        android:background="#770000ff"
        android:text="100%,height:10%"
        android:gravity="center"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="100%"/>


</android.support.percent.PercentFrameLayout>

(二) PercentRelativeLayout

<span style="color:#333333;"><?xml version="1.0" encoding="utf-8"?

> <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" android:clickable="true"> <TextView android:id="@+id/row_one_item_one" android:layout_width="0dp" android:layout_height="0dp" android:layout_alignParentTop="true" android:background="#7700ff00" android:text="w:70%,h:20%" android:gravity="center" app:layout_heightPercent="20%" app:layout_widthPercent="70%"/> <TextView android:id="@+id/row_one_item_two" android:layout_width="0dp" android:layout_height="0dp" android:layout_toRightOf="@+id/row_one_item_one" android:background="#396190" android:text="w:30%,h:20%" app:layout_heightPercent="20%" android:gravity="center" app:layout_widthPercent="30%"/> <ImageView android:id="@+id/row_two_item_one" android:layout_width="match_parent" android:layout_height="0dp" android:src="@drawable/tangyan" android:scaleType="centerCrop" android:layout_below="@+id/row_one_item_one" android:background="#d89695" app:layout_heightPercent="70%"/> <TextView android:layout_width="0dp" android:layout_height="0dp" android:layout_below="@id/row_two_item_one" android:background="#770000ff" android:gravity="center" android:text="100%,height:10%" app:layout_heightPercent="10%" app:layout_widthPercent="100%"/> </android.support.percent.PercentRelativeLayout></span>





原文地址:https://www.cnblogs.com/jzssuanfa/p/7285862.html