【问题标题】:converting match_parent to "0dp"将 match_parent 转换为“0dp”
【发布时间】:2016-10-19 08:59:26
【问题描述】:

当我在约束布局中使用android:layout_height="match_parent"android:layout_width="match_parent" 作为子级的高度/宽度并构建Gradle 文件时,它会分别自动更改为android:layout_height="0dp"android:layout_widtht="0dp"

从技术上讲,0dpma​​tch_parent 之间存在巨大差异。

大多数时候它不会影响我的布局,但有时这个东西会完全破坏我的约束布局。

我不知道为什么会这样。
请指导我。

【问题讨论】:

标签: android android-layout android-studio android-constraintlayout


【解决方案1】:

在 ConstraintLayout 中不支持 match_parent。如果您想要相同的行为,请使用 0dp 并将每一侧的约束设置为父级。

【讨论】:

  • 如果不支持 match_parent,为什么在 IDE 的 layout_width 和 layout_height 下拉菜单中将其作为一个选项?我并不是说你错了(因为很明显,如果你为 Google 工作并且你的名字不断弹出来回答 ConstraintLayout 问题,那么你显然对此了解很多)。但我是说 ConstraintLayout 目前的整个实现是一团糟,目前无法使用。
  • 但是如果我希望我的视图高度或宽度在 ConstraintLayout 中为 0dp(值为 0)怎么办?我不能使用 0dp 和 0px,因为它将充当 match_parent。
【解决方案2】:

您不应将 match_parent 用于 ConstraintLayout 中的任何视图。而是使用“匹配约束”(0dp)

match_parent 表示 filling whats left,其中 0dp 表示 match constraints。在 ConstraintLayout 中,不支持 match_parent

match_parent 的位置(x,y 和宽度/高度)依赖于它的父级,但 0dp 可以由约束/位置定义

【讨论】:

    【解决方案3】:

    与我一起将相对项放在另一个元素下,宽度匹配父项的方法是:

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/xxx"
        app:layout_constraintEnd_toEndOf="@+id/xxx"
        >
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多