[英]Add a navigation drawer to blank activity
我已经在空白活动(Android Studio)上创建了整个项目。 我想添加一个导航抽屉而不妨碍我以前的代码。是否可以将导航抽屉添加到空白活动中,如果可以,请进行指导。
步骤1:将导航视图包括到您的xml文件中,该视图将在创建时被您的活动夸大。
<?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" />
第二步:初始化视图
private NavigationView navigationView;
private DrawerLayout drawer;
第三步:发送事件在抽屉上关闭和打开
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();
}
希望这有助于设置导航抽屉。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/icon">
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/lvLeft"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ff8800"
android:choiceMode="singleChoice"
android:divider="@android:color/holo_blue_bright"
android:dividerHeight="0dp" />
<ListView
android:id="@+id/lvRight"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#0077ff"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.