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

标签 android android-theme android-styles

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

到现在为止,每个 Activity onApplyThemeResource-Method 都被覆盖了。

这是非常昂贵的,但不能以一种方式完成,因为每项 Activity 都有其特殊性,例如这个没有 ActionBar。

还有一个问题。这些样式不适用于 AlertDialogs,您可以查看我的应用程序!

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

@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

如果你想支持 Android O,你必须下载库并将以下代码添加到 ATE 类,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);
    }
}

关于android - 如何将可以在运行时更改的主题应用到 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38185636/

相关文章:

android - 尝试将我的 LogCat 输出到文件

Android nestedScrollView - 如何使其水平?

android - 如何将 API 10 的主题对话框设置为 API 17?

Android多色.xml选择

Android Studio : How to quickly move styling from layout. xml 到 style.xml?

android - 在 Java 中使用 Android 的垂直 LinearGradient

android - 一个 Activity 两个不同的 Layouts 有自己的主题?

java - 从进度对话框中删除背景

android - 不同 api 的不同 styles.xml

java - 使用 if() 以编程方式向relativelayout添加不同的按钮