*************效果图************
首先,布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnOpenMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开侧滑菜单" />
</LinearLayout>
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#9acf9a"
android:gravity="center"
android:text="这里是菜单布局" />
</android.support.v4.widget.DrawerLayout>
MainActivity中
···
private Button btnOpenMenu;
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOpenMenu = (Button) findViewById(R.id.btnOpenMenu);
drawer = (DrawerLayout) findViewById(R.id.drawer);
btnOpenMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawer.openDrawer(Gravity.LEFT);
}
});
}
···