今天進行了夜間模式的學習,出現的問題是:目前只能改按鈕的主題,沒法更改全部頁面的主題。
android
<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;app
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;ide
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar;學習
public class UserSettingsActivity extends BaseActivity {ui
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(); }
}this