百分比布局
来源:互联网 发布:md5用php可以解密吗 编辑:程序博客网 时间:2024/04/28 07:03
在百分比布局中,我们可以不再使用wrap_content 、match_parent等方式来指定控件的大小,而是允许直接指定控件在布局中所占的百分比,这样的话就可以轻松实现平分布局甚至是任意比例分割布局的效果了。
由于LinearLayout本身已经支持按比例指定控件的大小了,因此百分比布局只为FrameLayout和Relativelayout进行了功能扩展。
我们只需在项目的build.gradle中添加百分比布局库的依赖,就能保证百分比布局在Android所有系统版本上的兼容性了。
添加如下:
dependencies { compile 'com.android.support:percent:25.0.1'}
接下来修改xml文件的代码就可以啦。
<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/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn1" android:text="1" app:layout_widthPercent="50%" app:layout_heightPercent="50%" android:background="@color/colorAccent" android:layout_marginLeft="26dp" android:layout_marginStart="26dp" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="16dp" /> <Button android:id="@+id/btn2" android:text="2" app:layout_widthPercent="30%" app:layout_heightPercent="20%" android:layout_marginRight="88dp" android:layout_marginEnd="88dp" android:layout_marginTop="16dp" android:layout_below="@+id/btn1" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <Button android:id="@+id/btn3" android:text="3" app:layout_widthPercent="30%" app:layout_heightPercent="30%" android:layout_below="@+id/btn2" android:layout_marginTop="8dp" /></android.support.percent.PercentRelativeLayout>
最外层使用了PercentRelativeLayout,由于百分比布局并不是内置在系统SDK当中,所以需要把完整的包路径写出来。然后还必须定义一个app的命名空间,这样才能使用百分比布局的自定义属性。
使用app:layout_widthPercent=”50%”将按钮的宽度指定为布局的50%,
使用app:layout_heightPercent=”50%”将按钮的高度指定为布局的50%。
这里的app前缀属性之所以能用是因为刚才定义了app的命名空间,当然我们一直能使用的android前缀也是一样道理的。
PercentRelativeLayout是继承了RelativeLayout的所有属性的,同样PercentFrameLayout是继承了FrameLayout的所有属性的。
以下为效果图:
0 0
- 百分比布局
- 百分比布局
- 百分比布局
- 百分比布局
- 百分比布局
- 百分比布局
- 百分比布局
- 百分比布局
- CSS 使用百分比布局
- RelativeLayout实现百分比布局
- CSS百分比布局
- Android 支持百分比布局
- Android支持百分比布局
- Android百分比布局初探
- iOS AutoLayout 百分比布局
- Android百分比布局:PercentRelativeLayout
- Android百分比布局:PercentFrameLayout
- Android 百分比布局
- 研究生课程 算法分析-分治法
- Scrap学习笔记 --- python实现抓取图片
- tab切换fragment时,会出现滑动viewpager卡顿迟缓问题
- C语言-简单创建线程之pthread.h
- chdir改变当前目录
- 百分比布局
- java 异常com/google/protobuf/UnknownFieldSet
- JAVA byte最大值是127,最小值确实-128
- Python基础
- 单元总结(1)
- VUWE
- JavaScript - 模拟键盘输入支付密码
- const 的一些用法
- 电控笔记