Android 切換應用風格,夜間模式

來源:互聯網
上載者:User

標籤:android   夜間模式   風格切換   主題切換   style   

加入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;    @Override    public 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 {        @Override        public 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="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="?mainBackground"    android:orientation="vertical">    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="?textString" />    <ImageView        android: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" />    <Button        android:id="@+id/btnSet"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="50dp"        android:text="改變主題" />    <Button        android:id="@+id/btnGet"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="50dp"        android:text="Get Flag" /></LinearLayout>

轉載請註明出處:周木水的CSDN部落格 http://blog.csdn.net/zhoumushui 
我的GitHub:周木水的GitHub https://github.com/zhoumushui

Android 切換應用風格,夜間模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.