如何解决如何在侦听器中更改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 举报,一经查实,本站将立刻删除。