ConstraintLayout忽略了边距。

8

我的布局非常简单,可以用一个RelativeLayout替换,这样会一直正常工作。但是无论我在哪里放置边距,它们都被忽略了,不,我不想使用填充,链也不能解决问题。有什么建议吗?

例如,日期或制造商的边距以及其他边距都被忽略了。

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:padding="8dp"
    app:cardBackgroundColor="?attr/szykColorSecondary"
    app:cardCornerRadius="8dp"
    app:cardElevation="4dp">

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

        <TextView
            android:id="@+id/date"
            style="?attr/szyk_textMedium"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:gravity="left"
            android:singleLine="true"
            android:text="16.12.2014"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/manufacturer"
            style="?attr/szyk_textBody"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:paddingRight="8dp"
            android:text="Samsung"
            android:textAllCaps="true"
            android:textSize="24sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/model"
            app:layout_constraintTop_toBottomOf="@+id/date" />

        <TextView
            android:id="@+id/model"
            style="?attr/szyk_textBody"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="S6 edge"
            android:textAllCaps="true"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="@+id/manufacturer"
            app:layout_constraintLeft_toRightOf="@+id/manufacturer"
            app:layout_constraintRight_toLeftOf="@+id/delete"
            app:layout_constraintTop_toTopOf="@+id/manufacturer" />

        <ImageView
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_vector_delete"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/serial"
            style="?attr/szyk_textMedium"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="FDSF6D7A8FDAS6F7D89AS"
            android:textSize="12sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/manufacturer" />

        <TextView
            android:id="@+id/restore"
            style="?attr/szyk_textButton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:text="@string/restore"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/serial" />

    </android.support.constraint.ConstraintLayout>

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

版本

compile 'com.android.support.constraint:constraint-layout:1.0.2'

工具

compileSdkVersion 27
buildToolsVersion '27.0.2'

无论是日期还是制造商,都已经在问题中提到了。 - Szymon Klimaszewski
因为在我的Android Studio中它正在接受边距。 - AskNilesh
哦,奇怪。你使用的是27工具版本吗? - Szymon Klimaszewski
对我来说,它看起来像这样:https://imgur.com/a/GcRnS - Szymon Klimaszewski
@SzymonKlimaszewski 你解决了吗..? - AskNilesh
显示剩余11条评论
2个回答

1
抱歉,我的错误。问题的根本原因是我一直在使用的样式。它定义了android:layout_margin,这将覆盖所有更具体的边距,例如android:layout_marginTop
<item name="android:layout_margin">2dp</item>

与 Android 组件的其余部分一样,组件特定的覆盖会覆盖更一般的内容,例如样式。 - Leo supports Monica Cellio
不,如果您定义了layout_margin,则layout_margin_left|right|top|bottom将被忽略。 - Szymon Klimaszewski
抱歉,这个覆盖问题很荒谬,但你是对的。我编辑了答案以添加一些澄清,并撤销了我的反对票。 - Leo supports Monica Cellio

0
有时候预览显示的结果布局不正确。但是你的日期 TextView 没有包含任何关于底部位置的提及,所以系统不知道你想要从哪个位置开始设置边距。
尝试使用这个编辑过的 XML。
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:padding="8dp"
    app:cardBackgroundColor="@color/white_transparent_50"
    app:cardCornerRadius="8dp"
    app:cardElevation="4dp">

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

        <TextView
            android:id="@+id/date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="16.12.2014"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/manufacturer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:paddingRight="8dp"
            android:text="Samsung"
            android:textAllCaps="true"
            android:textSize="24sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/date" />

        <TextView
            android:id="@+id/model"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="S6 edge"
            android:textAllCaps="true"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="@id/manufacturer"
            app:layout_constraintLeft_toRightOf="@id/manufacturer"
            app:layout_constraintRight_toLeftOf="@id/delete"
            app:layout_constraintTop_toTopOf="@id/manufacturer" />

        <TextView
            android:id="@+id/serial"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="FDSF6D7A8FDAS6F7D89AS"
            android:textSize="12sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/manufacturer" />

        <TextView
            android:id="@+id/restore"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:text="123123123"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/serial" />

    </android.support.constraint.ConstraintLayout>

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

@SzymonKlimaszewski 实际上我已经创建了测试布局,移除了CardView的父类并使用自定义样式,检查了这个xml,边距正常工作。所以你的错误可能不在这个xml中。正如我所说,尝试重新启动Android Studio,有时候会有帮助。 - Eugene Troyanskii
请查看原帖的评论。对于一些人来说,它有效,但对我来说不行。 - Szymon Klimaszewski
@SzymonKlimaszewski 请在启动的应用程序中显示结果视图,而不是在布局构建器中。 - Eugene Troyanskii

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