我正在尝试在片段中设置工具栏。
尽管Google Developer文档已更新为包括Kotlin代码(请参见this page):
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my)
// Note that the Toolbar defined in the layout has the id "my_toolbar"
setSupportActionBar(findViewById(R.id.my_toolbar))
与片段中相对于活动中工具栏的设置有关。
我发现了这个SO post,这表明您不能只在片段中调用setSupportActionBar。引用:
片段没有这样的方法setSupportActionBar()。 ActionBar是一个 属性,以便将工具栏设置为actionBar, 活动应从ActionBarActivity扩展,然后您可以调用 您的片段:
...
如果您使用的是AppCompatActivity:
((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
但是上面给出的代码在Java中。
我怎么在Kotlin中称呼它?
答案 0 :(得分:3)
要从Kotlin的ActionBar
访问Fragment
,
if(activity is AppCompatActivity){
(activity as AppCompatActivity).setSupportActionBar(mToolbar)
}
要设置ActionBar
中的Fragment
标题,您可以
(activity as AppCompatActivity).supportActionBar?.title = "Title"
或
(activity as AppCompatActivity).supportActionBar?.setTitle(R.string.my_title_string)
答案 1 :(得分:1)
我认为Google的Navigation codelab中有一个实现,可以满足我的需要:启用标题,菜单项的自定义,并针对不同的片段上下文插入向上导航。具体来说:
navigation_activity.xml
<LinearLayout>
<android.support.v7.widget.Toolbar/>
<fragment/>
<android.support.design.widget.BottomNavigationView/>
</LinearLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
private var drawerLayout: DrawerLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.navigation_activity)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
//...
// Set up Action Bar
val navController = host.navController
setupActionBar(navController)
//...
}
private fun setupActionBar(navController: NavController) {
drawerLayout = findViewById(R.id.drawer_layout)
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val retValue = super.onCreateOptionsMenu(menu)
val navigationView = findViewById<NavigationView>(R.id.nav_view)
// The NavigationView already has these same navigation items, so we only add
// navigation items to the menu here if there isn't a NavigationView
if (navigationView == null) {
menuInflater.inflate(R.menu.menu_overflow, menu)
return true
}
return retValue
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Have the NavHelper look for an action or destination matching the menu
// item id and navigate there if found.
// Otherwise, bubble up to the parent.
return NavigationUI.onNavDestinationSelected(item,
Navigation.findNavController(this, R.id.my_nav_host_fragment))
|| super.onOptionsItemSelected(item)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(drawerLayout,
Navigation.findNavController(this, R.id.my_nav_host_fragment))
}
}
MainFragment.kt
class MainFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
setHasOptionsMenu(true)
return inflater.inflate(R.layout.main_fragment, container, false)
}
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
inflater?.inflate(R.menu.main_menu, menu)
}
}