使用layout_weight

时间:2011-11-15 09:30:25

标签: android android-layout

为什么中间的按钮消失了?而不是伸展来填充他们父母的所有宽度?而当我设置layout_height = 0dp时,它会使用高度进行填充并填充其父级吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"        
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Button" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:text="TextView" />

1 个答案:

答案 0 :(得分:3)

由于线性布局方向是垂直的,如果layout_weight为“1”,则仅占用剩余高度,此处方向是关键。如果要共享宽度,可以将方向更改为“水平”。请记住,您的孩子将被水平放置。