小编bag*_*ana的帖子

How to Change ActionBar title in fragment class in kotlin?

I'm new to android development in kotlin. I tried the following code to change the actionbar title from my fragment.It's not working.Please help

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)


    activity.actionBar?.title = "Example 1"
    countBtn.setOnClickListener(this)
    toastBtn.setOnClickListener(this)
    randomBtn.setOnClickListener(this)

}
Run Code Online (Sandbox Code Playgroud)

MainActivity.kt code is as below

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    setSupportActionBar(toolbar)
    val actionBar = supportActionBar
    actionBar?.title = getString(R.string.toolbar_title)
}
Run Code Online (Sandbox Code Playgroud)

android android-fragments kotlin android-actionbar

4
推荐指数
1
解决办法
3888
查看次数