首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有嵌套ViewGroup的ViewGroup

具有嵌套ViewGroup的ViewGroup
EN

Stack Overflow用户
提问于 2019-03-20 01:18:24
回答 1查看 382关注 0票数 2

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

代码语言:javascript
复制
<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替换first LinearLayout,屏幕将是红色的!在我的例子中,我需要像RelativeLayout这样的行为,但应该使用LinearLayout。这怎么可能呢?非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-20 03:30:55

请尝试以下操作

代码语言:javascript
复制
<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"并删除其他不必要的代码。如果超级父级是相对布局,那么只需删除"weight“参数,然后添加以下内容。

代码语言:javascript
复制
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55246653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档