kotlin android动作栏标题

时间:2018-06-13 13:42:43

标签: android kotlin android-actionbar

MainActivity中的以下功能,App名称为Qpon。 该应用程序运行良好,只有第一页的操作栏标题有问题。它假设为条形标题显示“fff”,但它在应用程序启动时显示应用程序名称“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")
    }

应用开始图片 enter image description here

从其他片段回来 enter image description here

1 个答案:

答案 0 :(得分:0)

WHERE

您可以查看代码中的评论或参考以下链接 访问https://android--code.blogspot.com/2018/02/android-kotlin-toolbar-example.html