Android ConstraintLayout 边距无法正常工作

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

如图所示, View 框在当前位置呈现,但内容不是。 TextView 和 ImageView 都呈现为没有任何边距。当我运行该应用程序时,它们也无法正确呈现(第二张图片)。我不知道是什么原因造成的。 Editor Preview Live Demo

编辑:我忘了包含我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<com.example.android.stundenplanexample.dynamic_recyclerview.ExpandingCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="2dp"
    android:layout_margin="12dp">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_margin="8dp"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_weight="0"
            android:src="@drawable/ic_expand_more_black_24dp"
            android:id="@+id/card_expand_toggle"
            tools:ignore="ContentDescription"
            android:layout_marginTop="16dp"
            card_view:layout_constraintTop_toTopOf="parent"
            card_view:layout_constraintRight_toRightOf="parent"
            android:layout_marginEnd="24dp" />

        <TextView
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:textAppearance="@android:style/TextAppearance.Material.Medium"
            android:textColor="?android:attr/textColorPrimary"
            android:layout_width="42dp"
            android:layout_height="wrap_content"
            android:text="@string/card_expand_string"
            android:id="@+id/textView3"
            card_view:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="16dp"
            card_view:layout_constraintLeft_toLeftOf="parent"
            android:layout_marginTop="16dp" />

        <TextView
            android:layout_height="wrap_content"
            android:id="@+id/extra_information"
            android:layout_width="wrap_content"
            tools:text="ToleToleToleInfo"
            card_view:layout_constraintLeft_toLeftOf="@+id/textView3"
            android:layout_marginTop="8dp"
            card_view:layout_constraintTop_toBottomOf="@+id/textView3"
            card_view:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="16dp" />
    </android.support.constraint.ConstraintLayout>

</com.example.android.stundenplanexample.dynamic_recyclerview.ExpandingCardView>

最佳答案

我看到了一个确定的问题和一个潜在的问题。

确定的问题是您的 textView3 指定了这些属性:

android:layout_margin="8dp"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"

这不会将顶部/开始设置为 16dp,将结束/底部设置为 8dp。它只会导致 marginTopmarginStart 属性被忽略。

潜在的问题是你指定

android:layout_marginStart="16dp"

但不要同时指定 marginLeft。如果要支持 API 17 之前的设备,则需要同时指定 marginStartmarginLeft(假设您删除了 margin 属性)。

关于Android ConstraintLayout 边距无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624112/

相关文章:

java - 在 Docker 容器内运行时偶尔会出现 Robolectric 测试失败

android - android studio中layout和layout v17的区别

android-studio - Lambda导致意外 token

android - 如何在不反转所选项目的情况下从左到右反转微调器?

java - 将 GLSurfaceView 类与 android xml 布局一起使用

android - LinearLayout 中的 ConstraintLayout 不起作用

java - 在 Android Studio 中从 POST 获取响应代码 400

java - 如何定义 Recycler.Adapter<VH extends RecylerView.ViewHolder> 的子类

android - 查找用户停放汽车的位置 Android

android - ListView中滚动时调用哪个函数