android学习之侧边菜单滑动,toolbar+DrawerLayout

android学习之侧边菜单滑动,toolbar+DrawerLayout

toolbar上篇有写,首先在xml文件中加入DrawerLayout,记住加上id


	android:id="@+id/drawer_layout"
......

DrawerLayout里面的内容需要写主内容区布局和侧边布局(或者选其一)(类似主内容写在了DrawerLayout布局中),内容区布局写在侧边布局前面;
另外控制侧边菜单从哪边滑出的:

android:layout_gravity="start"

实现DrawerLayout布局不覆盖住toolbar,需要toolbar写在DrawerLayout前,整个xml代码:





    
    

        


    

    
        
        
    
         

            

        

    
        

            

                

                
            
        

    
        
        
            
            

在Activity中:

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private ArrayAdapter arrayAdapter;
    private ListView lvLeftMenu;
    private Button login_btn;
    private Button setting_btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        login_btn = findViewById(R.id.login_btn);
        setting_btn = findViewById(R.id.setting_btn);

        toolbar  = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        lvLeftMenu = (ListView) findViewById(R.id.dl_left_menu); //侧边菜单

        getSupportActionBar().setDisplayShowTitleEnabled(false);//关掉默认的标题
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        /*
           侧滑菜单关闭/打开监听
         */
        mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

            }
        };
        mDrawerToggle.syncState();
        drawerLayout.addDrawerListener(mDrawerToggle);
        
        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,loginActivity.class);
                Bundle bundle = new Bundle();

                intent.putExtras(bundle); //传数据
                startActivity(intent);
            }
        });

        setting_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,settingActivity.class);
                Bundle bundle = new Bundle();

                intent.putExtras(bundle); //传数据
                startActivity(intent);
            }
        });


    }
    /*
        显示toolbar上设置按钮的
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }
}

你可能感兴趣的:(android学习之侧边菜单滑动,toolbar+DrawerLayout)