FrameLayout里有CardView造成的显示顺序问题

2022-03-24  本文已影响0人  a_simple_coder

AndroidFrameLayout(帧布局)默认的 下一个会自动显示在上一个的上面,但是里面有CardView的时候,其他的控件却看不见,例如在需求在CardView外层左上角显示排名,单独放的textview却看不见,布局代码和效果图如下:

<FrameLayout 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_gravity="center"
    android:layout_width="@dimen/dimen_75dp"
    android:layout_height="@dimen/dimen_75dp">

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        app:cardCornerRadius="@dimen/dimen_5dp">

        <ImageView
            android:layout_width="@dimen/dimen_60dp"
            android:layout_height="@dimen/dimen_60dp"
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            tools:src="@drawable/img_camera" />
    </androidx.cardview.widget.CardView>

    <ImageView
        android:layout_width="@dimen/dimen_20dp"
        android:layout_height="@dimen/dimen_20dp"
        android:layout_gravity="top|end"
        android:src="@drawable/icon_delete" />
</FrameLayout>
image.png

一般情况上面布局里的ImageView-delete是可以看见的,因为CardView自动阴影,设置elevation能改变FrameLayout里面显示的顺序,有阴影的时候,将不会遵循默认的自动覆盖逻辑。elevation最大的值会在最上层,因此我们将上面的 ImageView-delete 设置一下elevation或者设置CardViewapp:cardElevation属性为0,就可以解决看不见的问题了,布局代码和效果图如下:

<FrameLayout 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_gravity="center"
    android:layout_width="@dimen/dimen_75dp"
    android:layout_height="@dimen/dimen_75dp">

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        app:cardCornerRadius="@dimen/dimen_5dp"
        app:cardElevation="@dimen/dimen_0dp">

        <ImageView
            android:layout_width="@dimen/dimen_60dp"
            android:layout_height="@dimen/dimen_60dp"
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            tools:src="@drawable/img_camera" />
    </androidx.cardview.widget.CardView>

    <ImageView
        android:layout_width="@dimen/dimen_20dp"
        android:layout_height="@dimen/dimen_20dp"
        android:layout_gravity="top|end"
        android:src="@drawable/icon_delete" />
</FrameLayout>
image.png

重点提醒:设置elevation能改变FrameLayout里面显示的顺序

转载于:https://blog.csdn.net/ChinaDragon10/article/details/86089474

上一篇 下一篇

猜你喜欢

热点阅读