ChangeThemeForAndoird: 為Android應用設置主題,而不用重新運行Activity,例如可以無閃屏的切換日間、夜間模式,類似于知乎App
Android主題切換精簡庫
以簡單的方式實現日間、夜間模式。
一、使用方式
1.1 自定義屬性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 自定義屬性 -->
<attr name="root_view_bg" format="reference|color" />
<attr name="btn_bg" format="reference|color" />
<attr name="text_color" format="reference|color" />
</resources>1.2 在布局中使用自定義屬性設置View的背景、文本顏色等屬性
activity_main.xml中的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/root_view_bg"
tools:context="com.example.androidthemedemo.MainActivity" >
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/change_theme"
android:textColor="?attr/text_color"
android:textSize="20sp" />
<Button
android:id="@+id/change_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textview"
android:layout_marginTop="20dp"
android:text="@string/change_theme"
android:textColor="?attr/text_color" />
<Button
android:id="@+id/second_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/change_btn"
android:layout_marginTop="20dp"
android:text="@string/sec_act"
/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/second_btn"
android:layout_marginTop="20dp" />
</RelativeLayout>例如上述布局中我們將root_view的背景設置為"?attr/root_view_bg",代表它的背景是自定義屬性root_view_bg的值,還有Textview和Button的textColor屬性設置為"?attr/text_color"。
1.3 定義多個Theme
然后在不同的Theme中為這些屬性設置不同的值,例如,通常我們有日間和夜間模式兩種顏色模式。styles.xml中的完整代碼如下:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
</style>
<!-- 日間主題 -->
<style name="DayTheme" parent="AppTheme">
<item name="root_view_bg">@drawable/bg_day</item>
<item name="btn_bg">@color/white_btn_color</item>
<item name="text_color">@color/black_tx_color</item>
</style>
<!-- 夜間主題 -->
<style name="NightTheme" parent="AppTheme">
<item name="root_view_bg">@drawable/bg_night</item>
<item name="btn_bg">@color/black_btn_color</item>
<item name="text_color">@color/white_tx_color</item>
</style>
</resources>兩個主題下為同一個屬性設置了不同的值,達到切換主題時修改View的相關屬性的目的。例如定義在colors.xml中的顏色值。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 日間模式 -->
<color name="white_btn_color">#3BB32E</color>
<color name="black_tx_color">#333333</color>
<!-- 夜間模式 -->
<color name="black_btn_color">#aa7788</color>
<color name="white_tx_color">#f0f0f0</color>
</resources>1.4 設置要修改的View的屬性
下面我們為activity_main.xml中的視圖進行換膚設置:
ListView mNewsListView = (ListView) findViewById(R.id.listview);
// 為ListView設置要修改的屬性,在這里沒有對ListView本身的屬性做修改
ViewGroupSetter listViewSetter = new ViewGroupSetter(mNewsListView, 0);
// 綁定ListView的Item View中的news_title視圖,在換膚時修改它的text_color屬性
listViewSetter.childViewTextColor(R.id.news_title, R.attr.text_color);
// 構建Colorful對象
Colorful mColorful = new Colorful.Builder(this)
.backgroundDrawable(R.id.root_view, R.attr.root_view_bg) // 設置view的背景圖片
.backgroundColor(R.id.change_btn, R.attr.btn_bg) // 設置按鈕的背景色
.textColor(R.id.textview, R.attr.text_color) // 設置文本顏色
.setter(listViewSetter) // 手動設置setter
.create();首先我們定義了一個listViewSetter,該Setter用于為ListView的每個Item View中的news_title控件設置文本顏色,文本顏色的值是自定義屬性text_color的顏色值。然后構建Colorful對象,并且id分別為change_btn、root_view、textview的控件綁定特定屬性值,例如backgroundDrawable(R.id.root_view, R.attr.root_view_bg)代表root_view的背景Drawable為自定義屬性root_view_bg的值,textColor(R.id.textview, R.attr.text_color)表示id為textview的TextView控件的文本顏色為R.attr.text_color的值。這些屬性都在不同的Theme中有不同的值,因此切換Theme時就會發生變化。然后我們將listViewSetter添加到Colorful對象中,在修改主題時被遍歷ListView中的所有Item View,然后修改news_title控件的文本顏色。
1.5 切換主題
最后通過Colorful對象設置主題即可實現切換,代碼如下:
boolean isNight = false ;
// 切換主題
private void changeThemeWithColorful() {
if (!isNight) {
mColorful.setTheme(R.style.DayTheme);
} else {
mColorful.setTheme(R.style.NightTheme);
}
isNight = !isNight;
}