约束布局中的ImageView无法工作

4

我在正确显示 ImageView 上遇到了问题。我想在 ConstraintLayout 中显示 ImageView。在预览中,它看起来正是我需要的样子,但是当我在设备上启动它时,它看起来完全不同。这个布局放置在回收视图中。这段代码有什么问题吗?

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/promotionRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:background="#fff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<android.support.constraint.ConstraintLayout
    android:id="@+id/promotionImageLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintHeight_default="spread"
    android:background="@color/colorPrimary">
    <ImageView
        android:id="@+id/promotionImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_start_promotion"
        android:background="@mipmap/ic_start_promotion"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHeight_min="150dp" />
    <ImageView
        android:id="@+id/fadeGradientImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@drawable/fade_image_background" />
    <TextView
        android:text="Sample title"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="6dp"
        android:textColor="#ffffff"
        android:id="@+id/promotionNameTextView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:paddingBottom="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
<TextView
    android:id="@+id/promotionDescriptionTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="13sp"
    android:layout_marginTop="12dp"
    android:layout_marginStart="12dp"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:layout_marginEnd="12dp"
    android:layout_marginBottom="12dp"
    android:text="Sampe description" />
</LinearLayout>

编辑:深入解释:

我想为RecycleView创建一行。每一行必须包含图像、标题和描述。标题必须在图像的左下角。描述必须在图像下方。之后,我必须将渐变(底部为黑色)放入图像中。屏幕上的“预览”正是我需要的。

编辑2:这个布局的一切都没问题。它按预期工作,我忘记了我在kotlin代码中做了一些更改... 抱歉造成困扰。


1
为什么在 ConstraintLayout 中使用 LinearLayout?因为 ConstraintLayout 提供了线性布局和相对布局两种功能。 - user4571931
我认为你需要使用FrameLayout来处理这种情况。 - Ishan Fernando
2个回答

5
首先,每个视图都应该应用其父ViewGroup的属性规则。ConstraintLayout不支持match_parent。它支持0dp值,表示“匹配约束条件”。这样,视图将扩展以填充约束边界空间。
接下来,ConstraintLayout是为了实现更好的布局性能而创建的扁平化视图层次结构。因此,永远不要将其嵌套在LinearLayout中,因为它具有链特性,可以以更灵活的方式获得相同的行为。此外,您可以在顶层使用ConstraintLayout来实现结构。
另一件事,如果要在所有方向上定义相同的边距,只需使用layout_margin即可。
最后,您可能会遇到过度绘制问题。 ConstraintLayout足够灵活,可以使我们将视图定位为背景,并帮助我们避免重叠的背景。
以下是解决方案:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/promotionRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/promotion_image"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/promotion_image"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_start_promotion"
        app:layout_constraintHeight_min="150dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/shadow"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:adjustViewBounds="true"
        android:background="@drawable/fade_image_background"
        app:layout_constraintBottom_toBottomOf="@+id/promotion_image"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/promotion_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Sample title"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/promotion_image"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:padding="12dp"
        android:text="Sampe description"
        android:textSize="13sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/promotion_image" />

</android.support.constraint.ConstraintLayout>

试一下。希望这有所帮助!


这个答案确实帮了我很多。谢谢! - Jacob Hatwell

0
首先,试着更精确地描述你想要什么?在布局中显示图像视图是一些常见的词语。至于你的代码,除了你没有任何限制之外。你有一个奇怪的视图高度,对于第二个ImageView:
android:layout_height="match_parent"

它可能会覆盖所有其他子视图,这是一个非常奇怪的参数。


我添加了这个参数,因为我想先显示第一个ImageView(这张图片应该决定布局的高度),然后在其上放置另一个带有渐变遮罩的ImageView。 - Patryk kowalski
好的,那么正如我所说,请更精确地描述您想要什么?还有什么问题吗?很抱歉,我无法从您的帖子中理解。 - Stanislav Batura
修改了第一篇帖子,这样足够了吗?还是我需要写更多内容? - Patryk kowalski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接