Android 切换应用风格,夜间模式

加入SharedPreference标志,,可以记忆上次选用的风格,从而下次启动时不必重置。

package com.zms.nightstyle;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class Main extends Activity {private boolean isNight = false;private Button btnSet;private Button btnGet;private SharedPreferences sharedPreferences;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);sharedPreferences = getSharedPreferences("UseStyle", Context.MODE_WORLD_READABLE);isNight = sharedPreferences.getBoolean("isNight", false);if (isNight) {this.setTheme(R.style.MyThemeNight);} else {this.setTheme(R.style.MyThemeDefault);}setContentView(R.layout.main);btnSet = (Button) findViewById(R.id.btnSet);btnGet = (Button) findViewById(R.id.btnGet);btnSet.setOnClickListener(new onClickListenerImp());btnGet.setOnClickListener(new onClickListenerImp());}class onClickListenerImp implements View.OnClickListener {@Overridepublic void onClick(View v) {if (v == btnSet) {Editor editor = sharedPreferences.edit();if (isNight) {setTheme(R.style.MyThemeDefault);isNight = false;} else {setTheme(R.style.MyThemeNight);isNight = true;}editor.putBoolean("isNight", isNight);editor.commit();setContentView(R.layout.main);btnSet = (Button) findViewById(R.id.btnSet);btnGet = (Button) findViewById(R.id.btnGet);btnSet.setOnClickListener(new onClickListenerImp());btnGet.setOnClickListener(new onClickListenerImp());} else if (v == btnGet) {Toast.makeText(Main.this, "isNight: " + isNight, Toast.LENGTH_SHORT).show();}}}}两种风格主题:

<?xml version="1.0" encoding="utf-8"?><resources><!– 默认主题 –><style name="MyThemeDefault" parent="@android:style/Theme"><item name="btnColor">#00ff00</item><item name="mainBackground">#ffffff</item><item name="mainTextColor">#000000</item><item name="textString">默认主题</item></style><!– 夜间主题 –><style name="MyThemeNight" parent="@android:style/Theme"><item name="btnColor">#0000ff</item><item name="mainBackground">#000000</item><item name="mainTextColor">#ffffff</item><item name="textString">夜间主题</item></style></resources>布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="?mainBackground"android:orientation="vertical"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="?textString" /><ImageViewandroid:id="@+id/ivBook"android:layout_width="62dip"android:layout_height="42dip"android:layout_gravity="center"android:layout_marginTop="0dip"android:gravity="center"android:src="?btnColor" /><Buttonandroid:id="@+id/btnSet"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="50dp"android:text="改变主题" /><Buttonandroid:id="@+id/btnGet"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="50dp"android:text="Get Flag" /></LinearLayout>我的GitHub:周木水的GitHubhttps://github.com/zhoumushui

在乎的是沿途的风景以及看风景的心情,让心灵去旅行!

Android 切换应用风格,夜间模式

相关文章:

你感兴趣的文章:

标签云: