2012-10-18 64 views
1

我正在我的android应用中使用主题来控制哪些样式基于清单中当前选定的主题应用于每个元素。Android - 当样式是自定义的并且由主题控制时,以编程方式更改TextView的样式

在任何一个主题中(其中可能会有很多),我想在运行时切换一些样式。例如,我有一个定义文本通常应该如何显示的样式,另一个样式用于定义当代码输入错误时同一段文本的外观。

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

我产生了一个示例应用程序来说明我的问题(注意,以下ommit的片段些零碎不相关)

Attrs.xml:(我的自定义资源的引用,所以我的布局唐直接“T参考样式)

<resources> 
    <attr name="theme_style_one" format="reference" /> 
    <attr name="theme_style_two" format="reference" /> 
</resources> 

的themes.xml:(选择合适的样式应用基于主题)

<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> 

Styles.xml(的风格本身)

<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> 

Colors.xml(只是一些颜色)

<resources> 
     <color name="color_blue">#0000FF</color> 
     <color name="color_green">#00FF00</color> 
     <color name="color_red">#FF0000</color> 
</resources> 

activity_main布局:

<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" /> 

activity_main的onCreate:

@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); 
} 

我想实现的是改变的TextView的风格为 “theme_style_two” 编程。 “SetTextAppearance”命令不起作用。我无法直接在此命令中引用@ style/blue,因为如果我在清单中更改了主题,则会应用不正确的样式。

任何帮助将不胜感激!

回答

0
/** 
    * 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; 
    } 
相关问题