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

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

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

&lt;androidx.drawerlayout.widget.DrawerLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:id=&quot;@+id/drawer_layout&quot;
    android:layout_height=&quot;match_parent&quot;
    android:fitsSystemWindows=&quot;true&quot;
    android:visibility=&quot;visible&quot;
    tools:context=&quot;.Main_Screen&quot;
    tools:openDrawer=&quot;start&quot;&gt;

    &lt;include
        layout=&quot;@layout/app_bar&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;/&gt;

    &lt;com.google.android.material.navigation.NavigationView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;match_parent&quot;
        android:id=&quot;@+id/nav_view&quot;
        android:layout_gravity=&quot;start&quot;
        android:fitsSystemWindows=&quot;true&quot;
        app:menu=&quot;@menu/drawer_menu&quot;
        app:headerLayout=&quot;@layout/drawer_header&quot;
        /&gt;


&lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

this is the included appbar

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;com.google.android.material.appbar.AppBarLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:fitsSystemWindows=&quot;true&quot;
        style=&quot;@style/Widget.MaterialComponents.AppBarLayout.Primary&quot;&gt;

        &lt;androidx.appcompat.widget.Toolbar
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;?actionBarSize&quot;
            android:id=&quot;@+id/toolbar&quot;
            app:contentInsetStartWithNavigation=&quot;0dp&quot; /&gt;
    &lt;/com.google.android.material.appbar.AppBarLayout&gt;

&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:

确定