android - 具有嵌套 ViewGroup 的 ViewGroup

标签 android xml android-layout

我遇到了一个奇怪的问题。 例如,我们有这样的布局:

<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,结果相同。

但是如果先用 RelativeLayout 替换 LinearLayout 屏幕会变红! 在我的例子中,我需要类似 RelativeLayout 的行为,但应该使用 LinearLayout。 怎么可能? 非常感谢!

最佳答案

尝试以下操作

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

如果 super 父级是 LinearLayout 而不是 ConstraintLayout,请将 app:layout_constraintVertical_weight="1" 更改为 android:layout_weight="1" 并删除其他不必要的代码。如果 super 父级是相对布局,则只需删除“weight”参数并添加以下内容。

android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"

关于android - 具有嵌套 ViewGroup 的 ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55246653/

相关文章:

java - 我是否错误地使用了包?

android - EditText 底线颜色在焦点上变化

Android 应用程序可在模拟器上运行并通过设备上的 USB 调试运行,但不能在独立设备上运行

java - 无法处理 DOMSource : check that saxon9-dom. jar 在类路径上

java - 我可以在 Android 中将 FloatingActionButton 转换为 ImageButton 吗?

android导出到csv并作为电子邮件附件发送

c# - 在 .NET 中使用 XmlReader 对非常大的 XML 文件进行 XSLT 转换

java - 在 Resin 和 Spring-WS 中加载 XML Schema 文档的问题

android - 如何在android中为TextView设置字体?

android - 电子邮件附件图片无法发送