【Android:簡單夜間模式切換實現】夜間模式切換,完整代碼奉上!

找過很多博客,全特麼一套理論,沒有完整的源碼,根本都實現不了,

包括鴻洋dalao的,沒有源碼什麼都是浮雲...

掘金的文章說要SDK23.0以上纔有類庫可以調用....

(Android 6.0以下真是哭暈啊!)


項目的構成:之前有介紹過文件資源管理器,但是還是有些東西是沒有接觸過呢!



實現效果:主頁面

 

第二個頁面:




雖然圖片還是有點亮,最好的實現還是加蒙層吧....→、→


一、2個方法:

MainActivity.java 主方法,切換主題,

另外一個activity 是看看切換主題後有,xml頁面有沒有收到影響。


MainActivity.java

public class MainActivity extends Activity {

    private static boolean blFlag = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences preferences = getSharedPreferences("default_night", MODE_PRIVATE);
        blFlag = preferences.getBoolean("default_night",true);
        if (blFlag) {
            this.setTheme(R.style.BrowserThemeDefault);
        }
        else {
            this.setTheme(R.style.BrowserThemeNight);
        }

        setContentView(R.layout.activity_main);
    }

    public void btonclick(View view) {
        SharedPreferences preferences = getSharedPreferences("default_night",MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        if (blFlag) {
            this.setTheme(R.style.BrowserThemeNight);
            blFlag =false;
            editor.putBoolean("default_night",false);
        } else {
            this.setTheme(R.style.BrowserThemeDefault);
            blFlag = true;
            editor.putBoolean("default_night",true);

        }
        // 提交修改
        editor.commit();
        this.setContentView(R.layout.activity_main);
    }

    public void btonclick2(View view) {
        Intent intent = new Intent();
        intent.setClass(this, breakActivity.class);
        startActivity(intent);
    }


}


breakActivity.java

public class breakActivity extends Activity {
   private static boolean blFlag = false;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      SharedPreferences preferences = getSharedPreferences("default_night",
            MODE_PRIVATE);
      blFlag = preferences.getBoolean("default_night",true);
      if (blFlag) {
         this.setTheme(R.style.BrowserThemeDefault);
      }
      else {
         this.setTheme(R.style.BrowserThemeNight);
      }
      setContentView(R.layout.activity2);
   }
}


二、2個xml:跟以往有些不同

以前的text,background我們都是直接設定顏色的:

android:textColor="@android:color/black" 

現在是 

android:textColor="?tvcolor"

?是什麼意思呢?下面的values文件會解釋

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?bookimage"
    tools:context="example.com.night_mode.MainActivity">

    <TextView
        android:textColor="?tvcolor"
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello_world"
        android:textSize="16dp"
        android:gravity="center"
        android:layout_marginTop="10dp"/>

    <TextView
        android:textColor="?tvcolor2"
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="隨便測試"
        android:textSize="16dp"
        android:gravity="center"/>

    <Button android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日/夜間模式切換"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="btonclick"
        android:layout_gravity="center"/>

    <Button android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="跳轉其他頁面"
        android:layout_below="@id/textView1"
        android:layout_centerHorizontal="true"
        android:onClick="btonclick2"
        android:layout_gravity="center"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/p3"/>

</LinearLayout>

activity2.xml

<LinearLayout
   android:background="?bookimage"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
   android:id="@+id/textView3"
   android:textColor="?tvcolor2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="初音未來"
   android:textSize="22dp"
   android:layout_gravity="center"
   android:layout_marginTop="20dp"/>
   
<TextView
   android:textColor="?tvcolor"
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content"
   android:text="甩蔥歌"
   android:textSize="22dp"
   android:layout_gravity="center"
   android:layout_marginTop="20dp"/>

   </LinearLayout>


三、values文件:

一個很少見的attrs.xml :

這裏有點像javaweb中ssh框架中的struts.xml了,可以管理所有的方法



styles.xml:寫個白天和黑夜2套主題,換膚的可以參考


attrs.xml:這裏我們有三個文件,

分別是:背景色bookimage,文本顏色一:tvcolor,文本顏色二:tvcolor2

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="bookimage" format="reference|color" />
    <attr name="tvcolor" format="reference|color" />
    <attr name="tvcolor2" format="reference|color"/>
</resources>

styles.xml文件的2個主題:

默認:背景白色,文本一黑色,文本二藍色;

夜間:背景黑色,文本一白色,文本二白色;

<!-- 默認風格 -->
<style name="BrowserThemeDefault" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="bookimage">@android:color/white</item>
    <item name="tvcolor">@android:color/black</item>
    <item name="tvcolor2">@android:color/holo_blue_bright</item>
</style>


<!-- 夜間模式 -->
<style name="BrowserThemeNight" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="bookimage">@android:color/darker_gray</item>
    <item name="tvcolor">@android:color/white</item>
    <item name="tvcolor2">@android:color/white</item>
</style>

這裏只能調用類庫中有的顏色,我試了下color自定義的顏色,

居然不行!

要是有人解決了告訴我一下....

有問題去貼吧提問,謝謝!












發佈了73 篇原創文章 · 獲贊 147 · 訪問量 40萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章