具有嵌套ViewGroup的ViewGroup

问题描述 投票:2回答:1

我面临一个奇怪的问题。例如,我们有这样的布局:

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

我们的屏幕会变黑。如果我用FrameLayout或ConstraintLayout替换LinearLayout,结果相同。

但如果用RelativeLayout替换第一个LinearLayout,屏幕将变为红色!在我的情况下,我需要像RelativeLayout这样的行为,但应该使用LinearLayout。怎么可能?十分感谢!

android xml android-layout
1个回答
0
投票

请尝试以下方法

<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"
© www.soinside.com 2019 - 2024. All rights reserved.