繁体   English   中英

如何在片段上为整个应用设置主题?

[英]How to set the theme for the whole app on a fragment?

如何在片段上设置应用程序主题:这是我到目前为止想出的:

public class settings_fragment extends Fragment {
    Switch switch1;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.settings_fragment,container,false);

        switch1 = view.findViewById(R.id.switch1);

        //check if theme is in dark mode or not
        if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES){
            setTheme //This gives me an error, but it would work if it wasn't on a fragment
        }
        return view;
    }
}

您可以尝试调用 getActivity().setTheme(),然后调用 getActivity。 重新创建,但您的片段也将被重新创建。 所以我认为你需要的是在超级调用之前在你的容器活动 onCreate 方法中设置检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM