简体   繁体   English

如何分辨布局以50/50间隔所有内容?

[英]How to tell layout to space everything 50/50?

I have a vertical linear layout with two ListViews. 我有一个带有两个ListView的垂直线性布局。 I want the top ListView to take up 50% of the screen. 我希望顶部的ListView占据屏幕的50%。 I want the bottom listivew to take up 50% of the screen. 我希望底部的列表框占据屏幕的50%。 How can I do this in XML? 如何在XML中做到这一点?

The following layout should work 以下布局应该有效

<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>

How this works: 工作原理:

  • Initially both lists have no height 最初,两个列表都没有高度
  • After measuring everything with a height, the remaining height (in this case all of it as there are no other views with a specified height or wrap_content ) is divided up between all the views with no height and a layout_weight 在测量所有具有高度的东西之后,将剩余的高度(在这种情况下,因为没有其他具有指定高度或wrap_content的视图,所以全部都被划分为所有没有高度的视图和layout_weight
  • The height of each view will be (layout_weight of View) / (Sum of layout_weights in parent ViewGroup) 每个视图的高度将为(layout_weight of View) / (Sum of layout_weights in parent ViewGroup)视图的(layout_weight of View) / (Sum of layout_weights in parent ViewGroup)
  • In this case, (Sum of layout_weights in parent ViewGroup) = 2 and (layout_weight of View) = 1 for each ListView , so each ListView occupies 1/2 of the available space 在这种情况下,每个ListView (layout_weight of View) = 1 (Sum of layout_weights in parent ViewGroup) = 2 (layout_weight of View) = 1(Sum of layout_weights in parent ViewGroup) = 2并且(layout_weight of View) = 1 ,因此每个ListView占用可用空间的1/2

将Vertical LinearLayout设置为具有height:fill_parent ,然后将weight of each ListView to "1" eg android:layout_weight="1"weight of each ListView to "1" eg android:layout_weight="1"设置weight of each ListView to "1" eg android:layout_weight="1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM