英文:
What is a "Top-level destination" in android?
问题
这是我的翻译:
我正在使用Java在Android Studio中工作。我想要实现一个带有汉堡图标的导航抽屉(从左向右滑动),并且希望汉堡图标始终位于顶部。但是存在一个“顶级目标”的问题,如果没有这个,汉堡图标就不会显示出来。我找到的一个解决方案是将该活动设置为我的启动活动,但我不想这样做。
有人能否解释一下这个“顶级目标”是什么意思,以及可能的导航抽屉问题的解决方法!
这是我的导航布局:
<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:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:visibility="visible"
tools:context=".Main_Screen"
tools:openDrawer="start">
<include
layout="@layout/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/nav_view"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/drawer_header"
/>
</androidx.drawerlayout.widget.DrawerLayout>
这是包含的应用栏布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
style="@style/Widget.MaterialComponents.AppBarLayout.Primary">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:id="@+id/toolbar"
app:contentInsetStartWithNavigation="0dp" />
</com.google.android.material.appbar.AppBarLayout>
</LinearLayout>
英文:
I am working in Android Studio using Java. I want to implement a Navigation Drawer(those left to right swipe ones) with a Hamburger icon always on top. But there is some issue of "Top-level destination" without which that Hamburger icon doesn't show up. One of the solutions I found was to make that activity as my Launcher activity but I don't want that.
Can someone please explain to me what this "Top-level destination" phrase means and a possible workaround for my Navigation Drawer problem!!
This is my navigation layout
<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:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:visibility="visible"
tools:context=".Main_Screen"
tools:openDrawer="start">
<include
layout="@layout/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/nav_view"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/drawer_header"
/>
</androidx.drawerlayout.widget.DrawerLayout>
this is the included appbar
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
style="@style/Widget.MaterialComponents.AppBarLayout.Primary">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:id="@+id/toolbar"
app:contentInsetStartWithNavigation="0dp" />
</com.google.android.material.appbar.AppBarLayout>
</LinearLayout>
答案1
得分: 0
我已经创建了一个名为 LoginActivity
的活动,在这个活动中,我只放置了一个按钮。同时,我创建了一个名为 MainActivity
的活动,在这个活动中我添加了一个抽屉。通过点击这个按钮,我启动了 MainActivity
。
实际上,我并没有遇到你之前提到的问题。
但是我有一个建议,你可以尝试通过文件模板来创建带有抽屉的活动,而不是手动创建,以避免一些我们尚未发现的错误。
具体步骤是:右键点击包名 -> 新建 -> 活动 -> 带有导航抽屉的活动。
英文:
I have created an activity named LoginActivity
, where I just put a button, and created an activity named MainActivity
, where I put drawer. By clicking the button, I start the MainActivity
.
In fact, I have not recurrented the problem you have said above.
But I have a suggestion that you may try creating the activity with drawer by file template not by hand, to avoid some mistakes that we have not discovered.
The step is: Right click the package name -> New -> Activity -> Navigation Drawer Activity.
专注分享java语言的经验与见解,让所有开发者获益!
评论