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

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

<?xml version="1.0" encoding="utf-8"?>
<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_width="match_parent"
  android:layout_height="match_parent"
  android:padding="20dp"
  tools:context=".MainActivity">

  <android.support.v7.widget.CardView
      android:layout_width="match_parent"
      android:layout_height="200dp"
      app:cardBackgroundColor="@color/colorWhite"
      app:cardElevation="5dp"
      app:cardMaxElevation="10dp">

      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:text="卡片内容"
          android:textColor="@color/colorAccent" />
  </android.support.v7.widget.CardView>

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="排名1"
      android:textColor="@color/colorAccent" />

</FrameLayout>
复制代码

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

<?xml version="1.0" encoding="utf-8"?>
<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_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    tools:context=".MainActivity">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:cardBackgroundColor="@color/colorWhite"
        app:cardElevation="5dp"
        app:cardMaxElevation="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="卡片内容"
            android:textColor="@color/colorAccent" />
    </android.support.v7.widget.CardView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="5dp"
        android:text="排名1"
        android:textColor="@color/colorAccent" />

</FrameLayout>
复制代码

注意:TextView里的 android:elevation=" "里的值不能小于CardView 里的 app:cardElevation=" "的值,因为elevation最大的值会显示在最上层

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

转载于:https://juejin.im/post/5c348744f265da616f702490

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值