2012-01-06 14 views
5

私はカスタム属性を使用してアプリケーションでテーマ切り替えを実装しています。私は、次の属性が定義されている次のように定義されてsetTextAppearanceはカスタム属性を参照するコードを介して

<style name="NI_AppTheme.Dark"> 
    <item name="TextAppearance_Footer">@style/Footer</item> 
</style> 

@style/Footerを::

<style name="Footer" parent="@android:style/TextAppearance.Large"> 
    <item name="android:textColor">#00FF00</item> // Green 
</style> 

を今、私がしようとした場合

<resources> 
    <attr name="TextAppearance_Footer" format="reference"></attr> 
</resources> 

は私が異なっこの属性を定義する2つのテーマを持っていますこのスタイルをTextViewに設定します。

textView.setTextAppearance(this, R.attr.TextAppearance_Footer); 

テキストをGreenに設定しません)。しかし、私がxml経由でテキストの外観を指定した場合、

android:textAppearance="?TextAppearance_Footer" 

これは問題なく動作します。私は何が欠けていますか?動的にテーマを切り替えることができるので、属性を設定する必要があります。

追加情報:私が使用している場合

textView.setTextAppearance(this, R.style.NI_AppTheme.Dark); 

すべての権利を動作するようです。

EDIT:テスト済みワーキングソリューション(感謝の@nininho):

Resources.Theme theme = getTheme(); 
TypedValue styleID = new TypedValue(); 
if (theme.resolveAttribute(R.attr.Channel_Title_Style, styleID, true)) { 
    channelTitle.setTextAppearance(this, styleID.data); 
} 

答えて

11

なぜ使用しない:私はtextAppearanceスタイルでなければならないと思い

textView.setTextAppearance(this, R.style.Footer); 

編集:

TypedArray a = context.obtainStyledAttributes(attrs, 
new int[] { R.attr.TextAppearance_Footer }); 

int id = a.getResourceId(R.attr.TextAppearance_Footer, defValue); 
textView.setTextAppearance(this, id); 

EDIT:

がテストされたコードを修正して

たぶん、あなたはこれを試してみてください3210

Resources.Theme theme = getTheme(); 
TypedValue styleID = new TypedValue(); 
if (theme.resolveAttribute(R.attr.Channel_Title_Style, styleID, true)) { 
    channelTitle.setTextAppearance(this, styleID.data); 
} 
+0

おそらく、彼はテーマによって選択し、R.style.Footerを直接使用しないことを望んでいるためです。 – aromero

+0

だから彼はテーマを使うべきですが、質問から彼はそれを使いたくありません(理由は分かりません) –

+0

そうは確かです。彼がやろうとしているのは、TextAppearanceスタイルをテーマに委譲することです。 TextAppearance_Footerはリンクを行う属性です。たとえば、theme1とtheme2があるとしましょう。 theme1はTextAppearance_Footer => textAppearance1と、theme2はTextAppearance_Footer => textAppearance2と言うでしょう。ウィジェットでは、単にtextAppearance => TextAppearance_Footerと言うだけです。あなたが得るのは、現在のテーマ – aromero

関連する問題