我在空白活动(Android Studio)上创建了一个完整的项目。我想添加导航抽屉而不妨碍我以前的代码。有没有办法在空白活动中添加导航抽屉,如果有,请做指导。
答案 0 :(得分:1)
步骤1:在xml文件中包含导航视图,该文件将在创建时由yor活动充气。
<?xml version="1.0" encoding="utf-8"?>
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
第2步:初始化视图
private NavigationView navigationView;
private DrawerLayout drawer;
第3步:将事件发送到抽屉关闭并打开
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawer.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessary or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
希望这有助于设置导航抽屉。
答案 1 :(得分:0)
key