Day/Night 主题未在 android 中正确实施
Day/Night theme is not implementing correctly in android
出于某些原因,我在我的应用程序中实施了 android Day/Night 主题。
1) 我 无法 更改文本的 颜色 并且要使其正常工作,我必须单击切换按钮然后返回主屏幕,这意味着 未 显示在 设置 页面中,必须 刷新 通过转到主页
2) 我面临的第二个问题是,当应用 day/night 主题时,我的 recyclerView 帖子回到 1st 在列表中,不要只是在原来的地方应用。
请帮忙!!
我正在使用 Android 工作室
Day/Night 主题 class 文件:
public class settings extends AppCompatActivity {
private ToggleButton DayNightt;
private static Bundle bundle = new Bundle();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_page);
DayNightt = (ToggleButton) findViewById(R.id.dayNight_Switch);
DayNightt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if (isChecked){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
});
}
@Override
public void onPause() {
super.onPause();
bundle.putBoolean("ToggleButtonState", DayNightt.isChecked());
}
@Override
public void onResume() {
super.onResume();
DayNightt.setChecked(bundle.getBoolean("ToggleButtonState",false));
}
}
夜色xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimaryNight">#222222</color>
<color name="colorPrimaryDarkNight">#222222</color>
<color name="colorAccentNight">#FF4081</color>
<color name="textColorPrimaryNight">#FFFFFF</color>
<color name="colorBackgroundNight">#000000</color>
</resources>
风格之夜xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimaryNight</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkNight</item>
<item name="colorAccent">@color/colorAccentNight</item>
<item name="android:windowBackground">@color/colorBackgroundNight</item>
<item name="android:textColorPrimary">@color/textColorPrimaryNight</item>
</style>
</resources>
样式xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
颜色xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#824c83dd</color>
<color name="textColorPrimary">#000000</color>
<color name="colorPrimaryDark">#303f9f</color>
<color name="colorAccent">#FF4081</color>
<color name="SourceColor">#00bdbdbd</color>
<color name="background">#FFFFFF</color>
</resources>
添加color-night.xml这个你在正常color.xml中使用的字符串怎么样? <color name="textColorPrimary">#000000</color>
我已经通过添加以下代码成功实现了颜色校正部分:
颜色 xml 文件:
<color name="TextP">#000000</color>
夜色xml 文件:
<color name="TextP">#FFFFFF</color>
然后在 colors.xml
中为我的文本分配与上面相同的颜色
但其余部分对我来说仍然是个问题所以如果有人可以提供帮助,请。
出于某些原因,我在我的应用程序中实施了 android Day/Night 主题。
1) 我 无法 更改文本的 颜色 并且要使其正常工作,我必须单击切换按钮然后返回主屏幕,这意味着 未 显示在 设置 页面中,必须 刷新 通过转到主页
2) 我面临的第二个问题是,当应用 day/night 主题时,我的 recyclerView 帖子回到 1st 在列表中,不要只是在原来的地方应用。
请帮忙!!
我正在使用 Android 工作室
Day/Night 主题 class 文件:
public class settings extends AppCompatActivity {
private ToggleButton DayNightt;
private static Bundle bundle = new Bundle();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_page);
DayNightt = (ToggleButton) findViewById(R.id.dayNight_Switch);
DayNightt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if (isChecked){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
});
}
@Override
public void onPause() {
super.onPause();
bundle.putBoolean("ToggleButtonState", DayNightt.isChecked());
}
@Override
public void onResume() {
super.onResume();
DayNightt.setChecked(bundle.getBoolean("ToggleButtonState",false));
}
}
夜色xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimaryNight">#222222</color>
<color name="colorPrimaryDarkNight">#222222</color>
<color name="colorAccentNight">#FF4081</color>
<color name="textColorPrimaryNight">#FFFFFF</color>
<color name="colorBackgroundNight">#000000</color>
</resources>
风格之夜xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimaryNight</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkNight</item>
<item name="colorAccent">@color/colorAccentNight</item>
<item name="android:windowBackground">@color/colorBackgroundNight</item>
<item name="android:textColorPrimary">@color/textColorPrimaryNight</item>
</style>
</resources>
样式xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
颜色xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#824c83dd</color>
<color name="textColorPrimary">#000000</color>
<color name="colorPrimaryDark">#303f9f</color>
<color name="colorAccent">#FF4081</color>
<color name="SourceColor">#00bdbdbd</color>
<color name="background">#FFFFFF</color>
</resources>
添加color-night.xml这个你在正常color.xml中使用的字符串怎么样? <color name="textColorPrimary">#000000</color>
我已经通过添加以下代码成功实现了颜色校正部分:
颜色 xml 文件:
<color name="TextP">#000000</color>
夜色xml 文件:
<color name="TextP">#FFFFFF</color>
然后在 colors.xml
中为我的文本分配与上面相同的颜色但其余部分对我来说仍然是个问题所以如果有人可以提供帮助,请。