create view over cardview in android

北慕城南 提交于 2019-12-04 23:33:42

Try:

  • Use a RelativeLayout

  • Align the ImageView to the parent right/end and add a right/end margin

  • Align the FAB to the top of the card view and add a negative margin


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

The CardView has by default elevation in API 21+, you can manipulate the elevation to be less than that of the 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"/>

in Pre 21 you can use

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

This will not work in 21+ if you have different elevation

Just put CardView in other view like in FrameLayout. Just like below:

<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"/>

Screenshot:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!