[英]kotlin android action bar title
MainActivity中的以下功能,應用名稱為Qpon。 該應用程序運行良好,只有首頁的操作欄標題有問題。 它假定在欄標題上顯示“ fff”,但在應用啟動時顯示“ App Name”(Qpon)。 但是當我轉到其他片段並回到第一個片段時,它可以正確顯示。
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_qpon -> {
//message.setText(R.string.title_qpon)
actionBarIcon(R.drawable.ic_title_black)
createQponFragment()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_me-> {
//message.setText(R.string.title_me)
actionBarIcon(R.drawable.logged)
createMeFragment()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_tool -> {
//message.setText(R.string.title_tool)
actionBarIcon(R.drawable.logged)
createToolFragment()
return@OnNavigationItemSelectedListener true
}
R.id.navigation_tutorial -> {
//message.setText(R.string.title_tutorial)
actionBarIcon(R.drawable.tutorial)
createTutorialFragment()
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Obtain the FirebaseAnalytics instance.
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this)
actionBarIcon(R.drawable.ic_title_black)
createQponFragment()
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
fun getCurrentNumber():String {
var mAuth: FirebaseAuth? = null
mAuth = FirebaseAuth.getInstance()
val cUser = mAuth?.currentUser
val userPhone = cUser?.phoneNumber
if (userPhone != null) {
return userPhone.toString()
}
else { return "" }
}
fun actionBarIcon(imageName:Int) {
setSupportActionBar(findViewById(R.id.my_toolbar))
my_toolbar.setLogo(imageName)
if (imageName == R.drawable.ic_title_black) my_toolbar.setTitle("fff")
if (imageName == R.drawable.logged) my_toolbar.setTitle(getCurrentNumber())
if (imageName == R.drawable.tutorial) my_toolbar.setTitle("Tutorial")
}
fun actionBarIcon(imageName:Int) {
setSupportActionBar(findViewById(R.id.my_toolbar))
my_toolbar.setLogo(imageName)
/*
What is to be done to solve the issue:-
Below code is setting up title to fff if only the condition is met. but there you have not set any title if condition is not met so by default it is picking up app name.
So, give a default value when activity is visible. Either provide title to toolbar in layout or use
// Set toolbar title/app title
my_toolbar!!.title = "fff"
*/
if (imageName == R.drawable.ic_title_black) my_toolbar.setTitle("fff")
if (imageName == R.drawable.logged) my_toolbar.setTitle(getCurrentNumber())
if (imageName == R.drawable.tutorial) my_toolbar.setTitle("Tutorial")
}
您可以檢查代碼中的注釋或參考以下鏈接,例如訪問https://android--code.blogspot.com/2018/02/android-kotlin-toolbar-example.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.