【问题标题】:ConstraintLayout Problems using <include>使用 <include> 的 ConstraintLayout 问题
【发布时间】:2020-08-22 16:18:58
【问题描述】:

我试图弄清楚为什么这不起作用。我只在ConstraintLayout 中添加了两个&lt;include&gt; 部分,并且布局没有遵循我设置的任何约束。我正在尝试开始迁移到使用 ConstraintLayout 作为我的首选布局,但这样的事情不断将我推回到 RelativeLayoutLinearLayout

这是顶级布局文件(ConstraintLayout),显示了一些不起作用的约束:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <include
        android:id="@+id/includeButton"
        layout="@layout/include_button_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <include
        android:id="@+id/includeText"
        layout="@layout/include_text_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

</android.support.constraint.ConstraintLayout

这是第一个包含的布局(include_button_panel):

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/include_button_panel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Touch me!" />

</merge>

这是第二个包含的布局(include_text_panel):

<?xml version="1.0" encoding="utf-8"?>
<merge
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/include_text_panel_text"
        android:layout_width="wrap_content"
        android:background="#7986cb"
        android:layout_height="wrap_content"
        android:text="This is the text panel"
        android:textColor="@android:color/black"
        android:textSize="18sp" />
</merge>

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    从您包含的两个布局中删除 &lt;merge&gt; 标记。

    当您在&lt;include&gt; 标签上指定属性时,系统会将这些属性应用到包含布局的根视图。 &lt;merge&gt; 标签是一个特殊元素,允许您在布局中拥有多个视图 没有根视图。因此,当您包含的布局使用 &lt;merge&gt; 标记时,您的所有约束都会被抛出。幸运的是,您包含的两个布局在合并中只有一个视图,因此您可以完全删除合并。

    【讨论】:

    • 当你不使用合并标签但&lt;include&gt; 布局仍然忽略其父级中的约束时,你会怎么做?我遇到了这个确切的问题。
    • 对于任何想知道的人,我的问题的答案是我没有在父布局中的 &lt;include&gt; 声明中包含 android:layout_widthandroid:layout_heightstackoverflow.com/questions/43625651/…
    【解决方案2】:

    试试这个从你包含的两个布局中删除&lt;merge&gt;标签

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteY="81dp"
        tools:layout_editor_absoluteX="0dp">
    
        <include
            android:id="@+id/includeButton"
            layout="@layout/include_button_panel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <include
            android:id="@+id/includeText"
            layout="@layout/include_text_panel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案3】:

      正如 Cody 在 cmets 中提到的,问题可能是 您没有为包含的布局设置 layout_widthlayout_height ,女巫不会产生任何编辑器错误,但约束不会不能再工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-20
        • 1970-01-01
        • 2021-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多