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裏面顯示的順序

版權聲明:本文爲博主原創文章!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章