如何将ImageView置于CardView之上?请查看详细信息。

14

我正在开发一个应用程序,已经制作了如下所示的布局:

带有头像、名称和 Twitter 句柄的屏幕截图,其中名称和 Twitter 面板覆盖了部分头像

布局中包含 CircleImageViewCardView

以下是 XML 代码:

<RelativeLayout
    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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.playrapp.playr.Profile"
    tools:showIn="@layout/activity_profile">

    <android.support.v7.widget.CardView
        android:id="@+id/profileCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:contentPadding="10dp">

    </android.support.v7.widget.CardView>

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/userImageProfile"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="50dp"
        android:src="@mipmap/ic_launcher"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>
在这里,CardViewImageView上方。 我想让ImageViewCardView上方。 我该怎么做?
请告诉我。

使用虚拟LinearLayout包装它。 - AbdulMomen عبدالمؤمن
3个回答

42

CardView 的高度比 CircleImageView 高,所以你需要将 CircleImageView 的高度设置得更高,以使其显示在 CardView 之上。

CardView 的高度设置为 app:cardElevation="4dp",将 CircleImageView 的高度设置为 android:elevation="8dp"

这里是可用的 XML 代码。请尝试:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.CardView
        android:id="@+id/profileCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="16dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp"
        app:cardUseCompatPadding="false" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center_horizontal"
            android:layout_marginTop="75dp"
            android:padding="24dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hammad Nasir"
                android:textSize="32sp"
                android:textColor="#727272"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="\@hammadnasir"
                android:textSize="24sp"
                android:textColor="#727272" />

        </LinearLayout>
    </android.support.v7.widget.CardView>


    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/userImageProfile"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="75dp"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/ic_launcher"
        android:elevation="8dp" />

</RelativeLayout>

输出:

enter image description here

希望这能有所帮助~


2

CardView在XML文件中默认设置了一个高度:app:elevation="0dp"


2
这个做到了效果,但是CardView看起来不再像一张卡片了! :/ - Hammad Nasir
你是什么意思?你能分享一张截图吗? - Katharina
只有一个问题 - 为什么你想把图片放在卡片视图的顶部?你不能把它放在里面吗? - Katharina
默认高度为2dp,通过将ImageView的高度设置为3dp,我得到了想要的结果。你能告诉我如何在API级别低于21的情况下指定“android:elevation =”“吗? - Hammad Nasir
你不能在API 21以下的视图中设置高度...只有CardView可以,因为它是支持库的一部分。这就是为什么我建议将图像放在CardView中的原因。 - Katharina
显示剩余4条评论

0

CardView -> app:cardElevation ="4dp" ImageView -> android:translationZ="6dp"

CardView -> app:cardElevation ="4dp" ImageView -> android:translationZ="6dp"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接