繁体   English   中英

如何在 ConstraintLayout 中将 ImageView 放在 CardView 之上

[英]How to put ImageView on top of CardView in ConstraintLayout

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

在此处输入图像描述

用第二个 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>

这是 output

在此处输入图像描述

您必须使用app:cardElevation attr 进行卡片视图,并使用android:elevation attr 进行其他视图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM