Android LinearLayout的android:layout_weight属性
2014-07-11 17:53
274 查看
本文主要介绍Android LinearLayout的android:layout_weight属性意义
android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根据LinearLayout中所有相关元素的此属性值计算的。
除了已经固定大小的,其他设置了此属性的view所占大小(长度或高度)为自己layout_weight属性值/所有layout_weight属性值*总大小。这个属性在android的sdk中都没有介绍。下面举例介绍下
比如在一个layout中显示3个TextView,第一个TextView长度占20%,第二个长度占50%,第三个占长度30%,
则比例为20%:50%:30%=2:5:3。layout代码如下
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="文本1" />
<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="5"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="文本2" />
<TextView android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="文本3" />
</LinearLayout>
从以上代码可以看出只需要设置各个TextView的android:layout_weight属性值为对应的比例即可
其中android:layout_alignParentLeft="true" android:gravity="center_vertical"是为了方便查看而设置
android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根据LinearLayout中所有相关元素的此属性值计算的。
除了已经固定大小的,其他设置了此属性的view所占大小(长度或高度)为自己layout_weight属性值/所有layout_weight属性值*总大小。这个属性在android的sdk中都没有介绍。下面举例介绍下
比如在一个layout中显示3个TextView,第一个TextView长度占20%,第二个长度占50%,第三个占长度30%,
则比例为20%:50%:30%=2:5:3。layout代码如下
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="文本1" />
<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="5"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="文本2" />
<TextView android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="文本3" />
</LinearLayout>
从以上代码可以看出只需要设置各个TextView的android:layout_weight属性值为对应的比例即可
其中android:layout_alignParentLeft="true" android:gravity="center_vertical"是为了方便查看而设置
相关文章推荐
- 关于Android UI组件LinearLayout属性layout_weight与layout_width/height的问题
- android之LinearLayout中android:layout_weight属性理解
- Android LinearLayout的android:layout_weight属性
- 关于Android UI组件LinearLayout属性layout_weight与layout_width/height的问题
- Android LinearLayout布局的layout_weight属性探究
- Android UI: LinearLayout中layout_weight 属性的使用规则
- 对android LinearLayout中layout_weight属性使用
- Android布局之LinearLayout weight属性的理解
- 对android LinearLayout中layout_weight属性使用初探
- Android:深度探究线性布局LinearLayout中权重(layout_weight)属性
- [转载]对android LinearLayout中layout_weight属性使用初探
- 详解 Android Views 元素的 layout_weight 属性
- Android中 android:layout_weight 属性 完美解释
- android布局之LinearLayout, layout_weight很有用
- android:layout_weight属性的使用
- android layout_weight 属性
- 关于android:layout_weight属性使用注意事项
- android中如何在代码中直接设置View的layout_weight属性
- LinearLayout android:layout_weight的理解
- LinearLayout中android:layout_weight的作用