如何将 circleimageview 保持在 CardView 之前?在约束布局中?

问题描述

我想以 CircleImageView 领先于 CardView 的方式显示输出

Enter Image Description

但我的输出看起来像这样

enter image description here

这是我想与 RecylerView 一起使用的单行 XML

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


<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/circleImageView"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:src="@drawable/cat_placeholder"
    app:civ_border_color="@color/red"
    app:civ_border_width="2dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.cardview.widget.CardView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="32dp"
    app:cardElevation="50dp"
    app:layout_constraintBottom_toBottomOf="@+id/circleImageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/circleImageView"
    app:layout_constraintTop_toTopOf="@+id/circleImageView" />


    </androidx.constraintlayout.widget.ConstraintLayout>

解决方法

考虑将您的 CardViewCircleImageView 包装在 FrameLayout 中。此外,按照您希望它们的显示方式为这两个视图设置 android:elevation 属性。

<FrameLayout
    android:id="@+id/some_container"
    android:layout_width="..."
    android:layout_height="..."
    ... >

    <CardView
        android:id:"@+id/card"
        android:layout_width="..."
        android:layout_height="..."
        android:elevation="..."
        ...
        ... />

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/circleImageView"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:src="@drawable/cat_placeholder"
        android:elevation="..."
        ...
        ... />

</FrameLayout>

不要忘记查看 FrameLayout documentation 以了解如何最好地使用它。 (另外,请记住 CardView 本身就是一个 FrameLayout。)


此外,我建议您注意项目的 minSdkVersionelevation 属性在 API 21+ 中可用。您的应用程序不会在较低 API 设备中崩溃,但您可能无法获得所需的外观。强烈考虑使用 FrameLayout 来实现重叠,或者尝试设计自定义视图或使用一些库,例如用于模拟高程的阴影。

,

android:elevation="55dp" 添加到您的 imageView

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...