Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

android - Inconsistent shadow on MaterialCardView when its overlaid by children

When a child of a MaterialCardView fully overlays it, the shadow under it disappears. I have tried setting elevation to the view itself but to no effect.

Example layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- Elevation of this card is large for demonstration purposes -->
    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:layout_margin="16dp"
        app:cardElevation="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <View
            android:layout_width="120dp"
            android:layout_height="match_parent"
            android:background="@color/purple_500"
            android:elevation="24dp"
            app:elevation="24dp" />
    </com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.widget.ConstraintLayout>

Screenshot of the problem:

Shadow is clearly visible under white background of the card, but not under purple view

Here shadow is clearly visible under white background of the card, but not under purple view.

The app was tested on API 30 emulator, but I have observed similar behaviour on API 29 physical device.

question from:https://stackoverflow.com/questions/65642209/inconsistent-shadow-on-materialcardview-when-its-overlaid-by-children

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok it turns out that it was just an optical illusion: if you overlay the purple control in Paint the shadow is consistent:

enter image description here

Now its up to me how to prevent the following illusion from impacting the visual experience of my app.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...