约束布局获取图像视图的默认上边距

Constraint Layout Getting a default top margin for imageview

我正在尝试使用约束布局创建一个布局,上面有一个 ImageView,ImageView 中有一个按钮,下面是一个 TextView,然后下面是另一个 TextView。以下是 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    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:layout_margin="10dp"
    app:cardElevation="4dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/news_image1"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="16:9"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="0dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintBottom_toTopOf="@+id/news_title1"/>
            <ImageButton
                android:id="@+id/news_share_button_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/share_button_big"
                app:layout_constraintBottom_toBottomOf="@+id/news_image1"
                app:layout_constraintRight_toRightOf="@+id/news_image1"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"/>
            <TextView
                android:id="@+id/news_title1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:gravity="center_vertical|start"
                android:layout_alignParentBottom="true"
                android:lines="3"
                android:ellipsize="end"
                app:layout_constraintTop_toBottomOf="@+id/news_image1"
                app:layout_constraintBottom_toTopOf="@+id/news_read_more1"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:textColor="@color/colorPrimaryText"
                android:layout_margin="@dimen/news_cell0_textview_margin"
                android:textSize="12sp"
                android:typeface="monospace" />
            <TextView
                android:id="@+id/news_read_more1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:gravity="center_vertical|start"
                android:layout_alignParentBottom="true"
                android:lines="1"
                android:ellipsize="end"
                app:layout_constraintTop_toBottomOf="@+id/news_title1"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                android:textColor="@color/read_more_text_color"
                android:text="@string/news_read_more"
                android:layout_margin="@dimen/news_cell0_textview_margin"
                android:textSize="10sp" />
        </android.support.constraint.ConstraintLayout>
    </RelativeLayout>
</android.support.v7.widget.CardView>

除了第一个 ImageView 顶部的小边距外,一切都正确。无论我做什么,我都无法摆脱那个边缘。见下图。

请注意 ImageView 顶部和卡片之间的边距,这让我很困扰。

试试这个,我在你的布局中删除了 app:layout_constraintTop_toTopOf="parent" 它有效

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    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:layout_margin="10dp"
    app:cardElevation="4dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white">
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/news_image1"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="16:9"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"/>
            <ImageButton
                android:id="@+id/news_share_button_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_menu_share"
                app:layout_constraintBottom_toBottomOf="@+id/news_image1"
                app:layout_constraintRight_toRightOf="@+id/news_image1"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"/>
            <AliasTextView
                android:id="@+id/news_title1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:gravity="center_vertical|start"
                android:layout_alignParentBottom="true"
                android:lines="3"
                android:ellipsize="end"
                app:layout_constraintTop_toBottomOf="@+id/news_image1"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:textColor="@color/colorPrimaryText"
                android:layout_margin="@dimen/news_cell0_textview_margin"
                android:textSize="12sp"
                android:typeface="monospace" />
            <TextView
                android:id="@+id/news_read_more1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:gravity="center_vertical|start"
                android:layout_alignParentBottom="true"
                android:lines="1"
                android:ellipsize="end"
                app:layout_constraintTop_toBottomOf="@+id/news_title1"
                app:layout_constraintLeft_toLeftOf="parent"
                android:textColor="@color/read_more_text_color"
                android:text="@string/news_read_more"
                android:layout_margin="@dimen/news_cell0_textview_margin"
                android:textSize="10sp" />
        </android.support.constraint.ConstraintLayout>
    </RelativeLayout>
</android.support.v7.widget.CardView>

您的 news_imagenews_title1news_read_more1 视图形成一条链。显然,虽然我找不到这方面的文档,但垂直链中的所有视图都将共享垂直边距。换句话说,将 layout_marginToplayout_marginBottom 属性应用于这三个视图之一,最终会将其应用于 所有三个视图 .

记住这一点,您将不得不重新分配利润。

编辑

