为什么android layout_weight不能按预期工作?

时间:2016-11-23 13:16:35

标签: android android-linearlayout android-layout-weight

我有两个线性布局,每个占据总空间的50%。 在它们内部有不同的视图,我不明白为什么horizontalScrollView1listView1(看起来是ID)不可见。 这两个视图应该扩展以填充其父级中剩余的空间。 我做错了什么?

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout        
       android:layout_height="0dp"
       android:layout_weight="1"
       android:layout_width="match_parent">
       <HorizontalScrollView
         android:id="@+id/header"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"/>
       <HorizontalScrollView
         android:layout_height="0dp"
         android:layout_weight="1"
         android:layout_width="match_parent"
         android:id="@+id/horizontalScrollView1">
         <ListView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:id="@+id/list"/>
       </HorizontalScrollView>
    </LinearLayout>
    <LinearLayout         
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_width="match_parent"  > 
      <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Hello!!!" />
      <ListView
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="1"
         android:id="@+id/listView1"/>
    </LinearLayout>
 </LinearLayout>

4 个答案:

答案 0 :(得分:3)

您忘记将android:orientation="vertical"设置为主LinearLayout的{​​{1}}。这样LinearLayout的孩子就会被放置在垂直方向并且可见。

答案 1 :(得分:0)

你正在对你的元素使用match_parent,所以当其中一个占据整个宽度时,另一个没有绘制的位置。将内部元素宽度设置为wrap_content。如果你不做重量,LinearLayout的方向也不起作用。

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout        
       android:layout_height="0dp"
       android:layout_weight="1"
       android:layout_width="match_parent"
       android:orientation="vertical">
       <HorizontalScrollView
         android:id="@+id/header"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
       <HorizontalScrollView
         android:layout_height="0dp"
         android:layout_weight="1"
         android:layout_width="wrap_content"
         android:id="@+id/horizontalScrollView1">
         <ListView
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:id="@+id/list"/>
       </HorizontalScrollView>
    </LinearLayout>
    <LinearLayout         
      android:layout_height="0dp"
      android:layout_weight="1"
      android:layout_width="match_parent"  
      android:orientation="vertical"> 
      <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Hello!!!" />
      <ListView
         android:layout_width="wrap_content"
         android:layout_height="0dp"
         android:layout_weight="1"
         android:id="@+id/listView1"/>
    </LinearLayout>
 </LinearLayout>

答案 2 :(得分: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="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:background="@color/accent"
            android:layout_width="match_parent">
            <HorizontalScrollView
                android:id="@+id/header"
                android:background="@color/primary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <HorizontalScrollView
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:background="@color/black"
                android:id="@+id/horizontalScrollView1">
                <ListView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/list"/>
            </HorizontalScrollView>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:background="@color/green"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="match_parent"  >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Hello!!!" />
            <ListView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:id="@+id/listView1"/>
        </LinearLayout>
    </LinearLayout>

基本上你忘了在某些地方添加orientation = vertical,还需要指定android:layout_weight =&#34; 1&#34;对于你的两个Horizo​​ntalScrollView。

答案 3 :(得分:0)

这种情况正在发生,因为你没有提到@ + id / header水平滚动视图的权重。所以它采用了它的父线性布局的全高度。而且您忘了为两个父线性布局添加方向类型。

您可以通过此xml布局进行检查。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <HorizontalScrollView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </HorizontalScrollView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello!!!" />

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>