gpt4 book ai didi

android - 如何使用导航 Controller 组件设置不同的工具栏?

转载 作者:行者123 更新时间:2023-12-04 23:36:16 31 4
gpt4 key购买 nike

我实际上不确定,使用导航 Controller 组件设置不同工具栏的正确方法或最佳实践

在我的应用程序中。我想设置 2 个不同的工具栏。

  • 绿色工具栏
  • 红色工具栏

  • 两个不同颜色的工具栏。这只是为了简化案例,实际上我有多个工具栏

    我正在使用导航 Controller 组件。目前在我的主要 Activity 中作为主机,我使用此代码在我的主要 Activity 中设置了绿色工具栏
            setSupportActionBar(green_toolbar)
    supportActionBar?.setDisplayShowTitleEnabled(false)

    // set up top hierarchy destination
    val appBarConfiguration = AppBarConfiguration(setOf(
    R.id.destination_home,
    R.id.destination_search,
    R.id.destination_user_control,
    R.id.destination_create_event)
    )

    green_toolbar.setupWithNavController(navController,appBarConfiguration)

    那么使用导航 Controller 组件设置不同工具栏的最佳方法是什么?

    我必须在我的主要 Activity 中制作这两个不同的工具栏吗?还是我必须将 fragmentY 目的地(具有红色工具栏)设置为另一个 Activity 而不是 fragment ?

    或者....我不知道....请帮忙:)

    最佳答案

    下面的答案是针对使用底部导航 View 的应用程序,如果您使用的是抽屉导航,请使用 this anwer
    所以根据 here 的文档,我需要在每个 fragment 中设置工具栏。

    If, however, your top app bar changes substantially acrossdestinations, then consider removing the top app bar from youractivity and defining it in each destination fragment, instead.


    所以我们将在每个 fragment 中添加工具栏,而不是在 MainActivity 中设置它。如果您在每个 fragment 中设置工具栏,也可以实现折叠工具栏。
    例如,在您的底部导航菜单中,您有主页、个人资料和搜索 fragment 作为顶级 fragment (根),就像这样
    enter image description here
    所以在 每个 顶级 fragment 使用 onViewCreated 中的此代码设置工具栏你的 fragment 。
    val toolbar = view.findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbarInYourFragment)
    val appBarConfiguration = AppBarConfiguration(setOf(
    R.id.destination_home,
    R.id.destination_profile // set all your top level destinations in here
    R.id.destination_search)
    )

    val navHostFragment = NavHostFragment.findNavController(this);
    NavigationUI.setupWithNavController(toolbar, navHostFragment,appBarConfiguration)
    你必须通过 appBarConfiguration删除工具栏中的后退按钮。所以你必须通过 appBarConfiguration在每个顶级 fragment (家庭、搜索、个人资料)中,不仅在您的家中。
    对于其余的 fragment ,您不需要通过 appBarConfiguration ,所以对于其余的 fragment ,只需在 onViewCreated 中传递此代码.
    val toolbar = view.findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbarInYourFragment)
    val navHostFragment = NavHostFragment.findNavController(this);
    NavigationUI.setupWithNavController(toolbar, navHostFragment)
    如果您的工具栏有菜单,则添加以下代码:
    setHasOptionsMenu(true)

    (activity as AppCompatActivity).setSupportActionBar(toolbar)

    toolbar.setNavigationOnClickListener { view ->
    view.findNavController().navigateUp()
    }
    使用 AppBarConfiguration类,在 gradle 应用程序中你需要使用 navigation-ui-ktx工件,您需要像这样添加编译选项和 kotlin 选项
    android {


    compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

    }


    dependencies {

    def nav_version = "2.3.0-alpha04"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    }
    不要忘记在您的 res 值样式 xml 中添加 noActionBar
     <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">


    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>


    </style>



    关于android - 如何使用导航 Controller 组件设置不同的工具栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57014922/

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