【Android】Percent Support Library实践

来源:互联网 发布:免费的crm软件 编辑:程序博客网 时间:2024/06/06 00:32
Percent Support Library 用于使用百分比控制子View在布局里面占用的大小,相比于layout_weight这个属性具有更高的灵活性,并且margin属性也支持使用百分比控制,这是layout_weight不具备的,官方只提供了PercentFrameLayout和PercentRelativeLayout。目前支持的属性有:
layout_widthPercentlayout_heightPercentlayout_marginPercentlayout_marginLeftPercentlayout_marginTopPercentlayout_marginRightPercentlayout_marginBottomPercentlayout_marginStartPercentlayout_marginEndPercentlayout_aspectRatio     宽高比例
bulid.gradle引用
compile 'com.android.support:percent:26.1.0'
布局使用
<?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:id="@+id/content"    android:layout_width="match_parent"    android:layout_height="match_parent">    <View        android:id="@+id/view1"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:layout_alignParentTop="true"        android:background="#333366"        app:layout_heightPercent="10%"        app:layout_widthPercent="25%"/>    <View        android:id="@+id/view2"        android:layout_toRightOf="@id/view1"        app:layout_widthPercent="25%"        app:layout_heightPercent="10%"        android:background="#999933"/>    <View        android:id="@+id/view3"        android:layout_toRightOf="@id/view2"        app:layout_widthPercent="25%"        app:layout_heightPercent="10%"        android:background="#996600"/>    <View        android:id="@+id/view4"        android:layout_toRightOf="@id/view3"        app:layout_widthPercent="25%"        app:layout_heightPercent="10%"        android:background="#333333"/>    <View        android:id="@+id/view5"        app:layout_aspectRatio="578%"        app:layout_widthPercent="100%"        app:layout_marginTopPercent="10%"        android:background="#669999" />        <View        android:id="@+id/view6"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:layout_alignParentBottom="true"        app:layout_widthPercent="50%"        app:layout_heightPercent="10%"        android:background="#669999" />    <View        android:id="@+id/view7"        android:layout_alignParentBottom="true"        android:layout_toRightOf="@id/view6"        app:layout_widthPercent="25%"        app:layout_heightPercent="10%"        android:background="#999933"/></android.support.percent.PercentRelativeLayout>
效果图:

在Android中这种百分比布局已经被ConstraintLayout所替代。



原创粉丝点击