从另一个应用程序复制主题

PoO*_*oOk 5 android android-context android-theme

我有一个包含自定义主题的应用程序,我希望与其他应用程序共享.这个想法是这个应用程序为其他应用程序提供主题.

该主题在styles.xml中定义如下:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="Text">
    <item name="android:textColor">#00FF00</item>
    <item name="android:textColorHighlight">#FFFF9200</item>
    <item name="android:textColorHint">#FFCCFF</item>
    <item name="android:textColorLink">#5C5CFF</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">normal</item>
</style>

<style name="Button">
    <item name="android:background">#FF0000</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
    <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
    <item name="android:textColor">#FFFF00</item>
    <item name="android:textSize">22dip</item>
    <item name="android:gravity">center_vertical|center_horizontal</item>
</style>

<style name="Theme.example" parent="android:Theme">
    <item name="android:background">#FF0000</item>
    <item name="android:buttonStyle">@style/Button</item>
    <item name="android:textAppearance">@style/Text</item>
</style>

</resources>
Run Code Online (Sandbox Code Playgroud)

要从第二个应用程序获取此主题:

Context appThemesContext = this.getApplicationContext().  
                           createPackageContext("com.appThemes", 
                                                 Context.CONTEXT_IGNORE_SECURITY);
appThemesContext.setTheme(0x7f050002); //The resid of the desired theme
this.getTheme().setTo(appThemesContext.getTheme()); //Copy the theme
Run Code Online (Sandbox Code Playgroud)

问题是只复制像"背景"这样的直接属性,而不像"buttonStyle"这样的引用属性,因为"setTo"方法说:

将此主题设置为与其他主题保持相同的内容.如果这两个主题都来自同一个Resources对象,则在此函数返回后它们将是相同的.如果它们来自不同的资源,则只有它们共有的资源才会在此主题中设置.

有谁知道如何从其他应用程序的资源中复制主题?主题不会使用像图像等资源......只有值.

谢谢 ;)