Android學習之——切換應用主題實現日間和夜間效果的更換


前言

智能手機的迅速普及,大大的豐富了我們的娛樂生活。現在大家都喜歡晚上睡覺前玩會兒手機,但是應用的日間模式往往亮度太大,對眼睛有較為嚴重的傷害。
因此,如今的應用往往開發了日間和夜間兩種模式供用戶切換使用,那日間和夜間模式切換究竟是怎樣實現的呢?這就是我們今天學習的內容。

思路

  • 設置主題:

setTheme(int resid)

setTheme()方法應該被在Context中的所有View被實例化之前被調用(例如在setContentView(View)之前)。如下所示:

    setTheme(theme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  • 主題樣式:

為了簡單在這里我們使用繼承自Theme.AppCompat.Light.DarkActionBar的主題樣式來代替日間模式。如有其他要求,可以自定義來實現。

同理,使用繼承自Theme.AppCompat的主題樣式來代替夜間模式。

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>
<style name="AppThemeDark" parent="Theme.AppCompat">

</style>
* 重新生成activity

由setTheme()方法只能在View實例化之前被調用,所以,在切換主題后,需要重新生成一次activity以調用setTheme()方法。

    Intent intent = getIntent();
    overridePendingTransition(0, 0);//不設置進入退出動畫
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();
    overridePendingTransition(0, 0);
    startActivity(intent);
  • 保存主題到本地

使用SharedPreferences保存用戶所選主題到本地。

 public class SharedPreferrenceHelper {
 private static final String THEME = "theme";
 public static void settheme(Context context,String theme){
    SharedPreferences sp = context.getSharedPreferences("demo",Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString(THEME,theme);
    editor.apply();
 }
 public static String gettheme(Context context){
    SharedPreferences sp = context.getSharedPreferences("demo",Context.MODE_PRIVATE);
    return sp.getString(THEME,"1");
 }
 }
  • 獲得用戶所選主題和切換主題

獲得應用主題。

public static int getAppTheme(Context context){
String value = SharedPreferrenceHelper.gettheme(context);
switch (Integer.valueOf(value)){
    case 1:
        return R.style.AppTheme;
    case 2:
        return R.style.AppThemeDark;
    default:
        return R.style.AppTheme;
 }
 }

切換主題。

    public static void switchAppTheme(Context context){
    String value = SharedPreferrenceHelper.gettheme(context);
    switch (Integer.valueOf(value)){
        case 1:
            SharedPreferrenceHelper.settheme(context,"2");
            break;
        case 2:
            SharedPreferrenceHelper.settheme(context,"1");
            break;
        default:
            SharedPreferrenceHelper.settheme(context,"1");
            break;
    }
}

全部代碼

public class MainActivity extends ActionBarActivity {
TextView mTextView;
private int theme = 0;
private static final String TAG = "MainActivity";

@Override
protected void onResume() {
    Log.d(TAG,"onResume");
    super.onResume();
    if(theme==Utils.getAppTheme(this)){

    }else{
       reload();
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG,"onDestroy");
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("theme",theme);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    if(savedInstanceState==null){
        theme=Utils.getAppTheme(this);
    }else{
        theme=savedInstanceState.getInt("theme");
    }
    setTheme(theme);
    super.onCreate(savedInstanceState);
    Log.d(TAG,"onCreate");
    setContentView(R.layout.activity_main);
   ...
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    if(id==R.id.action_switch_theme){
        Utils.switchAppTheme(this);
        reload();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
public void reload() {
    Intent intent = getIntent();
    overridePendingTransition(0, 0);//不設置進入退出動畫
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();

    overridePendingTransition(0, 0);
    startActivity(intent);
}

@Override
protected void onRestart() {
    super.onRestart();
    Log.d(TAG, "onRestart");
}

@Override
protected void onPause() {
    super.onPause();
    Log.d(TAG,"onPause");
}
}

效果圖

效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM