Android layout Android-当文本视图的样式由主题自定义和控制时,通过编程更改其样式

Android layout Android-当文本视图的样式由主题自定义和控制时,通过编程更改其样式,android-layout,android-theme,android-styles,Android Layout,Android Theme,Android Styles,我在android应用程序中使用主题来控制根据清单中当前选定的主题应用于每个元素的样式 在任何一个主题(其中可能有许多主题)中,都有许多样式,我希望在运行时在这些样式之间进行切换。例如,我有一个样式定义了文本通常的外观,另一个样式定义了当代码输入错误时同一段文本的外观 我不能直接引用@style,因为这是由主题决定的 我已经制作了一个示例应用程序来说明我的问题(请注意,下面的代码片段中包含了一些不相关的部分) Attrs.xml:(我的自定义资源引用,因此我的布局不会直接引用样式) 我想实现的是

我在android应用程序中使用主题来控制根据清单中当前选定的主题应用于每个元素的样式

在任何一个主题(其中可能有许多主题)中,都有许多样式,我希望在运行时在这些样式之间进行切换。例如,我有一个样式定义了文本通常的外观,另一个样式定义了当代码输入错误时同一段文本的外观

我不能直接引用@style,因为这是由主题决定的

我已经制作了一个示例应用程序来说明我的问题(请注意,下面的代码片段中包含了一些不相关的部分)

Attrs.xml:(我的自定义资源引用,因此我的布局不会直接引用样式)

我想实现的是通过编程将TextView的样式更改为“theme\u style\u two”。“SetTextAppearance”命令无效。我无法在此命令中直接引用@style/blue,因为如果我在清单中更改主题,将应用不正确的样式

任何帮助都将不胜感激

    /**
     * Gets a colour attribute for the specified theme attribute
     * 
     * @param The activity context so we can get the theme
     * @param themeAttr The theme attribute we want to get the style attribute for (e.g. R.attr.text_small)
     * @param styleAttr The attribute of the style that we want to get the value for (e.g. android.R.attr.textColor)
     * @return The resource ID of the colour (default is black if the style attribute can't be found)
     */
    public static int GetColourFromThemeAttribute(Context c, int themeAttr, int styleAttr)
    {
        // Get the resource ID of the style that the attribute references for the current theme
        TypedValue typedValue = new TypedValue();
        c.getTheme().resolveAttribute(themeAttr, typedValue, true);

        // Define an array of attributes we want to get from the style
        int[] attributes = new int[] { styleAttr };

        // Get the style attributes from the style that the theme attribute references
        TypedArray a = c.obtainStyledAttributes(typedValue.data, attributes);

        // Get the colour from the list of style attributes
        int colour = a.getColor(0, Color.BLACK);

        // Release the typed array
        a.recycle();

        return colour;
    }
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ThemeOne" parent="android:Theme">
    <item name="theme_style_one">@style/blue</item>
    <item name="theme_style_two">@style/red</item>
</style>

<style name="ThemeTwo" parent="android:Theme">
    <item name="theme_style_one">@style/green</item>
    <item name="theme_style_two">@style/red</item> 
</style>
</resources>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="blue">
        <item name="android:textColor">@color/color_blue</item>
    </style>

    <style name="red">
        <item name="android:textColor">@color/color_red</item>
    </style>

    <style name="green">
        <item name="android:textColor">@color/color_green</item>
    </style>
</resources>
<resources>
      <color name="color_blue">#0000FF</color>
      <color name="color_green">#00FF00</color>
      <color name="color_red">#FF0000</color>
</resources>
<TextView
    android:id="@+id/txt_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world"
    style="?theme_style_one" />
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = (TextView)findViewById(R.id.txt_text);
    textView.setTextAppearance(this, R.attr.theme_style_two);
}
    /**
     * Gets a colour attribute for the specified theme attribute
     * 
     * @param The activity context so we can get the theme
     * @param themeAttr The theme attribute we want to get the style attribute for (e.g. R.attr.text_small)
     * @param styleAttr The attribute of the style that we want to get the value for (e.g. android.R.attr.textColor)
     * @return The resource ID of the colour (default is black if the style attribute can't be found)
     */
    public static int GetColourFromThemeAttribute(Context c, int themeAttr, int styleAttr)
    {
        // Get the resource ID of the style that the attribute references for the current theme
        TypedValue typedValue = new TypedValue();
        c.getTheme().resolveAttribute(themeAttr, typedValue, true);

        // Define an array of attributes we want to get from the style
        int[] attributes = new int[] { styleAttr };

        // Get the style attributes from the style that the theme attribute references
        TypedArray a = c.obtainStyledAttributes(typedValue.data, attributes);

        // Get the colour from the list of style attributes
        int colour = a.getColor(0, Color.BLACK);

        // Release the typed array
        a.recycle();

        return colour;
    }