学习android布局中的线性布局时,遇到这么个问题。
按照官网的例子,layout文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="2">
- <TextView
- android:text="red"
- android:gravity="center_horizontal"
- android:background="#aa0000"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- <TextView
- android:text="green"
- android:gravity="center_horizontal"
- android:background="#00aa00"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- <TextView
- android:text="blue"
- android:gravity="center_horizontal"
- android:background="#0000aa"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- <TextView
- android:text="yellow"
- android:gravity="center_horizontal"
- android:background="#aaaa00"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- </LinearLayout>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:text="row one"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"/>
- <TextView
- android:text="row two"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"/>
- <TextView
- android:text="row three"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"/>
- <TextView
- android:text="row four"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"/>
- </LinearLayout>
- </LinearLayout>
按照说明,上面的列表应该比下面的列表占用更多的高度才对,因为它的weight是2,可结果却是反而比下面的列表更矮,很奇怪。
于是请教公司的大牛,将文件修改如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="2">
- <TextView
- android:text="red"
- android:gravity="center_horizontal"
- android:background="#aa0000"
- android:layout_width="0dip"
- android:layout_height="fill_parent"
- android:layout_weight="2"/>
- <TextView
- android:text="greenaaa"
- android:gravity="center_horizontal"
- android:background="#00aa00"
- android:layout_width="0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- <TextView
- android:text="blue"
- android:gravity="center_horizontal"
- android:background="#0000aa"
- android:layout_width="0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- <TextView
- android:text="yellow"
- android:gravity="center_horizontal"
- android:background="#aaaa00"
- android:layout_width="0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- </LinearLayout>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1">
- <TextView
- android:text="row one"
- android:textSize="10pt"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="3"/>
- <TextView
- android:text="row two"
- android:textSize="10pt"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1"/>
- <TextView
- android:text="row three"
- android:textSize="10pt"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1"/>
- <TextView
- android:text="row four"
- android:textSize="10pt"
- android:layout_width="fill_parent"
- android:layout_height="0dip"
- android:layout_weight="1"/>
- </LinearLayout>
- </LinearLayout>
垂直布局时,将每个element的高度设置为“0dip”,水平布局时,将每个element的宽度设置为“0dip”,那么布局就会严格按照weight的比例显示了。