Android LinearLayout不可见

问题描述 投票:0回答:3

我正在使用android应用程序。

我有问题,我的LinearLayout小部件在屏幕上不可见。

这是我的布局

<LinearLayout
    android:id="@+id/linear_layout_row_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginRight="5dp"
    android:orientation="vertical"
    android:weightSum="2">

    <LinearLayout
        android:id="@+id/linear_layout_row1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="60dp"
        android:layout_marginEnd="60dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2">

        // Other widgets

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_layout_row2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="60dp"
        android:layout_marginBottom="40dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="1">

        // Other widgets

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_layout_row3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="60dp"
        android:layout_marginBottom="40dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2">

        // Other widgets
    </LinearLayout>
</LinearLayout>

在这种情况下,linear_layout_row3在屏幕上不可见。

如果我将其向上移动,将显示出来,但是下面移动的布局将不可见。

我不明白这个问题,您能帮忙找到它吗?

android android-layout android-layout-weight
3个回答
1
投票

问题是嵌套LinearLayouts的权重总和等于3(1 + 1 + 1)。

但是,标识为LinearLayout的根linear_layout_row_container具有android:weightSum="2"

因此,最后一个孩子将不可见。

像这样将其更改为3

<LinearLayout
    android:id="@+id/linear_layout_row_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginRight="5dp"
    android:orientation="vertical"
    android:weightSum="2">
    // Nested layouts
</LinearLayout>

这应该可以正常工作。


2
投票

更改android:id="@+id/linear_layout_row_container

android:weightSum="2"

android:weightSum="3"

1
投票

尝试一下:

  <LinearLayout
      android:id="@+id/linear_layout_row_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="center"
      android:layout_marginStart="5dp"
      android:layout_marginLeft="5dp"
      android:layout_marginEnd="5dp"
      android:layout_marginRight="5dp"
      android:orientation="vertical"
      android:weightSum="3">

    <LinearLayout
        android:id="@+id/linear_layout_row1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="60dp"
        android:layout_marginEnd="60dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2">

      // Other widgets

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_layout_row2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="60dp"
        android:layout_marginBottom="40dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="1">

      // Other widgets

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_layout_row3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="60dp"
        android:layout_marginBottom="40dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="2">

      // Other widgets
    </LinearLayout>
  </LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.