gpt4 book ai didi

Android Jetpack 导航 : One activity with several navigation views

转载 作者:行者123 更新时间:2023-11-29 22:55:13 25 4
gpt4 key购买 nike

我正在考虑新的 android jetpack navigation best practices每个应用程序中应该只有一个 Activity 。我想使用一个 Activity 以 fragment 形式实现以下菜单,但我对在同一个应用程序中实现多个菜单时的最佳实践感到非常不确定。

BottomNavigationView ( Reference repo ):

<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.android.navigationadvancedsample.MainActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav"/>
</LinearLayout>

截图:
BttomNavigationView

DrawerLayout ( Reference repo ):

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

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

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

<fragment
android:id="@+id/garden_nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_garden"/>

</LinearLayout>

<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
style="@style/Widget.MaterialComponents.NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/menu_navigation"/>

</androidx.drawerlayout.widget.DrawerLayout>

</layout>

截图:
enter image description here

考虑到来自 google 的这些示例,关于如何使用最佳实践在一个 Activity 中实现这两个 View 没有明确的答案,因为每个示例只为其应用使用一种形式的导航 View ,分别为 DrawerLayout , 或 BottomNavigationView

问题是:1. 为不同的 fragment 制作具有不同导航 View 的 Activity 的最佳实践是什么?例如,使用 DrawerLayout 的几个 fragment ,以及使用 BottomNavigationView 的其他 fragment 。2. 如果想在查看认证 View 和登录时隐藏DrawerLayoutBottomNavigationView,认证时显示它们怎么办?

最佳答案

您可以同时实现它们,您可以以编程方式隐藏或显示它们,或者在您的 Activity 之前拥有一个身份验证 View ,该 Activity 包含您的导航,而不是 <include/>这些导航组件。

关于Android Jetpack 导航 : One activity with several navigation views,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57502616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com