如何使用导航组件在NavHostFragment中设置subtilte?

时间:2020-07-05 05:37:38

标签: android android-architecture-components android-architecture-navigation android-navigation android-navigationview

package `my`.app.ui

import `my`.app.R
import android.os.Bundle
import androidx.fragment.app.Fragment

class Support : Fragment(R.layout.appui) {
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        activity.apply {
            this?.actionBar?.title = "title"
            this?.actionBar?.subtitle = "subtitle"
        }
    }
}

NavigationUI不支持设置标题或副标题?

以下是用于项目的依赖项

 - implementation 'androidx.core:core-ktx:1.3.0'
 - implementation 'androidx.appcompat:appcompat:1.1.0'
 - implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
 - implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'

1 个答案:

答案 0 :(得分:1)

您可以在设置方法之后添加OnDestinationChangedListener来设置标题:

    navController.addOnDestinationChangedListener { controller, destination, arguments ->
        if (destination.id == R.id.nav_xxx){
            supportActionBar?.title = "My Title"
        }
        //.....
    }