【问题标题】:ConstraintLayout margins约束布局边距
【发布时间】:2017-11-07 20:15:42
【问题描述】:

我有一个简单的布局。

<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/colorDarkGray">


    <RelativeLayout
        android:id="@+id/container3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:alpha="0.7"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Another text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Some text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

在设计视图中,它看起来如此 如您所见,没有左右边距。顶部和底部边距效果很好。如果我删除左边距或右边距,我的相对布局会稍微超出屏幕。 我想我完全可以在没有相对布局的情况下做到这一点,但我很感兴趣为什么会这样。

【问题讨论】:

  • 只需设置RelativeLayout宽度以匹配父级(0dp)
  • 这恰好导致您将 textView 与父级左右对齐,并且您的 RelativeLayout 是 wrap_content

标签: android android-constraintlayout


【解决方案1】:

您应该避免在 ConstraintLayout 中使用嵌套视图。原因是:

结果布局源应如下所示:

<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:background="@color/colorDarkGray">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:alpha="0.7"
        android:fontFamily="sans-serif"
        android:letterSpacing="0.03"
        android:text="Another text"
        android:textColor="@color/colorWhite"
        android:textSize="11sp"
        android:textStyle="normal"
        android:id="@+id/textView"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/textView2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="end"
        android:fontFamily="sans-serif"
        android:letterSpacing="0.03"
        android:text="Some text"
        android:textColor="@color/colorWhite"
        android:textSize="11sp"
        android:textStyle="normal"
        android:id="@+id/textView2"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

您可以按照Google's Codelab 中的步骤学习如何使用 ConstraintLayouts 构建布局。

【讨论】:

  • 感谢您,android:layout_width="0dp" 和 android:layout_height="wrap_content"(在 TextView 上)解决了我在 ConstraintLayout 中的 TextView 扩展以填充宽度而忽略其边距的问题。你知道这是在哪里记录的吗?
【解决方案2】:

尝试将您的RelativeLayout 宽度更改为android:layout_width="0dp"

<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/colorDarkGray">


    <RelativeLayout
        android:id="@+id/container3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:alpha="0.7"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Another text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Some text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

【讨论】:

  • ConstraintLayout 没有得到 match_parent。您必须将宽度设置为“0dp”
  • 这感觉违背了约束布局的目的
【解决方案3】:

是的,当您如下定义 RelativeLayout 时,您的布局中当然没有左右边距:

<RelativeLayout
    android:id="@+id/container3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"

这意味着RelativeLayout 具有wrap_content 的宽度和高度,并且它位于Parent 的中心(使用我们的constraintTop,Bot....)。

如何解决?只需将您的相对布局的width 设置为0dp。根据constraints,这意味着您的RelativeLayout 将与父级一样宽。

【讨论】:

    猜你喜欢
    • 2020-02-05
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多