2017-01-10 70 views
0

我想圍繞着樣式和主題。目前,我有我的應用程序一個主題:如何使用特定主題的特定樣式?

<style name="WhiteTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    ... 
</style> 

我也有不同的看法很多款式,像這樣:

<style name="BodyText" parent="TextAppearance.AppCompat.Body1"> 
    <item name="android:textSize">14sp</item> 
    <item name="android:textColor">@color/default_text_color</item> 
</style> 

...我用這樣的:

<TextView 
    ... 
    android:textAppearance="@style/BodyText"/> 

現在,如果我要創建一個新主題,例如DarkTheme,我如何確保將所有引用BodyText作爲其TextAppearance的TextView指向新樣式?

回答

1

創建你想擁有整個主題不同的資源生成的attr。

<attr name="someTextColor" format="color"/> 
在你的主題

現在,定義ATTRS

<style name="WhiteTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="someTextColor">@android:color/black</item> 
</style> 

<style name="DarkTheme" parent="Theme.AppCompat"> 
    <item name="someTextColor">@android:color/white</item> 
</style> 

現在你可以使用它們。

<style name="BodyText" parent="TextAppearance.AppCompat.Body1"> 
    <item name="android:textSize">14sp</item> 
    <item name="android:textColor">?attr/someTextColor</item> 
</style> 

您也可以從代碼

得到ATTR
/** 
* Returns color for attr from the {@link Theme} 
* 
* @param theme {@link Theme} to get int from 
* @param attr Attribute of the int 
* @return dimension for attr from the {@link Theme} 
*/ 
@ColorInt 
public static int getColor(@NonNull final Theme theme, @AttrRes final int attr) { 
    final TypedArray array = theme.obtainStyledAttributes(new int[]{attr}); 
    try { 
     return array.getColor(0, Color.TRANSPARENT); 
    } finally { 
     array.recycle(); 
    } 
} 

或者作爲ColorStateList

/** 
* Returns {@link ColorStateList} for attr from the {@link Theme} 
* 
* @param theme {@link Theme} to get int from 
* @param attr Attribute of the int 
* @return dimension for attr from the {@link Theme} 
*/ 
@Nullable 
public static ColorStateList getColorStateList(@NonNull final Theme theme, 
     @AttrRes final int attr) { 
    final TypedArray array = theme.obtainStyledAttributes(new int[]{attr}); 
    try { 
     return array.getColorStateList(0); 
    } finally { 
     array.recycle(); 
    } 
} 

然後

final int someTextColor = getColor(getTheme(), R.attr.someTextColor); 
// or 
final ColorStateList someTextColor = getColorStateList(getTheme(), R.attr.someTextColor); 
+0

非常感謝!這正是我正在尋找的。 – manabreak

0

的主題爲TextView的

<style name="Theme1" parent="Theme.AppCompat.Light.DarkActionBar" > 
     <item name="android:textColor">@color/colorAccent</item> 
     <item name="android:shadowDy">1</item> 
     <item name="android:shadowRadius">0.7</item> 
     <item name="android:textAppearance">@style/MyRedTextAppearance</item> 
    </style> 

    <style name="MyRedTextAppearance" > 
     <item name="android:textColor">@color/colorAccent</item> 
     <item name="android:shadowDy">1</item> 
     <item name="android:shadowRadius">0.7</item> 
    </style> 


    <style name="Theme2" parent="Theme.AppCompat.Light.Dialog" > 
     <item name="android:textColor">@color/colorPrimaryDark</item> 
     <item name="android:shadowDy">1</item> 
     <item name="android:shadowRadius">0.7</item> 
     <item name="android:textAppearance">@style/MyBlueTextAppearance</item> 
    </style> 

    <style name="MyBlueTextAppearance" > 
     <item name="android:textColor">@color/colorPrimary</item> 
     <item name="android:shadowDy">1</item> 
     <item name="android:shadowRadius">0.7</item> 
    </style> 

Textviews將使用

<TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     style="@style/Theme1" 
     android:text="Dummy"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Dummy" 
     style="@style/Theme2"/> 
+0

你沒有回答這個問題。我如何爲'WhiteTheme'指定'BodyText'風格以及與'DarkTheme'一起使用的不同風格? – manabreak

+0

感謝您的編輯。但是,它仍然沒有回答如何使用不同的文本視圖樣式。 AFAIK,這改變了所有TextView的默認樣式?如果我想要隨主題一起更改一打不同的TextView樣式,該怎麼辦? – manabreak

+0

你可以有tetxview的樣式 –