【发布时间】:2018-10-30 08:11:30
【问题描述】:
我有一个片段:
class HomeFragment : Fragment() { ... }
现在我正在尝试向其添加操作栏,但这不起作用:
setSupportActionBar(findViewById(R.id.toolbar_main))
如何设置 Support,然后将 Items 添加到 ActionBar?
这是它在 AppCompatActivity 中的工作方式:
// This adds items to the ActionBar
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_toolbar_main, menu)
return true
}
// This is the OnClickListener for the Buttons in the ActionBar
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
R.id.toolbar_edit -> {
true
}
R.id.toolbar_search -> {
true
}
else -> {
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
super.onOptionsItemSelected(item)
}
}
提前致谢!
【问题讨论】:
标签: android android-fragments kotlin android-actionbar android-toolbar