我如何可以动态地改变操作栏的动作? [英] How can I change Action Bar actions dynamically?
问题描述
我有一个动作条和标签导航的活动。我现在用的分割模式,所以标签在顶部和行动是在底部的酒吧。如何动态改变底部的动作?我需要这个,因为每一个标签都有不同的动作。
I have an Activity with ActionBar and tab navigation. I am using the split mode, so the tabs are at the top and actions are in the bottom bar. How can I dynamically change the bottom actions? I need this because every tab has different actions.
推荐答案
由于动作填充的活动的选项菜单中,您可以使用活动#invalidateOptionsMenu()
。这将转储当前菜单,并再次拨打您的活动的 onCreateOptionsMenu
/ 在prepareOptionsMenu
的方法来重建它。
Since the actions are populated by the activity's options menu you can use Activity#invalidateOptionsMenu()
. This will dump the current menu and call your activity's onCreateOptionsMenu
/onPrepareOptionsMenu
methods again to rebuild it.
如果你使用操作栏标签来改变你的片段配置有一个更好的办法。让每个片段管理自己的菜单部分。这些片段应该叫<一href="http://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu%28boolean%29"><$c$c>setHasOptionsMenu(true)$c$c>.当有选项菜单项中添加或删除系统将自动失效选项菜单,并呼吁每个分段的 onCreateOptionsMenu
/ 在prepareOptionsMenu片段
方法除了活动的。这样,每一个片段可以管理自己的项目,你不必担心手工执行菜单切换。
If you're using action bar tabs to change your fragment configuration there's a better way. Have each fragment manage its own portion of the menu. These fragments should call setHasOptionsMenu(true)
. When fragments that have options menu items are added or removed the system will automatically invalidate the options menu and call to each fragment's onCreateOptionsMenu
/onPrepareOptionsMenu
methods in addition to the activity's. This way each fragment can manage its own items and you don't need to worry about performing menu switching by hand.
这篇关于我如何可以动态地改变操作栏的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!