日夜主题在android中没有正确实现
出于某些原因,我在我的应用程序中实现了android Day/Night主题 1) 我无法更改文本的颜色,而且为了使其正常工作,我必须单击切换按钮并返回主屏幕,这意味着设置页面中未显示实现,必须通过进入主页刷新 2) 我面临的第二个问题是,当白天/晚上的主题被应用时,我的recyclerView帖子会回到列表中的1st,而不仅仅是应用到原来的位置 请帮忙 我正在使用Android studio 日间/夜间主题类文件:日夜主题在android中没有正确实现,android,layout,themes,android-appcompat,android-theme,Android,Layout,Themes,Android Appcompat,Android Theme,出于某些原因,我在我的应用程序中实现了android Day/Night主题 1) 我无法更改文本的颜色,而且为了使其正常工作,我必须单击切换按钮并返回主屏幕,这意味着设置页面中未显示实现,必须通过进入主页刷新 2) 我面临的第二个问题是,当白天/晚上的主题被应用时,我的recyclerView帖子会回到列表中的1st,而不仅仅是应用到原来的位置 请帮忙 我正在使用Android studio 日间/夜间主题类文件: public class settings extends AppCompat
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 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 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>
<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 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 name="TextP">#000000</color>
<color name="TextP">#FFFFFF</color>
彩色夜xml文件:
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 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 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>
<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 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 name="TextP">#000000</color>
<color name="TextP">#FFFFFF</color>
#222222
#222222
#FF4081
#FFFFFF
#000000
风格之夜xml:
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 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 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>
<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 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 name="TextP">#000000</color>
<color name="TextP">#FFFFFF</color>
@彩色/彩色PrimaryNight
@颜色/原色达奈特
@色彩/色彩之夜
@彩色/彩色背景夜
@颜色/文本颜色PrimaryNight
样式xml:
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 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 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>
<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 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 name="TextP">#000000</color>
<color name="TextP">#FFFFFF</color>
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@颜色/文本颜色主
假的
真的
彩色xml:
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 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 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>
<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 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 name="TextP">#000000</color>
<color name="TextP">#FFFFFF</color>
#824c83dd
#000000
#303f9f
#FF4081
#00BDBD
#FFFFFF
在color-night.xml中添加您在normal color.xml中使用的字符串怎么样<代码>#000000我通过添加以下代码成功实现了颜色校正部分:
为xml文件着色:
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 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 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>
<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 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 name="TextP">#000000</color>
<color name="TextP">#FFFFFF</color>
#000000
彩色夜xml文件:
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 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 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>
<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 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 name="TextP">#000000</color>
<color name="TextP">#FFFFFF</color>
#FFFFFF
然后在colors.xml中为我的文本指定与上面相同的颜色
但其余部分对我来说仍然是个问题,所以如果有人能帮忙,请。这是关于android studio中的代码的问题,而不是关于android studio的问题。不要使用Android Studio标签来询问ASHave中的代码,因为它已经在color-night.xml中添加了textColorPrimaryNight。这似乎是一个问题。对我来说,有时候安卓猜对了颜色,有时候就错了