如何强制一个空的 ConstraintLayout 填充它的父容器?
How can I force an empty ConstraintLayout to fill its parent container?
我正在使用 ConstraintLayout
作为叠加层。它应该与其父容器一样宽和一样高。但是,由于某些奇怪的原因,它不会垂直填充其父项(但会水平填充)。这是相关的 XML:
<androidx.constraintlayout.widget.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:context=".ui.home.HomeFragment">
<androidx.constraintlayout.widget.ConstraintLayout <-- This is the overlay
android:id="@+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
使用上面的代码,叠加层也不可见(它的高度是 0dp
)。当我在里面放置 TextView
时,它会扩展以适合 TextView
。简而言之,它的行为就好像它的高度被设置为 wrap_content
.
如何在容器空着的同时强制它填满容器?
问题很可能是您的尺码规格。来自 documentation for ConstraintLayout:
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
确实应该为此制定一个 lint 规则,或者 ConstraintLayout 应该拒绝 match_parent
.
我正在使用 ConstraintLayout
作为叠加层。它应该与其父容器一样宽和一样高。但是,由于某些奇怪的原因,它不会垂直填充其父项(但会水平填充)。这是相关的 XML:
<androidx.constraintlayout.widget.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:context=".ui.home.HomeFragment">
<androidx.constraintlayout.widget.ConstraintLayout <-- This is the overlay
android:id="@+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
使用上面的代码,叠加层也不可见(它的高度是 0dp
)。当我在里面放置 TextView
时,它会扩展以适合 TextView
。简而言之,它的行为就好像它的高度被设置为 wrap_content
.
如何在容器空着的同时强制它填满容器?
问题很可能是您的尺码规格。来自 documentation for ConstraintLayout:
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
确实应该为此制定一个 lint 规则,或者 ConstraintLayout 应该拒绝 match_parent
.