【问题标题】:Add a navigation drawer to blank activity向空白活动添加导航抽屉
【发布时间】:2016-11-19 15:14:38
【问题描述】:

我在一个空白活动(Android Studio)上创建了一个完整的项目。我想在不妨碍我以前的代码的情况下添加导航抽屉。有没有办法将导航抽屉添加到空白活动,如果是,请指导。

【问题讨论】:

  • @Swr7der 谢谢,能不能发个源代码,不能发也可以
  • 是的,但您应该尝试自己学习和编码。这对你有好处。如果在那之后,您遇到问题,请在此处发布。有同行可以帮助您。快乐编码:)
  • @Anonymous Android Studio 允许您创建模板。文件 > 新建 > 活动 > 导航抽屉活动。然后阅读android studio生成的代码
  • @Swr7der 谢谢

标签: java android xml android-studio navigation-drawer


【解决方案1】:

第 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();
}

希望这有助于如何设置导航抽屉。

【讨论】:

    【解决方案2】:
    <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>
    

    【讨论】:

    • 除了格式化代码之外,您还应该在答案中添加解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多