【问题标题】:How can i change the text in the app bar?如何更改应用栏中的文本?
【发布时间】:2020-08-18 04:57:33
【问题描述】:

我目前正在开发一个需要创建导航模板的应用程序。我一直在为我需要处理的应用程序微调默认导航模板的外观。但是,有一件事我一直无法找到解决方案,那就是更改应用栏中的文本。它一直说“首页”,但我需要把我的应用程序的标题放在那里。我尝试了几件事,例如:setTitle("App title here") 在 mainActivity 和清单文件中,我还尝试将android:label 中的文本更改为应用程序标题,但我从网上找到的解决方案中没有做任何事情似乎有效。

有什么办法可以解决这个问题并将“主页”文本更改为我的应用程序标题?提前感谢您的任何建议。

注释中要求的 OnCreate 内的代码:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setTitle("Simple Dark Calculator")
    setContentView(R.layout.activity_main)
    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)



    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    val navView: NavigationView = findViewById(R.id.nav_view)
    val navController = findNavController(R.id.nav_host_fragment)
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    appBarConfiguration = AppBarConfiguration(setOf(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow), drawerLayout)
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
}

【问题讨论】:

  • 在onCreate 中向我们展示您的代码,以便我们更好地帮助您。
  • @Armin 更新了我的问题。
  • 查看out

标签: android kotlin navigation-drawer android-architecture-navigation android-jetpack-navigation


【解决方案1】:

查看doc:

NavigationUI 使用导航图中的目标标签来保持顶部应用栏的标题是最新的。

在导航图中更改startDestination 的android:label。

<navigation 
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:label="@string/menu_home"
        ... >

    </fragment>

你也可以使用OnDestinationChangedListener在你的设置方法之后设置标题:

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

【讨论】:

    【解决方案2】:

    尝试删除setTitle("Simple Dark Calculator")这一行并在setSupportActionBar 方法之后添加以下代码:

    getSupportActionBar().setTitle("Simple Dark Calculator");
    

    要了解有关设置应用栏的更多信息: Set up the app bar

    【讨论】:

      【解决方案3】:

      在 Manifest 中试试这个:

      <activity android:name=".Youractivityname"
                        android:label="@string/app_name"
                        android:theme="@style/AppTheme.NoActionBar"
                        android:windowSoftInputMode="adjustResize"/>
      

      【讨论】:

        猜你喜欢
        • 2020-03-30
        • 2021-09-19
        • 2011-03-27
        • 1970-01-01
        • 2019-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-24
        相关资源
        最近更新 更多