简述:
PercentFrameLyout——是android团队新开发的一种布局方式,其特点是百分比方式控制控件的大小。
PercentFrameLyout另外继承FrameLyout特性,所有控件都默认摆放在布局的左上角。
元素属性:
LinearLayout 和 PercentFrameLyout 对比:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
app:layout_heightPercent="50%"
app:layout_weightPercent="50%"/>
PercentFrameLyout 主要使用app:layout_heightPercent 和 app:layout_weightPercent 设置控件大小,当然也可以内置的使用android:layout_width跟android:layout_height。
如何使用PercentFrameLyout:
使用PercentFrameLyout很简单,只需要在app/build.gradle文件,在dependencise闭包中添加依赖,如下内容:
dependencies {
compile 'com.android.support:percent:25.3.0' //只需添加依赖
}
需要注意的是SDK版本问题 ,另外奉上PercentFrameLyout的Github开源:点击打开链接。
比如现在最新版本需要↓↓↓
示范代码:
<?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">
<TextView
android:id="@+id/button"
android:gravity="center"
android:layout_gravity="left|top"
android:background="#0f0"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="左上角" />
<TextView
android:id="@+id/button2"
android:gravity="center"
android:background="#00f"
android:layout_gravity="right|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="右上角" />
<TextView
android:id="@+id/button3"
android:gravity="center"
android:background="#f00"
android:layout_gravity="left|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="左下角"/>
<TextView
android:id="@+id/button4"
android:gravity="center"
android:background="#ff0"
android:layout_gravity="right|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="右下角"/>
</android.support.percent.PercentFrameLayout>
总结:
可以看出我们使用了PercentFrameLyout不是内置系统的SDK当中,需要完整的包名路径写出,另外还必须定义app的命名空间,才可以使用PercentFrameLyout布局元素:
android.support.percent.PercentFrameLayout
xmlns:app="http://schemas.android.com/apk/res-auto"