How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?

后端 未结 10 1080
[愿得一人]
[愿得一人] 2020-11-22 04:05

I\'ve seen in the new material design Side Nav spec that you can display the drawer over the action bar and behind the status bar. How can I implement this?

10条回答
  •  面向向阳花
    2020-11-22 04:33

    With the release of the latest Android Support Library (rev 22.2.0) we've got a Design Support Library and as part of this a new view called NavigationView. So instead of doing everything on our own with the ScrimInsetsFrameLayout and all the other stuff we simply use this view and everything is done for us.

    Example

    Step 1

    Add the Design Support Library to your build.gradle file

    dependencies {
        // Other dependencies like appcompat
        compile 'com.android.support:design:22.2.0'
    }
    

    Step 2

    Add the NavigationView to your DrawerLayout:

     
    
         
    
          
     
    

    Step 3

    Create a new menu-resource in /res/menu and add the items and icons you wanna display:

    
    
        
            
            
        
    
        
            
                
            
        
    
    
    

    Step 4

    Init the NavigationView and handle click events:

    public class MainActivity extends AppCompatActivity {
    
        NavigationView mNavigationView;
        DrawerLayout mDrawerLayout;
    
        // Other stuff
    
        private void init() {
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
            mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    mDrawerLayout.closeDrawers();
                    menuItem.setChecked(true);
                    switch (menuItem.getItemId()) {
                        case R.id.nav_home:
                            // TODO - Do something
                            break;
                        // TODO - Handle other items
                    }
                    return true;
                }
            });
        }
    }
    

    Step 5

    Be sure to set android:windowDrawsSystemBarBackgrounds and android:statusBarColor in values-v21 otherwise your Drawer won`t be displayed "under" the StatusBar

    
    

    Optional Step

    Add a Header to the NavigationView. For this simply create a new layout and add app:headerLayout="@layout/my_header_layout" to the NavigationView.

    Result

    picture showing navigation view

    Notes

    • The highlighted color uses the color defined via the colorPrimary attribute
    • The List Items use the color defined via the textColorPrimary attribute
    • The Icons use the color defined via the textColorSecondary attribute

    You can also check the example app by Chris Banes which highlights the NavigationView along with the other new views that are part of the Design Support Library (like the FloatingActionButton, TextInputLayout, Snackbar, TabLayout etc.)

提交回复
热议问题