如何使用特定主题的特定样式?

时间:2017-01-10 10:55:01

标签: android android-theme android-styles

我试图围绕着风格和主题。我目前在我的应用中有一个主题:

<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的TextView作为TextAppearance指向新样式?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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"/>