我想知道是否有一种方法可以让 ActionBar
在我的 Activity
的一个 Fragment
的主题中而其他 Fragment
将与 Application
的主题一起去 ActionBar
?
本质上,我如何为我的一个 fragment 设置一个单独的主题,使其覆盖应用程序的主题?
最佳答案
2个选项
您可以使用
隐藏操作栏getActivity().getActionBar().hide();
如果使用支持库,不要忘记使用 getSupportActionBar()
- 第二个选项是使用 Theme.NoActionBar 为 fragment 设置一个新主题
我认为您需要 ContextThemeWrapper。在 fragment 的 onCreateView 中
您创建包含在主题中的 Activity 上下文的副本。
Context themeWrapper = new ContextThemeWrapper(getActivity(), R.style.myTheme);
然后创建现有 LayoutInflater 对象的副本,并将副本用于您的主题包装上下文。
LayoutInflater inflaterCopy = inflater.cloneInContext(themeWrapper);
然后使用复制的 inflater 为您的布局充气,因为它包含您的包装上下文。
View view = localInflater.inflate(R.layout.fragment, container, false);
将它放在 onCreateView 中并使用其布局 inflater 进行复制
关于Android:具有操作栏的众多 fragment 之一,而其他 fragment 则没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26644169/