gpt4 book ai didi

java - 更改 fragment 更改主题

转载 作者:行者123 更新时间:2023-11-29 23:05:15 25 4
gpt4 key购买 nike

我需要动态更改 fragment 主题,我尝试了这段代码,但它不适用于 onCreateonCreateView()

getActivity().setTheme(R.style.ActionMode);

如何从 fragment 设置主题?

最佳答案

onCreateView() 方法中,您必须创建 ContextThemeWrapper 并从中扩充主题样式,如下所示:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//create ContextThemeWrapper from the original Activity Context with the custom theme
Context context = new ContextThemeWrapper(getActivity(), R.style.your_Custom_Theme);
//clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(context);
//inflate using the cloned inflater, not the passed in default
return localInflater.inflate(R.layout.your_layout, container, false);
}

关于java - 更改 fragment 更改主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56630381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com