android:elevation only have shadow effects on the bottom side, how to make the shadow effects show on top side?

前端 未结 1 584
终归单人心
终归单人心 2020-12-13 13:04

I use a framelayout at the bottom of an activity, in order to show the shadow effects on the fragment, I add android:elevation. But the shadow effects only appear in the bot

1条回答
  •  鱼传尺愫
    2020-12-13 13:54

    There is a trick that can be used to display a shadow above a View.

    Basically we have to use two nested Layouts, where the outer Layout casts the shadow with an elevation and the inner layout sets the background. Then by setting a padding to the outer Layout, we can shift the inner Layout down, without moving the shadow, thus more of the shadow becomes visible:

    
    
        
    
            
    
        
    
    
    

    An important thing here is the attribute outlineProvider, that is required to make the outer Layout cast a shadow even without having a background set.

    Further we specify a negative margin to compensate for the offset created by the padding. Depending on the use-case we can omit that.

    But attention: If we shift the View too much, some rendering artifacts become visible:

    Source of this example on Github

    0 讨论(0)
提交回复
热议问题