【问题标题】:Constraint Layout Getting a default top margin for imageview约束布局获取图像视图的默认上边距
【发布时间】:2017-08-07 19:38:24
【问题描述】:

我正在尝试使用约束布局创建一个布局,上面有一个 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 顶部和卡片之间的边距,这就是困扰我的地方。

【问题讨论】:

  • 你确定图片没有那个边距吗?

标签: android android-layout android-imageview android-constraintlayout


【解决方案1】:

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

您必须重新分配利润,记住这一点。

编辑

看起来这种行为比我想象的要复杂一些。首先,它看起来只适用于具有spread“链样式”(这是默认值)的视图。其次,看起来上边距应用于它指定的视图以及链中高于的所有视图,而下边距应用于它指定的视图以及链中的所有视图低于。最后,边距似乎是累积的(因此,如果您的底部视图上边距为 10dp,中间视图上边距为 20dp,最终结果将是顶部视图上的 30dp,中间视图上的 30dp,以及中间视图上的 10dp底视图)。

至于在您的具体情况下如何解决这个问题?您应该能够毫无问题地使用左/右页边距。然后可能您应该在顶视图上使用底部边距来分隔三个视图。

编辑#2

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

【讨论】:

  • 太棒了!!!这确实是一个首发,我牺牲了所有的垂直边距,我得到了 Image 的上边距。这个问题有什么合适的解决办法吗?
  • @HarikrishnanT 我已经用更多信息更新了我的答案。希望这足以让您能够从这里获取它。祝你好运。
  • Muthukrishnan 的回答足以解决我的问题,除了一个地方。通过删除该约束来打破链甚至会从底部 TextView 中删除底部边距。为第一个视图添加底部边距是一个更好的解决方案。它解决了我的问题,但是在其他情况下,我需要更多地使用边距。因此,请检查其他可能的方法来打破链条。感谢伟大的首发。 :)
  • 感谢伟大的初学者,我设法解决了这个问题。我只需要将默认链样式(如您提到的 spread)更改为 spread_inside
【解决方案2】:

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

  1. 传播:观看次数均匀分布。例如。 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. 打包:视图打包在一起(在考虑边距之后)。然后,您可以通过更改链条的头部视图偏差来调整整个链条的偏差(左/右或上/下)。例如。 app:layout_constraintHorizontal_chainStyle=”packed” app:layout_constraintVertical_chainStyle=”packed”
  4. 加权:当链设置为展开或向内展开时,您可以通过将一个或多个视图设置为“匹配约束”(0dp) 来填充剩余空间。默认情况下,空间在设置为“匹配约束”的每个视图之间均匀分布,但您可以使用thelayout_constraintHorizontal_weightlayout_constraintVertical_weight attributes 为每个视图分配重要性权重。如果您熟悉线性布局中的layout_weight,则其工作方式相同。所以权重值最高的视图获得了最多的空间;具有相同权重的视图获得相同数量的空间。

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>

【讨论】:

    【解决方案3】:

    试试这个,我在你的布局中删除 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>
    

    【讨论】:

    • 它没有帮助。另外我需要图像是 centerCrop,不希望它缩放。
    • app:layout_constraintDimensionRatio="16:9" 这对你来说是强制性的吗..?
    • 是的,这就是我选择使用 ConstraintLayout 的主要原因。
    • 这实际上解决了问题,但现在最底部的TextView的底部边距也被删除了。
    • removed 表示,您看不到该文本。?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2020-02-05
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多