为什么 TextInputLayout 不跟随父 ConstraintLayout 的宽度?
Why doesn't the the TextInputLayout follow the width of parent ConstraintLayout?
你可以在这里看到我有一个 CardView,它是一个 ConstraintLayout 的父级。
此外,CardView layout_width 设置为 match_parent。 ConstraintLayout 设置为 match_parent。这一切都有效,因为 CardView 和 ConstraintLayout 是整个布局区域的宽度。
问题
您还可以看到我有一个 TextInputLayout,它是 ConstraintLayout 的子项。我对 layout_width 的唯一设置选择是 match_constraint(如下图所示)。
当我选择 match_constraint 时,该值会自动设置为 0dp,并且小部件最终在屏幕上为 0dp。
这是一个错误吗?
为什么 TextInputLayout 不遵循 ConstraintLayout 的宽度(整个布局的宽度)——类似于 match_parent?
你能建议解决这个问题的正确方法吗?是否应将 TextInputLayout 的 layout_width 设置为不同的值?
如果我查看布局的 XML,我会看到以下 TextInputLayout
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/description_layout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent">
如果我像下面这样更改 layout_width(通过手动输入值):
android:layout_width="match_parent"
然后布局看起来像我期望的那样。这似乎解决了这个问题。正确吗?
您不应将 match_parent
用于 Views
,它们是 ConstraintLayout
的子项,如 documentation:
中所述
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".
你的案例中缺少的是 TextInputLayout
的 end
约束。要使用 0dp
(match_constraint),您需要为该维度指定两个约束:
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/description_layout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
你可以在这里看到我有一个 CardView,它是一个 ConstraintLayout 的父级。
此外,CardView layout_width 设置为 match_parent。 ConstraintLayout 设置为 match_parent。这一切都有效,因为 CardView 和 ConstraintLayout 是整个布局区域的宽度。
问题 您还可以看到我有一个 TextInputLayout,它是 ConstraintLayout 的子项。我对 layout_width 的唯一设置选择是 match_constraint(如下图所示)。
当我选择 match_constraint 时,该值会自动设置为 0dp,并且小部件最终在屏幕上为 0dp。
这是一个错误吗?
为什么 TextInputLayout 不遵循 ConstraintLayout 的宽度(整个布局的宽度)——类似于 match_parent? 你能建议解决这个问题的正确方法吗?是否应将 TextInputLayout 的 layout_width 设置为不同的值?
如果我查看布局的 XML,我会看到以下 TextInputLayout
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/description_layout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent">
如果我像下面这样更改 layout_width(通过手动输入值):
android:layout_width="match_parent"
然后布局看起来像我期望的那样。这似乎解决了这个问题。正确吗?
您不应将 match_parent
用于 Views
,它们是 ConstraintLayout
的子项,如 documentation:
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".
你的案例中缺少的是 TextInputLayout
的 end
约束。要使用 0dp
(match_constraint),您需要为该维度指定两个约束:
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/description_layout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />