【问题标题】:ConstraintLayout margins not workingConstraintLayout 边距不起作用
【发布时间】:2017-07-06 17:25:49
【问题描述】:

如果我被限制的视图的可见性消失了,使用marginTop 下方的 xml 将被忽略。

此时最新的布局库版本会发生这种情况com.android.support.constraint:constraint-layout:1.0.0-beta4

例子:

tvMessageivCommentImagevisible - llLeftActionsllRightActions 的 16dp 上边距工作正常。如果ivCommentImagegone,则忽略边距。

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

<!-- some more views here -->

    <TextView
        android:id="@+id/tvMessage"
        style="@style/SocialFeed.Description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ivProfile"
        app:layout_goneMarginTop="0dp"
        tools:text="@string/lorem_ipsum_140chars"/>

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/ivCommentImage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:adjustViewBounds="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvMessage"
        app:layout_goneMarginTop="0dp"
        />

    <android.support.constraint.Guideline
        android:id="@+id/gCenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"/>

    <LinearLayout
        android:id="@+id/llLeftActions"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:gravity="center_vertical|left"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="@+id/tvMessage"
        app:layout_constraintRight_toLeftOf="@+id/gCenter"
        app:layout_constraintTop_toBottomOf="@+id/ivCommentImage"
        app:layout_goneMarginTop="0dp"
        />

    <LinearLayout
        android:id="@+id/llRightActions"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:gravity="center_vertical|right"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="@+id/gCenter"
        app:layout_constraintRight_toRightOf="@+id/tvMessage"
        app:layout_constraintTop_toBottomOf="@+id/ivCommentImage"
        app:layout_goneMarginTop="0dp"/>


</android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

【问题讨论】:

    标签: android android-xml android-constraintlayout


    【解决方案1】:

    它完全按预期工作 - 请注意,您在 @id/llLeftActions@id/llRightActions 上添加了 layout_goneMarginTop="0dp"...

    这意味着当它们指向的小部件 (@id/ivCommentImage) 被标记为已消失时,将用于顶部连接的边距将是... 0dp,而不是 16dp 的边距。这就是这些属性的用途! :)

    查看visibility behaviorgone margin attributes 上的文档

    【讨论】:

      【解决方案2】:
      <TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Available chats"
          tools:layout_editor_absoluteX="1dp"
          tools:layout_editor_absoluteY="1dp" />
      
      <ListView
          android:id="@+id/listChats"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          app:layout_constraintTop_toBottomOf="@+id/textView"/>
      

      请注意最后一行,在边缘添加约束会使约束起作用。

      您还可以在 Studio 中使用设计视图,并在对象之间拖放约束。

      【讨论】:

      • 我也有同样的问题,使用@+id/textView 有效,但@id/textView 无效。为什么@+id/textView 不仅仅是@id/textView
      猜你喜欢
      • 2022-08-12
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      相关资源
      最近更新 更多