簡體   English   中英

Kotlin Android操作欄標題

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM