简体   繁体   English

androidx中的导航抽屉

[英]Navigation drawer in androidx

I m new in android,i had search many example which is related from navigation drawer and I also apply to our project but the last things that In my project Humberger icon and navigation view(slide) are not work properly and humberger icon doesn't show.我是 android 的新手,我搜索了许多与导航抽屉相关的示例,我也适用于我们的项目,但最后一件事是在我的项目中 Humberger 图标和导航视图(幻灯片)不能正常工作,而 Humberger 图标不能节目。

I apply onCreatePost()我申请 onCreatePost()

And many method apply such as configuration and other but there is not a solution.so how can I fix this problem.. in my system install Androidx并且许多方法都适用,例如配置等,但没有解决方案。所以我该如何解决这个问题..在我的系统中安装 Androidx

I had the same issue and I fixed it by adding the navigation graph into the Hosted Fragment, I'll post the layout code from the main view.我有同样的问题,我通过将导航图添加到托管片段来修复它,我将从主视图发布布局代码。

main.xml main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

<!-- This layout contains the Action App Bar see the app_bar_main.xml code-->
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

<!-- This is the Navigation drawer widget, Which opens when you click on the hamburger icon -->
<!-- For that you could add header view -->
<!-- Can add menu or custom view for navigation tabs -->
        <com.google.android.material.navigation.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">
            <include layout="@layout/nav_body_main"/>
        </com.google.android.material.navigation.NavigationView>

    </androidx.drawerlayout.widget.DrawerLayout>

app_bar_main.xml app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_landing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.landing.main.MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main content_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation"/>
<!--  This navigation graph will sort the issue -->
</androidx.constraintlayout.widget.ConstraintLayout>

And find the Activity Code.并找到活动代码。

// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.

 mAppBarConfiguration = new AppBarConfiguration.Builder(
          R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
           R.id.nav_tools, R.id.nav_share, R.id.nav_send)
           .setDrawerLayout(drawer)
           .build();

    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController)

Easiest Way最简单的方法

The easiest way is to generate the code through the Android Studio since you are new.最简单的方法是通过 Android Studio 生成代码,因为您是新手。

RightClick on the Package folder -> New -> Activity -> Gallery右键单击 Package 文件夹 -> 新建 -> 活动 -> 图库

from that select Navigation Drawer Activity从那个 select 导航抽屉活动

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM