android - ConstraintLayout 内的 ImageView 不起作用

标签 android xml layout android-constraintlayout

我在正确显示 ImageView 时遇到问题。我想在 ConstraintLayout 中显示 ImageView。在预览时它看起来完全符合我的需要,但是当我在设备上启动它时它看起来完全不一样。此布局位于回收 View 内。这段代码有什么问题?

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 创建行。每行必须包含图像、标题和描述。标题必须位于图片的左下角。说明必须在图片下方。之后我必须将渐变(底部的黑色)放入图像中。带有“预览”的屏幕正是我所需要的。

EDIT2:使用此布局的一切都可以。它按预期工作,我忘记了我对 kotlin 代码进行了一些更改...对问题深表歉意。

最佳答案

首先,每个 View 都应应用其父级 ViewGroup 的属性规则。 ConstraintLayout 不支持 match_parent。它支持 0dp 值,这意味着“匹配约束”。这样 View 将扩展以填充约束有界空间。

接下来,创建了 ConstraintLayout 以实现平面 View 层次结构以获得更好的布局性能。因此,永远不要将它嵌套在 LinearLayout 中,因为它具有链功能,可以以更灵活的方式获得相同的行为。另外,您可以在顶层使用 ConstraintLayout 实现结构。

另一件事,如果你要在所有方向上定义相同的边距,你可以只使用layout_margin

最后,你有 overdraw问题。 ConstraintLayout 足够灵活,允许我们将 View 定位为背景并帮助我们避免背景重叠。

这是一个解决方案:

<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>

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

关于android - ConstraintLayout 内的 ImageView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51767791/

相关文章:

ruby-on-rails - 为什么 `layout nil` 不起作用?

android - Robospice - 在更改 Activity 时保持 spice 服务继续运行

java - 如何使用 Python 生成 AST(以 XML 表示)的控制流?

c# - 确定数据表中列的字段长度

java - 带有重复标签的 XPath 和 Java

html - 当我放大屏幕时,让 Twitter-Bootstrap 的缩略图在响应式网格布局中水平(横向)布局

java - 以 50 的倍数自动调整 JPanel 的大小

PHP base64_decode 保存损坏的图像

android - Android OpenGL 多重采样/抗锯齿问题

android - 安排 JobService 仅在应用程序运行时运行(在前台)