如何将可以在运行时更改的主题应用到 UI?

How to apply a Theme, which could be changed in runtime, to the UI?

我的应用程序的用户可以更改应用程序的主题。 您可以在 Play 商店中查看:https://play.google.com/store/apps/details?id=at.guger.musixs

到目前为止,每个活动 onApplyThemeResource-方法都被覆盖。

这是非常昂贵的,但不能以一种方式完成,因为每个 activity 都有自己的特点,例如这个没有ActionBar。

还有一个问题。这些样式不适用于AlertDialogs,如果您检查我的应用程序,您会看到!

那么有没有更好的样式应用方式呢?我看到了一个名为 AppThemeEngine 的模块,它可以绕过主题,但是这个需要超级用户权限,所以它对普通客户没有用。

@Override
protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
    super.onApplyThemeResource(theme, resid, first);

    mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    int iThemeID = R.style.NoActionBarThemeBlue;

    String sPrefBlue = getResources().getString(R.string.pref_color_blue);
    String sPrefGray = getResources().getString(R.string.pref_color_gray);
    String sPrefGreen = getResources().getString(R.string.pref_color_green);
    String sPrefOrange = getResources().getString(R.string.pref_color_orange);
    String sPrefRed = getResources().getString(R.string.pref_color_red);

    String sPrefColor = mPreferences.getString(getResources().getString(R.string.keyThemeColor), sPrefBlue);

    if (sPrefColor.equals(sPrefBlue)) {
        iThemeID = R.style.NoActionBarThemeBlue;
    }
    else if (sPrefColor.equals(sPrefGray)) {
        iThemeID = R.style.NoActionBarThemeGray;
    }
    else if (sPrefColor.equals(sPrefGreen)) {
        iThemeID = R.style.NoActionBarThemeGreen;
    }
    else if (sPrefColor.equals(sPrefOrange)) {
        iThemeID = R.style.NoActionBarThemeOrange;
    }
    else if (sPrefColor.equals(sPrefRed)) {
        iThemeID = R.style.NoActionBarThemeRed;
    }

    RuntimeInfo.setThemeID(iThemeID);

    theme.applyStyle(iThemeID, true);
}

我的styles.xml:

<resources>
    <style name="ActionBarThemeBlue" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="colorPrimary">@color/bluePrimary</item>
        <item name="colorPrimaryDark">@color/bluePrimaryDark</item>
        <item name="colorAccent">@color/blueAccent</item>
    </style>

    <style name="NoActionBarThemeBlue" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="colorPrimary">@color/bluePrimary</item>
        <item name="colorPrimaryDark">@color/bluePrimaryDark</item>
        <item name="colorAccent">@color/blueAccent</item>
    </style>

    <style name="ActionBarThemeGray" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="colorPrimary">@color/grayPrimary</item>
        <item name="colorPrimaryDark">@color/grayPrimaryDark</item>
        <item name="colorAccent">@color/grayAccent</item>
    </style>

    <style name="NoActionBarThemeGray" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="colorPrimary">@color/grayPrimary</item>
        <item name="colorPrimaryDark">@color/grayPrimaryDark</item>
        <item name="colorAccent">@color/grayAccent</item>
    </style>


    <style name="ActionBarThemeGreen" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="colorPrimary">@color/greenPrimary</item>
        <item name="colorPrimaryDark">@color/greenPrimaryDark</item>
        <item name="colorAccent">@color/greenAccent</item>
    </style>

    <style name="NoActionBarThemeGreen" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@android:color/white</item>
        <item name="colorPrimary">@color/greenPrimary</item>
        <item name="colorPrimaryDark">@color/greenPrimaryDark</item>
        <item name="colorAccent">@color/greenAccent</item>
    </style>

    ...

</resources>

我希望有人知道更改主题的更好方法! 谢谢!

使用以下应用主题引擎效果很好:

https://github.com/garretyoder/app-theme-engine

如果要支持AndroidO,需要下载库,在ATEclass、applyTaskDescription方法中添加如下代码:

if (icon == null) {
    Drawable mIconDrawable = activity.getApplicationInfo().loadIcon(activity.getPackageManager());
    if (mIconDrawable instanceof BitmapDrawable) {
        icon = ((BitmapDrawable)mIconDrawable).getBitmap();
    } else if (mIconDrawable instanceof AdaptiveIconDrawable) {
        icon = getBitmapFromDrawable(mIconDrawable);
    }
}