您的位置:首页 > 移动开发 > Android开发

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

2017-05-17 17:18 726 查看
这种布局方式是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.
上图

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: