今天进行了夜间模式的学习,出现的问题是:目前只能改按钮的主题,无法更改所有页面的主题。
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
app:titleTextColor="?attr/titleColor"
app:title="设置"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="黑夜模式"
android:textColor="?attr/tvMainColor"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pref_night_summary"
android:textColor="?attr/tvSubColor"
android:textSize="16dp" />
</LinearLayout>
<Switch
android:id="@+id/nightMode"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:clickable="true"
android:gravity="center"
android:switchMinWidth="50dp"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:foreground="@color/greyC"
/>
package com.xrj.night;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.CompoundButton;
import android.widget.Switch;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar;
public class UserSettingsActivity extends BaseActivity {
private Switch nightMode;
private SharedPreferences sharedPreferences;
private Boolean night_change;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preference_layout);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Intent intent = getIntent();
/*
if(intent.getExtras() != null) night_change = intent.getBooleanExtra("night_change", false);
else night_change = false;
*/
initView();
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(isNightMode()) myToolbar.setNavigationIcon(getDrawable(R.drawable.ic_settings_black_24dp));
else myToolbar.setNavigationIcon(getDrawable(R.drawable.ic_settings_white_24dp));
}
public void initView(){
nightMode = findViewById(R.id.nightMode);
nightMode.setChecked(sharedPreferences.getBoolean("nightMode", false));
nightMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setNightModePref(isChecked);
setSelfNightMode();
}
});
}
private void setNightModePref(boolean night){
//通过nightMode switch修改pref中的nightMode
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("nightMode", night);
editor.commit();
}
private void setSelfNightMode(){
//重新赋值并重启本activity
super.setNightMode();
Intent intent = new Intent(this, UserSettingsActivity.class);
//intent.putExtra("night_change", !night_change); //重启一次,正负颠倒。最终为正值时重启MainActivity。
startActivity(intent);
finish();
}
}