我有一个根标签为
的 Activityandroidx.constraintlayout.widget.ConstraintLayout
我在此 Activity 中包含 3 个布局文件并垂直列出它们。中间一个下面的约束不起作用
app:layout_constraintTop_toBottomOf="@+id/lay1"
app:layout_constraintBottom_toTopOf="@+id/lay3"
我不明白为什么中间那个占据了整个屏幕而不是从顶部的底部开始到底部的顶部结束。你能帮帮我吗?
Activity :
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/lay1"
layout="@layout/empty_linear_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="@+id/lay2"
layout="@layout/empty_linear_layout"
app:layout_constraintBottom_toTopOf="@+id/lay3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lay1" />
<include
android:id="@+id/lay3"
layout="@layout/empty_linear_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
empty_linear_layout.xml :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="button" />
</LinearLayout>
最佳答案
如果你想覆盖layout_
<include>
上的参数布局,你必须同时指定 layout_width
和 layout_height
.来自 the developer docs :
However, if you want to override layout attributes using the
<include>
tag, you must override bothandroid:layout_height
andandroid:layout_width
in order for other layout attributes to take effect.
因此将这两行添加到您的第二个 View :
android:layout_width="0dp"
android:layout_height="0dp"
(注意对于ConstraintLayout
,0dp
表示匹配约束条件)
关于Android 约束布局无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57339306/