迁移到 www.trinea.cn @Android @Java @性能优化 @开源,滴滴国际化项目 Android 端演进

Android LinearLayout的android:layout_weight属性

本文主要介绍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代码如下

<?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"是为了方便查看而设置

 

 

 



已有 0 人发表留言,猛击->>这里<<-参与讨论


ITeye推荐



posted @ 2012-04-16 22:46  Trinea  阅读(1344)  评论(0编辑  收藏  举报