首页 > 解决方案 > 如何在片段中设置工具栏副标题?

问题描述

我有一个主要活动的应用程序,主要用作导航主机。我为主要活动定义了一个工具栏,我可以在主要活动中设置标题和副标题。

我还有三个片段,它们通过按下底部导航视图中的项目来显示。

所有这些工作正常。

我需要在每个片段中设置工具栏副标题。我在谷歌搜索后编写的代码编译良好,但没有设置字幕。

以下是相关的代码:

主要活动:

const val DEBUG: Boolean = true
class MainActivity : AppCompatActivity() {

    private lateinit var navcontroller: NavController
    lateinit var toolbar: Toolbar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        setSupportActionBar(findViewById(R.id.toolbar))

        toolbar = findViewById<Toolbar>(R.id.main_toolbar)
        toolbar.setTitle("Carnatic Songs")
        toolbar.subtitle = "Songs"

//        toolbar.setNavigationIcon(R.mipmap.ic_launch_icon)


        // Using the Navigation component of Jetpack. For this to work, the ids of the menu items are
        // to be exactly the same as the fragment ids

        navcontroller = Navigation.findNavController(this, R.id.nav_host_fragment)
        NavigationUI.setupWithNavController(bottomNavigationView, navcontroller)
}

MainActivity 中的字幕设置成功并显示。

活动主.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".view.MainActivity">

    <include
        android:id="@+id/main_toolbar"
        layout="@layout/toolbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_toolbar"
        app:navGraph="@navigation/navigation_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

工具栏.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    >

</androidx.appcompat.widget.Toolbar>

在片段中,我正在执行以下操作来设置字幕:

(activity as AppCompatActivity).supportActionBar?.subtitle= noSongsDisplayed + " Songs"

但是,没有设置字幕。我在 onViewCreated 和 onResume 中都试过了,但没有任何效果。

我正在使用 androidx 库。

感谢您提前提供任何帮助。

标签: androidandroid-fragmentsandroid-toolbar

解决方案


推荐阅读