Android L FAB Button shadow

前端 未结 3 2005
名媛妹妹
名媛妹妹 2020-12-12 18:50

In the Material Design guidelines Google presented a new style of button, the FAB Button. I found instructions how to make it but I have trouble adding the shadow.

3条回答
  •  猫巷女王i
    2020-12-12 18:57

    You can use a Button:

    
    

    where the ic_action_add is your icon.

    drawable/ripple.xml is:

    
        
            
                
            
        
    
    

    anim/anim.xml is:

    
        
            
        
        
            
        
    
    

    Dimens.xml is

    
        56dp
    
        2dp
        4dp
    
    

    With the elevation attribute you should set the Outline via code:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layoutfab);
    
            //Outline            
            Button fab = (Button) findViewById(R.id.fab)
    
            ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
              @Override
                  public void getOutline(View view, Outline outline) {
                     // Or read size directly from the view's width/height
                     int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
                     outline.setOval(0, 0, size, size);
                  }
            };
            fab.setOutlineProvider(viewOutlineProvider);
        }        
    }
    

提交回复
热议问题