Android LinearLayout не виден - PullRequest
       12

Android LinearLayout не виден

7 голосов
/ 14 октября 2019

Я работаю над приложением для 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 не отображается на экране.

Если я переместу его вверх, будет показано, однако перемещенный ниже макет не будет виден.

Iне понимаю проблемы, не могли бы вы помочь найти ее?

Ответы [ 3 ]

3 голосов
/ 14 октября 2019

Проблема в том, что общая сумма весов вложенных 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 голосов
/ 14 октября 2019

Изменить android:id="@+id/linear_layout_row_container

android:weightSum="2"

на

android:weightSum="3"
1 голос
/ 14 октября 2019

попробуйте это:

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