什么是安卓中的“顶层目的地”?

huangapple 未分类评论40阅读模式
英文:

What is a "Top-level destination" in android?

问题

这是我的翻译:

我正在使用Java在Android Studio中工作。我想要实现一个带有汉堡图标的导航抽屉(从左向右滑动),并且希望汉堡图标始终位于顶部。但是存在一个“顶级目标”的问题,如果没有这个,汉堡图标就不会显示出来。我找到的一个解决方案是将该活动设置为我的启动活动,但我不想这样做。

有人能否解释一下这个“顶级目标”是什么意思,以及可能的导航抽屉问题的解决方法!

这是我的导航布局:

  1. <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:id="@+id/drawer_layout"
  6. android:layout_height="match_parent"
  7. android:fitsSystemWindows="true"
  8. android:visibility="visible"
  9. tools:context=".Main_Screen"
  10. tools:openDrawer="start">
  11. <include
  12. layout="@layout/app_bar"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"/>
  15. <com.google.android.material.navigation.NavigationView
  16. android:layout_width="wrap_content"
  17. android:layout_height="match_parent"
  18. android:id="@+id/nav_view"
  19. android:layout_gravity="start"
  20. android:fitsSystemWindows="true"
  21. app:menu="@menu/drawer_menu"
  22. app:headerLayout="@layout/drawer_header"
  23. />
  24. </androidx.drawerlayout.widget.DrawerLayout>

这是包含的应用栏布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:orientation="vertical">
  6. <com.google.android.material.appbar.AppBarLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:fitsSystemWindows="true"
  10. style="@style/Widget.MaterialComponents.AppBarLayout.Primary">
  11. <androidx.appcompat.widget.Toolbar
  12. android:layout_width="match_parent"
  13. android:layout_height="?actionBarSize"
  14. android:id="@+id/toolbar"
  15. app:contentInsetStartWithNavigation="0dp" />
  16. </com.google.android.material.appbar.AppBarLayout>
  17. </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

  1. &lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  2. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  3. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:id=&quot;@+id/drawer_layout&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. android:fitsSystemWindows=&quot;true&quot;
  8. android:visibility=&quot;visible&quot;
  9. tools:context=&quot;.Main_Screen&quot;
  10. tools:openDrawer=&quot;start&quot;&gt;
  11. &lt;include
  12. layout=&quot;@layout/app_bar&quot;
  13. android:layout_width=&quot;match_parent&quot;
  14. android:layout_height=&quot;wrap_content&quot;/&gt;
  15. &lt;com.google.android.material.navigation.NavigationView
  16. android:layout_width=&quot;wrap_content&quot;
  17. android:layout_height=&quot;match_parent&quot;
  18. android:id=&quot;@+id/nav_view&quot;
  19. android:layout_gravity=&quot;start&quot;
  20. android:fitsSystemWindows=&quot;true&quot;
  21. app:menu=&quot;@menu/drawer_menu&quot;
  22. app:headerLayout=&quot;@layout/drawer_header&quot;
  23. /&gt;
  24. &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

this is the included appbar

  1. &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  2. android:layout_width=&quot;match_parent&quot;
  3. android:layout_height=&quot;wrap_content&quot;
  4. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  5. android:orientation=&quot;vertical&quot;&gt;
  6. &lt;com.google.android.material.appbar.AppBarLayout
  7. android:layout_width=&quot;match_parent&quot;
  8. android:layout_height=&quot;wrap_content&quot;
  9. android:fitsSystemWindows=&quot;true&quot;
  10. style=&quot;@style/Widget.MaterialComponents.AppBarLayout.Primary&quot;&gt;
  11. &lt;androidx.appcompat.widget.Toolbar
  12. android:layout_width=&quot;match_parent&quot;
  13. android:layout_height=&quot;?actionBarSize&quot;
  14. android:id=&quot;@+id/toolbar&quot;
  15. app:contentInsetStartWithNavigation=&quot;0dp&quot; /&gt;
  16. &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  17. &lt;/LinearLayout&gt;

答案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.

huangapple
  • 本文由 发表于 2020年4月4日 15:24:13
  • 转载请务必保留本文链接:https://java.coder-hub.com/61024973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定