【问题标题】:How to put ImageView on top of CardView in ConstraintLayout如何在 ConstraintLayout 中将 ImageView 放在 CardView 之上
【发布时间】:2022-04-26 21:50:47
【问题描述】:

我想创建一个这样的布局,但我不知道如何将 CardView 背景放置在 ImageView 下,然后将 TextView 设置在图像下方。图片的高度应为 300dp,宽度为 200dp。

【问题讨论】:

  • 因为那张图片不是卡片视图的一部分,它只是在它上面延伸。
  • 使用另一张卡片放置图像视图并为此设置更高的高度。
  • 我知道它不是 cardview 的一部分。如何使用 textView 为 cardview 设置约束,并为图像设置另一个 cardview。它应该与上面的示例重叠。提前致谢。

标签: java android android-constraintlayout


【解决方案1】:

用第二个 CardView 的顶部约束第一张卡片的底部和顶部,这允许您将第一张卡片视图的中心与第二个 CardView 的顶部对齐。 为了让第一个 CradView 位于上方,只需添加比下方卡片更高的高度

例如:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <androidx.cardview.widget.CardView
        app:cardCornerRadius="15dp"
        android:id="@+id/bottomCard"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_margin="20dp"
        app:cardElevation="9dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias=".7">

    </androidx.cardview.widget.CardView>


    <androidx.cardview.widget.CardView
        app:cardElevation="10dp"

        app:cardBackgroundColor="@color/purple_700"
        app:cardCornerRadius="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/bottomCard"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/bottomCard">

        <ImageView
            android:id="@+id/image"
            android:layout_width="250dp"
            android:layout_height="300dp"
            android:background="@drawable/ic_android_black_24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>

这是输出

【讨论】:

  • 问这个问题的人告诉我这就是我要找的东西!。但是无论谁投票给我,请向我解释为什么?或提出改进意见
【解决方案2】:

您必须使用app:cardElevation attr 来查看卡片,并使用android:elevation attr 来查看其他视图

【讨论】:

    猜你喜欢
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2017-11-14
    相关资源
    最近更新 更多