【问题标题】:Set width to match constraints in ConstraintLayout设置宽度以匹配 ConstraintLayout 中的约束
【发布时间】:2016-06-02 23:32:13
【问题描述】:

我想将视图的左侧和右侧限制为其父视图的边距,并使其填充分配的空间。但是,将宽度设置为 match_parentwrap_content 似乎会产生相同的结果。

是否有与 match_constraints 等价的东西(而不是 match_parent 和 wrap_content)? match_parentwrap_content 会影响布局还是在新的约束布局中被忽略?

【问题讨论】:

  • 为什么match_parent 不适合你?

标签: android android-constraintlayout


【解决方案1】:

match_parent 是不允许的。但是您实际上可以将宽度和高度设置为 0dp,并将顶部和底部或左右约束设置为“父级”。

例如,如果你想对元素的宽度设置match_parent 约束,你可以这样做:

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>

【讨论】:

  • Android 团队应该让“match_parent”工作。这将非常容易。只需让 match_parent 提供与应用程序被限制为父级的开始和结束相同的功能。很容易垂直或水平检测。
  • 但是 0dp 不会导致双重测量吗?
【解决方案2】:

不支持match_parent,所以使用android:layout_width="0dp"。使用0dp,您可以将约束视为“可扩展”而不是“填充剩余的”。

另外,0dp 可以由一个位置定义,其中match_parent 的位置(x、y 和宽度、高度)依赖于它的父级

【讨论】:

  • 为什么这个被接受的答案?!?约束布局不支持 match_parent。而这个答案无法实现它。
  • match_parent 不受支持。
  • 这个答案是正确的。它明确指出不支持 match_parent。此外,为了对“match_parent”设置执行合理的替代,0dp 的左右两侧设置为父级(边距 0 或选择适合)将给出相同的结果。在 Arieck 的回答中,这个答案中唯一真正遗漏的是需要在两侧(或垂直的顶部和底部)设置约束。我就是这样做的,我没有任何问题。此外,当与其他组件一起使用时,它还可以作为重量设置。
  • 如果有人来这里寻求它不起作用的答案。它似乎在测试版中被破坏了。它似乎在 1.0.2 中工作。经验教训 - 冻结库,不要使用机会更新。
  • @Tequilaman 评论可以是答案。他正确地指出了实施它的方法。在两边创建一个 0dp 边距的约束,它就可以了。
【解决方案3】:

显然match_parent 是:

  • NOT OK 对于直接在 ConstraintLayout 下的视图
  • OK 对于嵌套在 ConstraintLayout 正下方的视图中的视图

所以如果你需要你的视图作为match_parent,那么:

  1. ConstraintLayout直系子女应使用 0dp
  2. 嵌套元素 (例如,ConstraintLayout 的孙子) 可以使用match_parent

例子:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="16dp">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/phoneNumberInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/phoneNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.design.widget.TextInputLayout>

【讨论】:

  • 在 ConstraintLayout 中嵌套的 ConstraintLayout 怎么样,在您的示例中将 TextInputLayout 更改为 ConstraintLayout?
  • @superuser 然后这是一个约束布局,同样的规则适用。 0dp
【解决方案4】:

ConstraintLayout 不支持match_parent。将宽度设置为0dp 以使其匹配约束。

【讨论】:

  • 并且支持wrap_content?
  • 是的,据我所知。
  • 支持wrap_content
【解决方案5】:

来自official doc

重要提示:不建议将 MATCH_PARENT 用于包含在 约束布局。类似的行为可以通过使用来定义 MATCH_CONSTRAINT 与对应的左/右或上/下 约束被设置为“父”。

所以如果你想达到MATCH_PARENT的效果,你可以这样做:

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

【讨论】:

  • 请注意,截至 2021 年的文档更加强大。它不再被称为“推荐”:developer.android.com/training/constraint-layout 注意:您不能将 match_parent 用于 ConstraintLayout 中的任何视图。而是使用“匹配约束”(0dp)。
【解决方案6】:

您可以检查您的适配器。

 1 - MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater);
 2 - MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater, viewGroup, false);

我在使用 1 时遇到了和你一样的问题。 你可以试试2。

【讨论】:

  • 这是金子!谢谢!
【解决方案7】:

直接将视图设置为 match_parent 是不可能的,但我们可以用一些不同的方式来实现,但不要忘记在 Start 和 End 中使用 Left 和 Right 属性,因为如果你使用 RTL 支持,它会需要。

    <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

【讨论】:

    【解决方案8】:

    将宽度或高度(您需要匹配父级的任何内容)设置为 0dp 并设置左侧、右侧、顶部、底部的边距作为匹配父级

    【讨论】:

      【解决方案9】:

      在办公室文档中: https://developer.android.com/reference/android/support/constraint/ConstraintLayout

      当维度设置为 MATCH_CONSTRAINT 时,默认行为是让生成的大小占用所有可用空间。

      使用 0dp,相当于“MATCH_CONSTRAINT”

      重要提示:不建议将 MATCH_PARENT 用于 ConstraintLayout 中包含的小部件。类似的行为可以通过使用 MATCH_CONSTRAINT 来定义,并将相应的左/右或上/下约束设置为“父”

      【讨论】:

        【解决方案10】:

        如果您希望 TextView 位于父级的中心..
        你的主要布局是约束布局

        <androidx.appcompat.widget.AppCompatTextView
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:text="@string/logout"
             app:layout_constraintLeft_toLeftOf="parent"
             app:layout_constraintRight_toRightOf="parent"
             android:gravity="center">
        </androidx.appcompat.widget.AppCompatTextView>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-12-03
          • 2020-02-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-29
          • 1970-01-01
          相关资源
          最近更新 更多