简体   繁体   English

导航抽屉在标题中显示当前目的地名称

[英]Navigation drawer showing current destination name in title

I've developed into my app simple Navigation Drawer mechanism.我已经开发成我的应用程序简单的导航抽屉机制。 More about code used in app here: https://developer.android.com/guide/navigation/navigation-ui有关应用程序中使用的代码的更多信息: https://developer.android.com/guide/navigation/navigation-ui

The problem's when user's switching between destinations toolbar's title changes so that it displays current location xml layout name:问题是当用户在目标工具栏的标题之间切换时,它会显示当前位置 xml 布局名称:

在此处输入图像描述

When restarting or launching app current title is that specified in manifest, when switching to other destination it changes again.重新启动或启动应用程序时,当前标题是清单中指定的,当切换到其他目的地时,它会再次更改。

在此处输入图像描述

Code of main activity, layout and Kotlin:主要活动、布局和Kotlin的代码:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:openDrawer="start"
    tools:context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.CurrencyApp.AppBarOverlay">

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

            <com.google.android.material.switchmaterial.SwitchMaterial
                android:id="@+id/color_mode_switch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|end"
                android:text="@string/action_color_mode"
                android:textAppearance="@style/TextAppearance.AppCompat.Small" />
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <fragment
        android:id="@+id/fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/nav_view"
        app:menu="@menu/drawer_menu"
        app:headerLayout="@layout/nav_header"
        android:layout_gravity="start"/>
</androidx.drawerlayout.widget.DrawerLayout>

Kotlin file: Kotlin 文件:

class MainActivity : AppCompatActivity() {

    private lateinit var binding : ActivityMainBinding
    private lateinit var prefs : SharedPreferences
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setSupportActionBar(binding.toolbar)
        val navController = findNavController(R.id.fragment)
        val drawerLayout  = binding.drawerLayout
        val appBarConfiguration = AppBarConfiguration(navGraph = navController.graph,drawerLayout)

        binding.toolbar.setupWithNavController(navController,appBarConfiguration)

Nav drawer header:导航抽屉 header:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="176dp"
    android:background="@color/purple_200"
    android:gravity="bottom"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:text="Testowy nav drawer"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="info@codingflow.com"/>

</LinearLayout>

Nav Drawer menu:导航抽屉菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_message"
            android:icon="@drawable/ic_add_circle_outline_24px"
            android:title="Message" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:title="Share"
                android:icon="@drawable/ic_add" />
        </menu>
    </item>
</menu>

Navigation file:导航文件:

    <?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
    app:startDestination="@id/mainFragment">
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.currencyapp.MainFragment"
        android:label="fragment_main"
        tools:layout="@layout/fragment_main" >
        <action
            android:id="@+id/main_to_tab"
            app:destination="@id/tabFragment" />
        <action
            android:id="@+id/main_to_card"
            app:destination="@id/recyclerFragment2" />
    </fragment>
    <fragment
        android:id="@+id/recyclerFragment2"
        android:name="com.example.currencyapp.RecyclerFragment"
        android:label="fragment_recycler"
        tools:layout="@layout/fragment_recycler" />
    <fragment
        android:id="@+id/tabFragment"
        android:name="com.example.currencyapp.TabFragment"
        android:label="TabFragment" >
        <action
            android:id="@+id/tab_to_main"
            app:destination="@id/mainFragment" />
    </fragment>
</navigation>

Those titles are not their respective layout xml's names but are android:label s you set in the nav_graph .这些标题不是它们各自的布局 xml 的名称,而是您在 nav_graph 中设置的android:label nav_graph

If you want them always be the app's name, set android:label="@string/app_name" .如果您希望它们始终是应用程序的名称,请设置android:label="@string/app_name"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM