如何更改应用栏中的文本?
How can i change the text in the app bar?
我目前正在开发一个需要创建时使用的导航模板的应用程序。我已经 fine-tuning 我需要处理的应用程序的默认导航模板的外观。但是,有一件事我一直无法找到解决方案,那就是更改应用栏中的文本。它一直在说 'Home' 但我需要把我的应用程序的标题放在那里。我尝试了几种方法,例如:setTitle("App title here")
在 mainActivity 中,在清单文件中我也尝试将 android:label
中的文本更改为应用程序标题,但我在网上找到的解决方案似乎什么也没做上班。
有什么方法可以解决这个问题并将 'home' 文本更改为我的应用程序标题?提前感谢您的任何建议。
根据评论中的要求在 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)
}
尝试删除 setTitle("Simple Dark Calculator")
行并在 setSupportActionBar
方法之后添加以下代码:
getSupportActionBar().setTitle("Simple Dark Calculator");
要阅读有关设置应用栏的更多信息:
Set up the app bar
勾选 doc:
NavigationUI uses the destination labels from your navigation graph to keep the title of the top app bar up-to-date.
在您的导航图中,将 android:label
更改为 startDestination
。
<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"
}
//.....
}
在清单中试试这个:
<activity android:name=".Youractivityname"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustResize"/>
我目前正在开发一个需要创建时使用的导航模板的应用程序。我已经 fine-tuning 我需要处理的应用程序的默认导航模板的外观。但是,有一件事我一直无法找到解决方案,那就是更改应用栏中的文本。它一直在说 'Home' 但我需要把我的应用程序的标题放在那里。我尝试了几种方法,例如:setTitle("App title here")
在 mainActivity 中,在清单文件中我也尝试将 android:label
中的文本更改为应用程序标题,但我在网上找到的解决方案似乎什么也没做上班。
有什么方法可以解决这个问题并将 'home' 文本更改为我的应用程序标题?提前感谢您的任何建议。
根据评论中的要求在 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)
}
尝试删除 setTitle("Simple Dark Calculator")
行并在 setSupportActionBar
方法之后添加以下代码:
getSupportActionBar().setTitle("Simple Dark Calculator");
要阅读有关设置应用栏的更多信息: Set up the app bar
勾选 doc:
NavigationUI uses the destination labels from your navigation graph to keep the title of the top app bar up-to-date.
在您的导航图中,将 android:label
更改为 startDestination
。
<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"
}
//.....
}
在清单中试试这个:
<activity android:name=".Youractivityname"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustResize"/>