我将自定义 View
应用到 ActionBar
,就像这样
// Inflate the "Done/Discard" custom ActionBar view.
LayoutInflater inflater = (LayoutInflater) DetailsHost.mActionBar
.getThemedContext().getSystemService(DetailsHost.LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(
R.layout.actionbar_custom_view_done_discard, null);
// Show the custom ActionBar view and hide the normal Home icon and title.
DetailsHost.mActionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
DetailsHost.mActionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
(基于 Roman Nuriks 代码)。
如何恢复初始布局?注:我用的是ActionBarSherlock
最佳答案
显示/隐藏自定义操作栏 View
由于您只在栏中添加了一个自定义 View 而没有删除标题,因此隐藏该自定义View
应该就足够了。您可以使用方法 setDisplayShowCustomEnabled()
。只需调用:
getActivity().getActionBar().setDisplayShowCustomEnabled(false);
并再次启用主页功能:
getActivity().getActionBar().setDisplayShowHomeEnabled(true);
(注意在所有代码示例中使用 getSupportActionBar()
而不是 getActionBar()
如果您使用的是 actionbar compat。还有 getActivity( )
仅在 fragment 中需要,在 Activity 中指的是 Activity 本身,在大多数情况下 this
)
恢复操作栏标题
但是,如果您在创建自定义 View 时也删除了标题,则还必须再次启用它。
getActivity().getActionBar().setDisplayShowTitleEnabled(true);
完全恢复
您还可以调用带有选项组合的 setDisplayOptions() 方法,以在一次调用中重新配置操作栏。下面的示例删除自定义 View 并显示标题。
getActivity().getActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
参见 Android API docs有关这些选项的更多详细信息。
关于android - 恢复默认操作栏布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13885179/