Android linearlayout中layout_weight详解

来源:互联网 发布:telnet的端口号是多少 编辑:程序博客网 时间:2024/04/29 22:59

在学习android布局过程中,发现linearlayout中的layout_weight属性有些奇怪,因此记录下来

1.先上一段代码,需要注意的是TextView中layout_width的值和layout_wwight的值

<?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="wrap_content"    android:orientation="horizontal" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="#FFFFFF"        android:text="111"        android:textSize="20sp" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="#444444"        android:text="222"        android:textSize="20sp" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="#666666"        android:text = "333"        android:textSize="20sp"/>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="#999999"        android:text = "444"        android:textSize="20sp"/>    </LinearLayout>

下面是这段代码的效果图

2.改变TextView的layout_weight值

<?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="wrap_content"    android:orientation="horizontal" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="#FFFFFF"        android:text="111"        android:textSize="20sp" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="#444444"        android:text="222"        android:textSize="20sp" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="<span style="color:#FF0000;">2</span>"        android:background="#666666"        android:text = "333"        android:textSize="20sp"/>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="<span style="color:#FF0000;">4</span>"        android:background="#999999"        android:text = "444"        android:textSize="20sp"/>    </LinearLayout>
注意此处只修改了两个参数的值,下面是效果图

是不是有点奇怪呢,第三个和第四个TextView都没有显示出来

3.下面将TextView的数目减少到两个,而layout_weight属性值分别设置为1和2,会发生什么情况呢

<?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="wrap_content"    android:orientation="horizontal" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="<span style="color:#FF0000;">1</span>"        android:background="#FFFFFF"        android:text="111"        android:textSize="20sp" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="<span style="background-color: rgb(255, 0, 0);">2</span>"        android:background="#444444"        android:text="222"        android:textSize="20sp" /> </LinearLayout>

会出现什么情况呢,下面是效果图


4.然后将其中一个layout_weight的值不做设置,看看默认的会是什么样的

<?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="wrap_content"    android:orientation="horizontal" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#FFFFFF"        android:text="111"        android:textSize="20sp" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="2"        android:background="#444444"        android:text="222"        android:textSize="20sp" /></LinearLayout>
下面是效果图

看样子不写的时候layout_weight的值默认是0了

5.下面将TextView中的layout_width值设置为0,看看又会出现什么意想不到的效果呢

<?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="wrap_content"    android:orientation="horizontal" >    <TextView        android:layout_width="0"        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="#FFFFFF"        android:text="111"        android:textSize="20sp" />    <TextView        android:layout_width="0"        android:layout_height="wrap_content"        android:layout_weight="2"        android:background="#444444"        android:text="222"        android:textSize="20sp" />    <TextView        android:layout_width="0"        android:layout_height="wrap_content"        android:layout_weight="3"        android:background="#666666"        android:text = "333"        android:textSize="20sp"/>    <TextView        android:layout_width="0"        android:layout_height="wrap_content"        android:layout_weight="4"        android:background="#999999"        android:text = "444"        android:textSize="20sp"/>    </LinearLayout>
下面上效果图了


总结:

通过上面的几个例子可以得出以下结论

1.当LinearLayout的几个子控件的layout_width="match_parent"的时候,而且有layout_weight的时候,子控件所占的宽度与layout_weight的值成反比,layout_weight的默认值是0,即会占满整个宽度。

2.至于当子控件的layout_weight值设置的不一致时,有些子控件并没有显示,很奇怪

3.当子控件的layout_width设置为0dip的时候,每个自控件耳朵宽度与该子控件的layout_weight的值成正比。

本人也是android初学者,希望多与大家交流,学习经验。如有不当或错误之处,请指出。


0 0
原创粉丝点击