android - 恢复默认操作栏布局

标签 android android-layout android-actionbar actionbarsherlock

我将自定义 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/

相关文章:

当我在设备上运行时,Android 数据库应用程序卡住然后崩溃

java - (TensorflowLite/Android) 无法实例化 Activity ComponentInfo

android - 显示来自广播接收器的透明 Activity

java - Android:以英寸为单位设置 View 宽度(在英寸和 dp 之间转换)

android - 在 android 中设置 actionbar 的样式

android - StickyHeaderListView 与 ActionBarPullToRefresh 重叠

android - 如何在android中按下菜单按钮时显示按钮

android - 避免在 CoordinatorLayout 内的 ViewPager 内的底部 View 中滚动

java - 不幸的是,该应用程序已停止工作 - android

抽屉导航 fragment 中的 Android 操作栏选项卡