【问题标题】:How do margins work in a ConstraintLayout?边距如何在 ConstraintLayout 中工作?
【发布时间】:2018-03-29 09:21:09
【问题描述】:

ma​​in_layout.xml

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

    <CollapsingTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

看起来像边界限制 ConstraintLayout 不考虑 any 水平边距,只是水平居中 TextView

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    这是因为您将 TextView 的宽度设置为 "wrap_content"

    要强制它适应约束,您需要使用"0dp" 的宽度。这是"match_constraint" 的功能等价物,但目前不被认为是一个选项。

    【讨论】:

      【解决方案2】:
      <android.support.constraint.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="wrap_content">
      
          <CollapsingTextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginEnd="16dp"
              android:layout_marginStart="16dp"
              android:layout_marginTop="8dp"
              android:textSize="16sp"
              app:layout_constraintTop_toTopOf="parent" />
      
      </android.support.constraint.ConstraintLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-12
        • 1970-01-01
        • 2013-12-12
        • 1970-01-01
        • 2019-12-12
        • 1970-01-01
        相关资源
        最近更新 更多