转载请注明出处amoscxy的博客:https://mp.youkuaiyun.com/mdeditor/80149395
基本布局之 - PercentFrameLayout/PercentRelativeLayout
百分比布局
1.1 PercentFrameLayout
前面介绍的3中布局都是从Android1.0版本开始支持了,一直沿用到现在,可以说满足了绝大多数场景的界面设计需求,不过LinearLayout支持使用layout_weight属性来实现按比例指定控件大小的功能,其它布局都不支持,比如说,如果想用RelativeLayout来实现让两个按钮平分布局宽度的效果,就必须使用Android引入的一种全新的布局方式来解决此问题—百分比布局,在这种布局中我们可以不再使用wrap_content/match_parent等方式来指定控件的大小,而是允许直接指定控件在布局中所占的百分比,这样的话就可以轻松实现平分布局甚至任意比例分割布局的效果了
由于LinearLayout本身已经支持按比例指定控件的大小了,因此百分比布局只为FrameLayout和RelativeLayout进行了功能扩展,提供了PercentFrameLayout和PercentRelativeLayout这两个全新的布局
百分比布局是新增布局,那么怎样才能做到让新增布局在所有Android版本上都能使用呢,为此,Android团队将百分比布局定义在support库当中,我们只需要在项目的build.gradle中添加百分比布局的依赖,就能保证百分比布局在Android所有系统版本上的兼容性
最终效果
实例目录
在app/build.gradle文件的dependencies闭包中添加上百分比依赖
dependencies{
...
compile 'com.android.support:percent:24.2.1'
...
}
- activity_main.xml
<?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">
<Button
android:id="@+id/button1"
android:layout_gravity="left|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="Button 1"
/>
<Button
android:id="@+id/button2"
android:layout_gravity="right|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="Button 2"
/>
<Button
android:id="@+id/button3"
android:layout_gravity="left|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="Button 3"
/>
<Button
android:id="@+id/button4"
android:layout_gravity="right|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:text="Button 4"
/>
</android.support.percent.PercentFrameLayout>
最外层我们使用PercentFrameLayout,由于百分比布局并不是内置在系统SDK中的,所有需要把完整的包路径写出来,探后必须定义一个app的命名空间,这样才能使用百分比布局的自定义属性
在PercentFrameLayout中我们定义了4个按钮,使用app:layout_widthPercent属性将各按钮的宽度指定为布局的50%,使用app:layout_heightPercent属性将各按钮的高度指定为布局的50%,这里之所以能使用app前缀的属性就是因为刚才定义了app的命名空间,当然我们一直使用android前缀属性也是同样的道理
不过PercentFrameLayout还是会继承FrameLayout的特性,即所有的控件默认都是摆放在布局的左上角,那个为了让这4个按钮不会重叠,这里还是借助了layout_gravity来分别将这4个按钮放置在布局的左上、右下、左下、右下4个位置
- 最终效果
可以看到,每一个按钮的宽和高都占据了布局的50%,这样我们就轻松实现了4个按钮平分屏幕的效果
1.2 PercentRelativeLayout
PercentRelativeLayout的用法也是非常相似的,它继承了RelativeLayout中的所有属性,并可以使用app:layout_widthPercent和app:layout_heightPercent来按百分比指定控件的宽高
最终效果
修改activity_main.xml
<?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">
<Button
android:id="@+id/button1"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button 1"
/>
<Button
android:id="@+id/button2"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button 2"
/>
<Button
android:id="@+id/button3"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button 3"
/>
<Button
android:id="@+id/button4"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button 4"/>
</android.support.percent.PercentRelativeLayout>
使用PercentRelativeLayout就不能再使用android:layout_width/android:layout_height,必须使用app:layout_widthPercent/app:layout_heightPercent来控制控件的大小
转载请注明出处amoscxy的博客:https://mp.youkuaiyun.com/mdeditor/80149395