【发布时间】:2017-12-05 00:46:05
【问题描述】:
是否可以将整个应用的主题从片段更改为深色主题?我的想法是我有一个可以切换以在深色/浅色主题之间切换的开关。这一切都是在一个片段中完成的,所以我开始有点困惑。这可能吗>
这是那个监听器:
Switch switchTheme = (Switch) view.findViewById(R.id.switchTheme);
switchTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// switchDark(getView(), isChecked);
TextView txt = (TextView) getView().findViewById(R.id.lblDark);
if (isChecked) {
txt.setText("checked");
// switchDark(getView(), isChecked);
(MainActivity) getActivity().getTheme().applyStyle(R.style.AppThemeDark, true);
}
else{
txt.setText("unchecked");
}
}
});
这是我的styles.xml:
<!-- 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>
<!-- colors for the dark theme -->
<style name="AppThemeDark" parent="Theme.AppCompat">
<item name="colorPrimary">@color/darkColorPrimary</item>
<item name="colorPrimaryDark">@color/darkColorPrimaryDark</item>
<item name="colorAccent">@color/darkColorAccent</item>
</style>
<style name="AppThemeDark.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.Dark.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.Dark.PopupOverlay" parent="ThemeOverlay.AppCompat" />
这是我的colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorGrey">#D3D3D3</color>
<color name="colorTransparent">#FFFFFF</color>
<color name="colorLearned">#32CD32</color>
<!-- app/src/main/res/values/colors.xml -->
<color name="darkColorPrimary">#1e2756</color>
<color name="darkColorPrimaryDark">#141831</color>
<color name="darkColorAccent">#2aac4b</color>
</resources>
有没有更简单的方法来做到这一点?有没有办法我可以通过代码访问 styles.xml 和 color.xml 并自己手动更改颜色?
例如,我可以直接做类似getResources().getColor(R.color.colorAccent) = "FFFFFF"; 的事情吗?
更新我现在所做的是在主要活动中创建了一个函数,它是这样的:
public void ToggleTheme(boolean isChecked){
if (isChecked) {
this.getTheme().applyStyle(R.style.AppThemeDark_NoActionBar, true);
}
else{
this.getTheme().applyStyle(R.style.AppTheme, true);
}
}
然后像这样在片段内部调用它:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_settings, container, false);
Switch switchTheme = (Switch) view.findViewById(R.id.switchTheme);
switchTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// switchDark(getView(), isChecked);
TextView txt = (TextView) getView().findViewById(R.id.lblDark);
if (isChecked) {
txt.setText("checked");
// switchDark(getView(), isChecked);
// getResources().getColor(R.color.colorPrimary);
((MainActivity) getActivity()).ToggleTheme(isChecked);
}
else{
txt.setText("unchecked");
((MainActivity) getActivity()).ToggleTheme(isChecked);
}
}
});
return view;
}
它似乎改变了主题,但保持背景为白色。我已经检查过了,我的颜色确实设置为应该生效的较暗颜色。
【问题讨论】:
-
你应该做两个主题然后 (MainActivity) getActivity().setTheme(R.style.DarkThemeHere);
-
我会用我现在所拥有的来更新我的问题。