Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

夜間模式實戰:changeskin 換膚庫的使用總結與踩坑記錄

編輯:Android 資訊

項目要做夜間模式,選用的是鴻洋的 changeskin——接入成本低,效果穩定。以下把使用方式和實踐中遇到的問題一併記錄。

接入方式

作為 module 依賴引入:

dependencies {
    compile project(':changeskin')
}

或直接添加二進制依賴:

dependencies {
    compile 'com.zhy:changeskin:3.0.0'
}

補充一句:最初用的是二進制依賴,後來因為需要支持自定義 View,只能把整個庫源碼放進項目,否則無法識別自有控件的屬性。

第一步:Application 中初始化

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        SkinManager.getInstance().init(this);
    }
}

第二步:Activity 繼承 BaseSkinActivity

所有需要換膚的 Activity 都繼承 com.zhy.changeskin.base.BaseSkinActivity。

第三步:資源命名約定

需要參與換膚的資源統一以 skin 開頭,例如背景:

<android.support.v4.widget.DrawerLayout
    android:background="@drawable/skin_main_bg" ... />

文字顏色:

<TextView
    android:id="@+id/id_tv_title"
    android:textColor="@color/skin_item_text_color" />

應用內換膚

準備一套皮膚資源:命名規則是在原名後加後綴,比如 skin_main_bg 對應 skin_main_bg_red。然後調用:

SkinManager.getInstance().changeSkin("red");

參數就是後綴名。

實踐中的注意事項

1. 後期動態生成的 View 必須 inject。比如 adapter 中的 item、通過 include 引入的布局,都需要手動 inject,否則不會參與換膚。

2. 多屬性支持。同一個 View 上可以疊加多組皮膚屬性,形如 skin:white:textColor|skin:orange:background。

3. 自定義 View 屬性支持。庫無法識別自有控件的屬性,做法是把庫源碼集成進項目,並在枚舉中添加自己的類型:

ROUNDPROGRESSCOLOR("roundProgressColor") {
    @Override
    public void apply(View view, String resName) {
        if (view instanceof RoundProgressBar) {
            int color = getResourceManager().getColor(resName);
            if (color == -1) return;
            ((RoundProgressBar) view).setCricleProgressColor(color);
        }
    }
}

4. background 現已支持 color。早期版本只認 drawable,作者後續補上了顏色分支:

BACKGROUND("background") {
    @Override
    public void apply(View view, String resName) {
        Drawable drawable = getResourceManager().getDrawableByName(resName);
        if (drawable != null) {
            view.setBackgroundDrawable(drawable);
        } else {
            int color = getResourceManager().getColor(resName);
            if (color == -1) return;
            view.setBackgroundColor(color);
        }
    }
}

5. getDrawableByName 也補上了 color 支持。先按 drawable 查找,失敗後再退回 color 查找:

public Drawable getDrawableByName(String name) {
    try {
        name = appendSuffix(name);
        return mResources.getDrawable(
                mResources.getIdentifier(name, DEFTYPE_DRAWABLE, mPluginPackageName));
    } catch (Resources.NotFoundException e) {
        try {
            return mResources.getDrawable(
                    mResources.getIdentifier(name, DEFTYPE_COLOR, mPluginPackageName));
        } catch (Resources.NotFoundException e2) {
            e.printStackTrace();
            return null;
        }
    }
}

6. 頭像蒙版有個小技巧。夜間模式下的頭像蒙版不必額外做邏輯:白天把蒙層設為透明,夜間設為黑色即可。

7. 根布局的 skin 屬性不生效。目前只能在外層再包一個容器布局繞過,屬於權宜之計。

尚未解決的問題

問題一:padding 莫名失效。庫裡並沒有對 padding 做限制,但實踐中多次遇到:當背景是 .9.png 或 XML 定義的 drawable,並且在夜間屬性生效之後,原本設定的 padding 會失效。原因暫未定位。

問題二:隱藏 header 後 SearchBar 無法切換。搜索欄放在 header 中,首次顯示時換膚正常;一旦 header 被隱藏,SearchBar 的夜間模式就切不動了,而 header 保持顯示時則一切正常。

小結

changeskin 的整體思路是「命名約定 + 屬性攔截 + 資源包替換」,接入成本很低。真正花時間的地方在邊界情況:自定義 View、動態生成的 item、以及像 padding 失效這種難以定位的怪現象。把庫源碼放進項目看起來麻煩,但在需要擴展屬性時,這是最直接的出路。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved