【问题标题】:Android ConstraintLayout Margins not working correctlyAndroid ConstraintLayout Margins 无法正常工作
【发布时间】:2018-01-19 07:36:19
【问题描述】:

如图所示,视图框在当前位置呈现,但内容不是。文本视图和图像视图被渲染为好像没有任何边距。当我运行应用程序时,它们也没有正确渲染(第二张图片)。我不知道是什么原因造成的。

编辑:我忘记包含我的 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>

【问题讨论】:

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


    【解决方案1】:

    确保在设置开始和结束边距之前设置开始和结束约束。我遇到了同样的问题,设置结束约束后它工作了。

    【讨论】:

      【解决方案2】:

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

      明确的问题是您的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 属性)。

      【讨论】:

      • 边距不是手动设置的,而是由设计视图设置的。无论如何我会试试的,谢谢
      • 谢谢,这就是问题所在。我想我没有看到它,因为我通常使用 css 和 html,这确实有效。
      • 你知道我可以在哪里报告这个错误(因为编辑器显然没有正确呈现它)
      • 您不需要同时添加开始/结束和左/右页边距。 android:layout_marginStart 和 android:layout_marginEnd 就足够了。
      • @BenP。首先,我认为目前没有太多理由支持 API 17 之前的设备,因为这不到 2% 的用户。但是,如果出于某种原因您确实想要,我建议您定义一个单独的布局文件版本,并将带有 layout_marginStart 和 End 的版本粘贴在 values-v17 文件夹中。这将使事情变得不那么混乱,并防止任何潜在的冲突。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 2012-09-13
      • 2012-03-24
      • 2012-11-03
      • 2013-11-24
      • 1970-01-01
      • 2014-09-17
      相关资源
      最近更新 更多