在 android 中创建 cardview 视图

create view over cardview in android

我想创建这个布局

这是我使用此代码的卡片视图(灰色视图)和图像视图(蓝色视图)

<FrameLayout 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:background="@color/abc_search_url_text_normal"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:layout_marginBottom="3dp"
    android:paddingTop="5dp"
    app:cardBackgroundColor="@color/abc_input_method_navigation_guard">


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

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="bottom"
    android:layout_marginBottom="30dp"
    android:layout_marginRight="10dp"
    android:scaleType="fitXY"
    android:src="@drawable/abc_ratingbar_full_material" />

</FrameLayout>

但结果我的图像视图转到了 cardview 的后面,我尝试使用 reletivelayout 而不是 framelayout,但结果相同。

尝试:

  • 使用相对布局

  • 将 ImageView 与父级对齐 right/end 并添加 right/end 边距

  • 将 FAB 对齐到卡片视图的顶部并添加负边距


<RelativeLayout
    ...>
    <CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    />

   <ImageView
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/cardView"
    android:layout_marginTop="-32dp"
    android:layout_marginRight="20dp"
   />

</RelativeLayout>

CardView 的默认高度为 API 21+,您可以将高度设置为低于 ImageView

    <android.support.v7.widget.CardView  xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:cardElevation="1dp"/>

 <ImageView
        android:id="@+id/image_view_user"
        android:layout_width="48dp"
        android:layout_height="48dp"
         android:elevation="2dp"/>

在 Pre 21 中你可以使用

overlayView.bringToFront();
root.requestLayout();
root.invalidate();

如果您有不同的高度,这将不适用于 21 岁以上

只需将 CardView 放在其他视图中,就像在 FrameLayout 中一样。 如下所示:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginStart="10sp"
android:layout_marginEnd="10sp"
>
<FrameLayout
    android:layout_marginTop="15sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="100sp"
        app:cardCornerRadius="5sp"
        app:cardBackgroundColor="@color/colorPrimary">
    <!--other view items -->
    </android.support.v7.widget.CardView>
</FrameLayout>
     <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="90sp"
        android:layout_height="90sp"
        app:civ_border_width="2dp"
        android:layout_marginStart="10sp"
        app:civ_border_color="@color/colorPrimaryDark"
        android:id="@+id/profileImg"
        android:background="@drawable/ic_if_profle_1055000"
        xmlns:app="http://schemas.android.com/apk/res-auto"/>

截图:

另一种解决方法是将图像视图包装在卡片视图中。只需确保背景是透明的,高度和宽度环绕内容,它就会位于前一个卡片视图之上。

您可以将 CardView 包裹在 FrameLayout 中,但阴影将被裁剪掉。 设置 android:clipChildren="false" 修复它

<android.support.constraint.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="wrap_content"
    android:clipChildren="false"
    android:layout_height="140dp">

<FrameLayout
    android:id="@+id/frameLayout4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:clipChildren="false">

    <android.support.v7.widget.CardView
        app:cardCornerRadius="@dimen/card_radius"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.CardView>
</FrameLayout>
</android.support.constraint.ConstraintLayout>

只需将 ImageView 的高度添加到大于 CardView 的高度

<FrameLayout 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:background="@color/abc_search_url_text_normal"
android:orientation="vertical">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:layout_marginBottom="3dp"
    android:paddingTop="5dp"
    app:cardBackgroundColor="@color/white">


</androidx.cardview.widget.CardView>

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="bottom"
    android:layout_marginBottom="30dp"
    android:layout_marginRight="10dp"
    android:scaleType="fitXY"
    android:elevation="8dp"
    android:src="@drawable/ic_launcher_background" />