如何在侦听器中更改Android Studio中颜色选择器片段中的AppTheme

问题描述

我正在尝试实现颜色选择器库-https://github.com/kristiyanP/colorpicker 目的是根据用户选择的颜色更改应用程序主题(标题栏,操作栏和其他元素)。 到目前为止,我已经能够实现libary并根据所选颜色更改片段的背景颜色。但是,我的目的是改变主题本身,而不仅仅是背景。

style.xml文件-

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#E91E63</item>
    <item name="colorPrimaryDark">#F44336</item>
    <item name="colorAccent">#673AB7</item>
</style>

点击按钮即可实现应用更改主题的片段-

public class SettingsFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_settings,container,false);
    final FrameLayout themeframe = (FrameLayout) view.findViewById(R.id.settingLayout);
    Button themeBtn = (Button)view.findViewById(R.id.colorButton);


    themeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            final ColorPicker colorPicker = new ColorPicker(getActivity());
            ArrayList<String> colors = new ArrayList<>();
            colors.add("#258174");
            colors.add("#3C8D2F");
            colors.add("#20724f");
            colors.add("#6a3ab2");
            colors.add("#6a3ab2");
            colorPicker.setColors(colors)
                    .setColumns(5)
                    .setRoundColorButton(true)
                    .setOnChooseColorListener(new ColorPicker.OnChooseColorListener() {
                        @Override
                        public void onChooseColor(int position,int color) {
                            themeframe.setBackgroundColor(color); //Changes the fragment background color
                                  
                            // code to change the actionbar and title bar color throughout the app with saved preferences option to save theme on reopening

   

                     }

                        @Override
                        public void onCancel() {

                        }
                    }).show();
        }
    });
    return view;
}

}

到目前为止,我尝试使用getApplication()和set主题,但两者似乎都不适合。 color是用于确定应用程序主题/操作栏和标题栏颜色的变量。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...