如何告诉布局将所有东西都放到50/50?

She*_*lam 1 java xml eclipse android android-linearlayout

我有一个带有两个ListView的垂直线性布局.我希望顶级ListView占据屏幕的50%.我希望底部的listivew占据屏幕的50%.我怎么能用XML做到这一点?

Jos*_*arl 11

以下布局应该有效

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <ListView
        android:id="@+id/list1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <ListView
        android:id="@+id/list2"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是如何工作的:

  • 最初两个列表都没有高度
  • 在测量了所有高度之后,剩余高度(在这种情况下全部因为没有其他具有指定高度的视图或wrap_content)在所有视图之间划分,没有高度和layout_weight
  • 每个视图的高度都是 (layout_weight of View) / (Sum of layout_weights in parent ViewGroup)
  • 在这种情况下,(Sum of layout_weights in parent ViewGroup) = 2(layout_weight of View) = 1每个ListView,因此每个ListView占用1/2的可用空间