Add elevation/shadow on toolbar for pre-lollipop devices

前端 未结 8 1245
猫巷女王i
猫巷女王i 2020-11-28 05:39

I updated my android app to the new material design, but I also wanted to add some shadow or elevation to the Toolbar. There seem to be some (hacky) ways of doing it via ima

8条回答
  •  自闭症患者
    2020-11-28 05:54

    I have a CollapsingToolbarLayout with Toolbar and a Toolbar-like View that moves up and down, but lays above NestedScrollView, like in https://github.com/k0shk0sh/CoordinatorLayoutExample. I tried many variants. Sometimes a shadow scrolled above a screen with NestedScrollView, sometimes the Toolbar drew a solid shadow without transparency, sometimes the shadow was aliased. Anyway, this is my solution.

    Say, you have a layout:

    
        
             
                 
                     
                 
            
        
    
        
    
        
    
            
                
            
    
        
    
        
        
        
    
    
    

    Add a shadow (drawable/shadow.xml):

    
    
        
    
    

    Add this method. Here scrollShadow is a view named "scroll_shadow":

    private void setShadowVisibility(int visibility) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            scrollShadow.setVisibility(visibility);
        }
    }
    

    Manipulate it as you wish. It will show a gradient on pre-Lollipop devices and shows a normal shadow for Android 5.0+.

提交回复
热议问题