Android界面百分比适配-PercentRelativeLayout

来源:互联网 发布:全球电视直播软件apk 编辑:程序博客网 时间:2024/05/17 22:03

 在使用PercentRelativeLayout前我们需要现在build.gradle里添加上

compile 'com.android.support:percent:22.2.0'
来添加支持库。

然后实现main.xml:

<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:orientation="vertical"><TextViewandroid:background="#ffffeeff"android:text="1"android:id="@+id/a"        android:gravity="center"app:layout_heightPercent="10%"app:layout_widthPercent="100%"app:layout_marginBottomPercent="5%"/>    <TextViewandroid:background="#ffbbff00"android:text="2"android:id="@+id/b"android:gravity="center"android:layout_below="@+id/a"        app:layout_heightPercent="10%"app:layout_widthPercent="10%"app:layout_marginBottomPercent="5%"/><TextViewandroid:background="#ff0ff0ff"android:text="3"android:id="@+id/c"android:gravity="center"android:layout_below="@+id/b"        app:layout_heightPercent="10%"app:layout_widthPercent="20%" app:layout_marginBottomPercent="10%"/><TextViewandroid:background="#fffdd000"android:text="4"android:id="@+id/d"android:gravity="center"android:layout_below="@+id/c"app:layout_heightPercent="10%"app:layout_widthPercent="30%"app:layout_marginBottomPercent="20%"/><TextViewandroid:background="#ff0000ff"android:text="5"android:gravity="center"android:layout_below="@+id/d"app:layout_heightPercent="10%"app:layout_widthPercent="40%"/></android.support.percent.PercentRelativeLayout>
使用android:layout_below="@+id/d"这类语句控制某两个控件间的位置关系。

app:layout_heightPercent="10%"app:layout_widthPercent="30%"app:layout_marginBottomPercent="20%"

这三句分别控制控件宽、长和两控件间隔占手机屏幕的百分比。

效果图: