我正在尝试使用约束布局创建一个布局,在顶部有一个 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 顶部和卡片之间的边距,这让我很困扰。
最佳答案
您的 news_image
、news_title1
和 news_read_more1
View 形成一条链。显然,虽然我找不到这方面的文档,但垂直链中的所有 View 都将共享垂直边距。换句话说,将 layout_marginTop
或 layout_marginBottom
属性应用到这三个 View 之一,最终会将其应用到所有这三个 View 。
记住这一点,您将不得不重新分配利润。
编辑
看起来这种行为比我想象的要复杂一些。首先,它看起来只适用于具有 spread
“链式”(默认)的 View 。其次,看起来上边距应用于它指定的 View 以及链中那个上方的所有 View ,而下边距应用于它指定的 View 以及链中那个下方的所有 View 。最后,边距似乎是累积的(所以如果你的底 View 有 10dp 的顶部边距,中间 View 有 20dp 的顶部边距,那么最终结果将是顶 View 30dp,中间 View 30dp,你的 10dp底 View )。
至于在您的具体情况下如何解决这个问题?您应该能够毫无问题地使用左/右页边距。然后可能您应该在顶 View 上使用底部边距来分隔三个 View 。
编辑#2
Muthukrishnan 的回答让我意识到您可以通过简单地从 View 中删除链来解决这个问题。如果您从 ImageView 中删除 app:layout_constraintBottom_toTopOf="@+id/news_title1"
约束,这将打破链条,现在垂直边距不再共享。
关于android - 约束布局获取 ImageView 的默认上边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45554511/