java - 更改 fragment 更改主题

标签 java android fragment themes

我需要动态更改 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/

相关文章:

java - 在 postgresql 中使用文本类型列转义双引号并通过 jackson 解析 json

java - 如何在 JUNIT 测试中使用 assertEquals 或任何其他方式比较两个对象?

android - 如果您尝试整洁架构,使用 Dagger 的最佳方法是什么?

java - 如何将ListView添加到fragment_main.xml

Android 错误 - FragmentTransaction 类型中的方法 replace(int, Fragment) 不适用于参数 (int, ListFragment)

java - 如何使用 Java 中的通用方法来 CRUD MySQL 中的任何表,使用带有 Criteria 的 Hibernate,传递表名和/或字段作为参数?

java - 我们如何去除 libGDX 中的背面纹理?

android - 找不到 keystore 条目。但我知道它在那里

android - 使用触摸监听器围绕其中心旋转 android 布局 View

android - fragment 回栈,android?