drawerLayout左侧菜单(或右侧)的展开与隐藏

一。布局文件



    
    

        

二。java代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private DrawerLayout draw_layou;
private Button btn1;
private Button btn2;

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

    //初始化控件
    draw_layou = findViewById(R.id.draw_layou);
    btn1 = findViewById(R.id.btn1);
    btn2 = findViewById(R.id.btn2);

    //点击侧滑
    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);

    //drawerLayout的侧拉监听事件
    draw_layou.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(@NonNull View view, float v) {

        }
        @Override
        public void onDrawerOpened(@NonNull View view) {

            Toast.makeText(MainActivity.this,"侧拉菜单打开了~",Toast.LENGTH_LONG).show();
        }
        @Override
        public void onDrawerClosed(@NonNull View view) {
            Toast.makeText(MainActivity.this,"侧拉菜单关闭了...",Toast.LENGTH_LONG).show();
        }
        @Override
        public void onDrawerStateChanged(int i) {

            Toast.makeText(MainActivity.this,"侧拉菜单状态改变了! ",Toast.LENGTH_LONG).show();
        }
    });
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn1:
            //点击主界面中的按钮弹出侧滑界面
            draw_layou.openDrawer(Gravity.LEFT);
            break;

        case R.id.btn2:
            //点击侧滑界面中的按钮缩回侧滑界面
            draw_layou.closeDrawer(Gravity.LEFT);
            break;
    }
}

}

你可能感兴趣的:(drawerLayout左侧菜单(或右侧)的展开与隐藏)