使用 <include> 的 ConstraintLayout 问题
ConstraintLayout Problems using <include>
我想弄清楚为什么这不起作用。我只在 ConstraintLayout
中添加了两个 <include>
部分,并且布局不遵循我设置的任何约束。我正在尝试开始迁移到使用 ConstraintLayout
作为我的首选布局,但像这样的事情一直把我推回 RelativeLayout
和 LinearLayout
.
这是顶级布局文件(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>
试试这个 从您包含的两个布局中删除 <merge>
标签
<?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>
从您包含的两个布局中删除 <merge>
标签。
当您在 <include>
标签上指定属性时,系统会将这些属性应用于包含的布局的根视图。 <merge>
标签是一个特殊元素,允许您在布局中拥有多个视图 而无需 具有根视图。因此,当您包含的布局使用 <merge>
标记时,您的所有约束都会被丢弃。幸运的是,您包含的两个布局在合并中只有一个视图,因此您可以完全删除合并。
正如 Cody 在评论中提到的,问题可能是 您没有为包含的布局设置 layout_width
和 layout_height
, 不会产生任何编辑器错误,但约束不再起作用。
我想弄清楚为什么这不起作用。我只在 ConstraintLayout
中添加了两个 <include>
部分,并且布局不遵循我设置的任何约束。我正在尝试开始迁移到使用 ConstraintLayout
作为我的首选布局,但像这样的事情一直把我推回 RelativeLayout
和 LinearLayout
.
这是顶级布局文件(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>
试试这个 从您包含的两个布局中删除 <merge>
标签
<?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>
从您包含的两个布局中删除 <merge>
标签。
当您在 <include>
标签上指定属性时,系统会将这些属性应用于包含的布局的根视图。 <merge>
标签是一个特殊元素,允许您在布局中拥有多个视图 而无需 具有根视图。因此,当您包含的布局使用 <merge>
标记时,您的所有约束都会被丢弃。幸运的是,您包含的两个布局在合并中只有一个视图,因此您可以完全删除合并。
正如 Cody 在评论中提到的,问题可能是 您没有为包含的布局设置 layout_width
和 layout_height
, 不会产生任何编辑器错误,但约束不再起作用。