Android 4.4 remove shadow from CardView

2020-04-17 04:26发布

问题:

I tried to remove the shadow from the Cardview.
I used android support v7 Cardview on android v4.4.2

My layout look:

<android.support.v7.widget.CardView
    android:id="@+id/cardContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    card_view:cardBackgroundColor="@color/transparent"
    card_view:cardCornerRadius="0dp"
    card_view:cardElevation="0dp"
    card_view:cardPreventCornerOverlap="false"
    card_view:contentPadding="0dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include layout="@layout/include_profile_header" />

        <include layout="@layout/include_profile_footer" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

As you can see i used:

  card_view:cardElevation="0dp" 

Tried so far:

android:background="@null"  

But still you can see in the image i attached the top shadow that showed.

回答1:

You cannot remove the shadow from the cardview in 4.4. Anything below 5.0 elevation does nothing so all the cardview is in 4.4 is an image with a shadow

To create a card with a shadow, use the card_view:cardElevation attribute. CardView uses real elevation and dynamic shadows on Android 5.0 (API level 21) and above and falls back to a programmatic shadow implementation on earlier versions. For more information, see Maintaining Compatibility.

http://developer.android.com/training/material/lists-cards.html#CardView



回答2:

Just add below code to cardview tag

app:cardElevation="0dp"

and don't forget to add

xmlns:app="http://schemas.android.com/apk/res-auto"

UPDATE

Also try this put below codes in cardview

app:cardElevation="0dp"
app:cardMaxElevation="0dp"
app:cardCornerRadius="1dp"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false"
android:background="@null"

The app:cardCornerRadius should be bigger than 0(>0) otherwise throw exception

I'm using API 28 support library

implementation 'com.android.support:cardview-v7:28.0.0'



回答3:

I just fixed this problem by using a relative layout instead. Simply change the CardView value to a relative layout and you will essentially get the same result, but without the shadow