Elevation on Android Lollipop not working

后端 未结 9 1552
长情又很酷
长情又很酷 2020-11-28 12:22

I am trying to make use of the elevation property in the latest Android Lollipop preview release. I set the targetSdk to 21 and the theme to Material. Next i added a backgro

9条回答
  •  旧时难觅i
    2020-11-28 13:00

    TL;DR

    Check your card: (or whatever word you use) namespace declaration and make sure it matches this: xmlns:card="http://schemas.android.com/apk/res-auto"

    I know there are a few answers here already, but I wanted to add mine as it wasn't included in these current suggestions. In order to get the shadows working on both KitKat and Marshmallow, (only emulators I tried, I'm sure it works in between) I added the following xml attributes to my card:

    card:cardElevation="25dp"
    card:cardUseCompatPadding="true"
    

    After banging my head against my desk for why that wasn't working, trying to set the background color of the card to something completely opaque, enabling hardware acceleration in the manifest, and even praying, I checked the namespace declarations in my file. To my horror, I saw that the card xml namespace had been assigned to the following:

    xmlns:card="http://schemas.android.com/tools"
    

    After fixing that namespace declaration, I ran my dummy app again and let out a sigh of relief as shadows were finally being shown as expected.

    Here is proof in case you're a doubter like me. And please Google, Android, whoever: Make shadows great again. They shouldn't be this difficult.

    Here is the entire file that created the layout pictured below:

    
    
    
    
    
    
    
        
    
    

    Yes, I know. It's annoying that you can't simply copy and paste this layout and run it because it has PercentRelativeLayout in it, but I left it there on purpose. You should definitely check it out. Super powerful, very useful. Nice nice nice. Hope this helped.

提交回复
热议问题