看起来行为比我想象的要复杂一些。首先,它看起来只适用于带有 spread "chain style" 的视图(这是默认值)。其次,它看起来像上边距应用于它指定的视图以及链中那个 以上 的所有视图,而下边距应用于它指定的视图以及链中该视图 下方 的所有视图。最后,边距似乎是累积的(所以如果你的底视图有 10dp 的顶部边距,中间视图有 20dp 的顶部边距,那么最终结果将是顶视图 30dp,中间视图 30dp,你的 10dp底部视图)。

至于你的具体情况是如何解决这个问题的?您应该可以毫无问题地使用 left/right 页边距。然后可能你应该在顶视图上使用底部边距 space 三个视图。

编辑#2

Muthukrishnan 的回答让我意识到您可以通过简单地从视图中删除链来解决这个问题。如果您从 ImageView 中删除 app:layout_constraintBottom_toTopOf="@+id/news_title1" 约束,这将打破链条,现在垂直边距不共享。

感谢 Ben P 的出色入门,我设法找到了解决方案。 ConstraintLayout 中提供了三种(加上一种加权的)链接样式。根据 this great tutorial,链接样式解释为:

  1. Spread:浏览量分布均匀。例如。 app:layout_constraintHorizontal_chainStyle=”spread” app:layout_constraintVertical_chainStyle=”spread”
  2. Spread inside: 第一个和最后一个视图被贴在链条两端的约束上,其余均匀分布。例如。 app:layout_constraintHorizontal_chainStyle=”spread_inside” app:layout_constraintVertical_chainStyle=”spread_inside”
  3. Packed:视图被打包在一起(在考虑边距之后)。然后,您可以通过更改链的头部视图偏差来调整整个链的偏差(left/right 或 up/down)。例如。 app:layout_constraintHorizontal_chainStyle=”packed” app:layout_constraintVertical_chainStyle=”packed”
  4. Weighted:当链设置为spread或spread inside时,可以通过设置一个或多个视图为“match constraints”来填充剩余的space (0dp)。默认情况下,space 在设置为 "match constraints," 的每个视图之间均匀分布,但您可以使用 thelayout_constraintHorizontal_weightlayout_constraintVertical_weight attributes 为每个视图分配重要性权重。如果您熟悉线性布局中的 layout_weight,则其工作方式相同。所以权重值最高的视图获得最多的space;具有相同权重的视图获得相同数量的 space.

spread 是默认的链接样式。我将其更改为 spread_inside 以便第一个和最后一个视图附加到链的每一端的约束(因此遵守提供的边距)。 xml 现在看起来像:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    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:layout_margin="10dp"
    app:cardElevation="4dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/news_image1"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="16:9"
                android:scaleType="centerCrop"
                android:adjustViewBounds="true"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_marginTop="0dp"
                app:layout_constraintVertical_chainStyle="spread_inside"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintBottom_toTopOf="@+id/news_title1"/>
            <ImageButton
                android:id="@+id/news_share_button_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/share_button_big"
                app:layout_constraintBottom_toBottomOf="@+id/news_image1"
                app:layout_constraintRight_toRightOf="@+id/news_image1"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"/>
            <TextView
                android:id="@+id/news_title1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:gravity="center_vertical|start"
                android:layout_alignParentBottom="true"
                android:lines="3"
                android:ellipsize="end"
                app:layout_constraintVertical_chainStyle="spread_inside"
                app:layout_constraintTop_toBottomOf="@+id/news_image1"
                app:layout_constraintBottom_toTopOf="@+id/news_read_more1"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:textColor="@color/colorPrimaryText"
                android:layout_margin="@dimen/news_cell0_textview_margin"
                android:textSize="12sp"
                android:typeface="monospace" />
            <TextView
                android:id="@+id/news_read_more1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:gravity="center_vertical|start"
                android:layout_alignParentBottom="true"
                android:lines="1"
                android:ellipsize="end"
                app:layout_constraintVertical_chainStyle="spread_inside"
                app:layout_constraintTop_toBottomOf="@+id/news_title1"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                android:textColor="@color/read_more_text_color"
                android:text="@string/news_read_more"
                android:layout_margin="@dimen/news_cell0_textview_margin"
                android:textSize="10sp" />
        </android.support.constraint.ConstraintLayout>
    </RelativeLayout>
</android.support.v7.widget.CardView>