ConstraintLayout的子项外边距无法显示?

6

我在我的应用程序中使用Android的新ConstraintLayout,但是我一直遇到元素边距无法显示的问题。为了参考,我在我的gradle文件中为ConstraintLayout使用了以下行:com.android.support.constraint:constraint-layout:1.0.0-beta4。我的布局代码如下。

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/post_vertical_padding"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/post_vertical_padding"
    android:background="?android:attr/selectableItemBackground">

    <TextView
        style="@style/TitleText"
        android:id="@+id/post_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/post_line_vertical_padding"
        android:layout_marginEnd="@dimen/post_type_horizontal_padding"
        android:layout_marginRight="@dimen/post_type_horizontal_padding"
        app:layout_constraintLeft_toRightOf="@id/poster_profile_pic"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/poster_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/post_type"
        app:layout_constraintBaseline_toBaselineOf="@id/post_type" />

</android.support.constraint.ConstraintLayout>
1个回答

18
  1. 如果您使用 paddingStart,则不需要 paddingLeft
  2. app:layout_constraintLeft_toRightOf="@id/poster_profile_pic" 这里没有 id 为 poster_profile_pic 的图片视图
  3. android:layout_marginBottom 只有包括 constraintBottom_toBottomOf="parent" 才能起作用
  4. android:layout_marginRight 应该在 poster_name 中更改为 android:layout_marginLeft,因为 poster_name 具有 app:layout_constraintLeft_toRightOf="@id/post_type"

6
亲爱的amorenew,根据代码片段,您发布的问题是正确的,但在实际代码中不存在。这是因为我试图隔离一种情况并删除了周围的代码。然而,您提到的第三点很有帮助。似乎要在“ConstraintLayout”中应用边距,必须在该方向上存在约束条件。因此,只有在该元素左侧存在约束条件时才会应用左边距。这不包括来自其他元素的该侧约束条件,例如“layout_constraintRight_toLeftOf”将不起作用。 - Matias Grioni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接