ViewGroup с вложенной ViewGroup - PullRequest
2 голосов
/ 19 марта 2019

У меня странная проблема. Например, у нас есть этот макет:

<android.support.constraint.ConstraintLayout 
...
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">

<LinearLayout
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f00"/>

</LinearLayout>
</android.support.constraint.ConstraintLayout>

Наш экран станет черным. Тот же результат, если я заменю LinearLayout на FrameLayout или ConstraintLayout.

Но если заменить первый LinearLayout на RelativeLayout, экран будет красным! В моем случае мне нужно поведение, подобное RelativeLayout, но я должен использовать LinearLayout. Как это возможно? Большое спасибо!

1 Ответ

0 голосов
/ 19 марта 2019

Попробуйте следующее

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000">
    <LinearLayout
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintVertical_weight="1"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#f00"></LinearLayout>
    </LinearLayout> </android.support.constraint.ConstraintLayout>

Если суперпопулярником является LinearLayout вместо ConstraintLayout, измените app:layout_constraintVertical_weight="1" на android:layout_weight="1" и удалите другие ненужные коды. Если в качестве родительского элемента выбрано «Относительное расположение», просто удалите только параметр «вес» и добавьте следующее.

android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...