简体   繁体   English

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

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

I want to create a layout like this, but I don't know how to place the CardView background under the ImageView and then set TextView below the image.我想创建这样的布局,但我不知道如何将 CardView 背景放在 ImageView 下,然后在图像下方设置 TextView。 The image should have 300dp height and 200dp width.图片的高度应为 300dp,宽度应为 200dp。

在此处输入图像描述

Constrain the bottom and top of your first card with the top of the second CardView, this allows you to align the center of the first card view with the top of the second CardView.用第二个 CardView 的顶部约束第一张卡片的底部和顶部,这允许您将第一张卡片视图的中心与第二个 CardView 的顶部对齐。 and so that the First CradView is above just add an elevation higher than that of the card below并让第一个 CradView 在上面只需添加一个比下面的卡片高的高度

for example:例如:

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

this is the output这是 output

在此处输入图像描述

you must use app:cardElevation attr for card view and use android:elevation attr for other views for do that您必须使用app:cardElevation attr 进行卡片视图,并使用android:elevation attr 进行其他视图

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

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