2016-04-07 18 views
0

styles.xmlstyles.xml(v21)の2つのスタイルが定義されています。どちらも、特定のデフォルトアンドロイドテキストスタイルにリンクするtextAppearance属性を含んでいます。Android:styles.xmlで定義された特定のスタイルからtextColor属性を取得

のstyles.xml

<style name="NotificationTitle"> 
    <item name="android:textAppearance"> 
     @android:style/TextAppearance.StatusBar.EventContent.Title 
    </item> 
</style> 

のstyles.xml(V21)

<style name="NotificationTitle"> 
    <item name="android:textAppearance"> 
     @android:style/TextAppearance.Material.Notification.Title 
    </item> 
</style> 

これらのデフォルトのスタイルが含まれているいくつかのテキスト属性:textColortextSize、など:

... 
<style name="TextAppearance.Material.Notification.Title"> 
    <item name="textColor">@color/primary_text_default_material_light</item> 
    <item name="textSize">@dimen/notification_title_text_size</item> 
</style> 
... 

私はこれをtextColor私のカスタムスタイルの名前(この例ではNotificationTitle)の名前でプログラムで取得する必要があります。 obtainStyledAttributesを使用して取得しようとしましたが、返されません。私は何をすべきか?

<style name="NotificationTitle" parent="TextAppearance.AppCompat.Notification.Title"/> 
<style name="NotificationText" parent="TextAppearance.AppCompat.Notification.Line2"/> 

、すべてが他の部分とうまく:私は少し遅れて答えを持つが、私は変更のビットでコードをしようとした場合

int resultColor; 
int[] attrs = {android.R.attr.textColor}; 
TypedArray ta = context.obtainStyledAttributes(R.style.NotificationText, attrs); 

if (ta != null) { 
    resultColor = ta.getColor(0, Color.TRANSPARENT); 
    ta.recycle(); 
} 
+0

正しいコンテキストを渡していることを確認してください。 –

+0

文脈が適切である、私はちょうど - 私が直接渡す場合チェックした。 'android.R.style.TextAppearance_Material_Notification_Title'を 'obtainStyledAttributes'に設定すると、正しい色が返されます。しかし、私がカスタムスタイル 'R.style.NotificationText'を渡すと、何も返されません。 –

答えて

1

申し訳ありませんが、私はこれにスタイルを変更しましたあなたのコードの(あなたがAppCompatを使用した今スタイルのv21は必要ありませんでした)

私はこれが役に立ちそうです。

+1

あなたの答えをありがとう。私はちょうどあなたが提案した変更で古い例を試してみました - それは動作します!何らかの理由で、私は 'TextAppearance.StatusBar.EventContent.Title'と' TextAppearance.Material.Notification.Title'から自分のカスタムスタイルを継承しようとしませんでした。最後に、質問は終了します。 –

+0

うれしい –

関連する問題