恢复默认操作栏布局

Lea*_*ros 11 android android-layout actionbarsherlock android-actionbar

我应用自定义ViewActionBar,像这样的

// 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));
Run Code Online (Sandbox Code Playgroud)

(基于Roman Nuriks代码).

如何恢复初始布局?注意:我使用ActionBarSherlock

Tom*_*mik 29

显示/隐藏自定义操作栏视图

由于您只在不删除标题的情况下向条形图添加了自定义视图,因此隐藏该自定义应该足够了View.你可以使用方法setDisplayShowCustomEnabled().只需致电:

getActivity().getActionBar().setDisplayShowCustomEnabled(false);
Run Code Online (Sandbox Code Playgroud)

并再次启用主页功能:

getActivity().getActionBar().setDisplayShowHomeEnabled(true);
Run Code Online (Sandbox Code Playgroud)

(注意在所有代码示例中使用getSupportActionBar()而不是getActionBar()使用actionbar compat.也getActivity()只需要片段,在活动中引用活动本身,在大多数情况下this)

恢复操作栏标题

但是,如果您在创建自定义视图时也删除了标题,则还必须再次启用该标题.

getActivity().getActionBar().setDisplayShowTitleEnabled(true);
Run Code Online (Sandbox Code Playgroud)

完全恢复

您还可以使用一组选项调用setDisplayOptions()方法,以在一次调用中重新配置操作栏.以下示例删除自定义视图并显示标题.

getActivity().getActionBar().setDisplayOptions(
    ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
Run Code Online (Sandbox Code Playgroud)

有关这些选项的详细信息,请参阅Android API文档.