android 百分比布局percentFrameLayout,percentRelativeLayout的使用

来源:互联网 发布:阿里云速度 编辑:程序博客网 时间:2024/05/22 08:04


这种布局方式是google新添加的一种布局,需要引入安卓的support兼容包,这个包大家都很清楚,一般android的新特效新功能都会在support包中。


我们知道。在安卓的布局中,只有LinearLayout才支持设置权重weight来按比例划分控件的大小,其他布局都不支持这种方式,这就显得,有时候我们一些复杂点的布局搞起来就有点繁琐。


为此,android引入了一种全新的布局方式来解决这个问题,---百分比布局,简单的理解就是在这个布局的控件可以设置百分比啦,很高大上有木有,,,,


1.引入


compile'com.android.support:percent:24.2.1'
2.新建一个layout文件
<?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:text="1"        android:layout_gravity="left|top"        app:layout_widthPercent="50%"        app:layout_heightPercent="50%"        />    <Button        android:text="2"        android:layout_gravity="right|top"        app:layout_widthPercent="50%"        app:layout_heightPercent="50%"        />    <Button        android:text="3"        android:layout_gravity="left|bottom"        app:layout_widthPercent="50%"        app:layout_heightPercent="50%"        />    <Button        android:text="4"        android:layout_gravity="right|bottom"        app:layout_widthPercent="50%"        app:layout_heightPercent="50%"        /></android.support.percent.PercentFrameLayout>
我们可以看到,这里多了两个新属性---
app:layout_widthPercent="50%"app:layout_heightPercent="50%"
这就是指定我们的控件和父布局的百分之50.
上图

原创粉丝点击