简体   繁体   中英

What is the difference between TextView's style and android:textAppearance attributes?

If I define my TextView as:

 <TextView
        style="@android:style/TextAppearance.DeviceDefault.Large"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

it is basically the same as doing:

 <TextView
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

I know that style is some kind of broader qualifier (ie one can't set all attributes in android:textAppearance ) but then it raises the question: why bother? Is there any advantage of using android:textAppearance over style ?

It seems that style is an attribute for all the views even TextView and textAppearance only apply some 'style components' which are available only for texts. You can apply your own styles in both using styles.xml

https://developer.android.com/guide/topics/resources/style-resource.html https://developer.android.com/reference/android/R.attr.html#textAppearance

textAppearance

Default appearance of text: color, typeface, size, and style.

style

This applies to everything

From Styles and Themes https://developer.android.com/guide/topics/ui/look-and-feel/themes#textappearance

One limitation with styles is that you can apply only one style to a View. In a TextView, however, you can also specify a TextAppearance attribute which functions similarly to a style

TextAppearance allows you to define text-specific styling while leaving a View's style available for other uses. Note, however, that if you define any text attributes directly on the View or in a style, those values would override the TextAppearance values.

You can only have one style attribute per view, but using TextAppearance allows you to in essence define a style using the restricted set of text-relevant attributes. You can use both a style and a TextAppearance in one View.

You can combine Styles with TextAppearance.
For instance, in TextAppearance you set all the text-related logic for a button.
In a Style you may reuse it and add extra attrbutes, such as padding, size etc.

For instance,

<style name="TextAppearanceTitle" parent="TextAppearance.MaterialComponents.Button">
    <item name="android:textColor">#f0f</item>
</style>

<style name="SomeButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
    <item name="android:textAppearance">@style/TextAppearanceTitle</item>
    <item name="android:padding">12dp</item>
    <item name="android:elevation">4dp</item>
</style>

It might be useful to someone, here is the list of supported attributes for TextView's TextAppearance:

<attr name="textColor" />
<attr name="textSize" />
<attr name="textStyle" />
<attr name="typeface" />
<attr name="fontFamily" />
<attr name="textColorHighlight" />
<attr name="textColorHint" />
<attr name="textColorLink" />
<attr name="textAllCaps" format="boolean" />
<attr name="shadowColor" format="color" />
<attr name="shadowDx" format="float" />
<attr name="shadowDy" format="float" />
<attr name="shadowRadius" format="float" />
<attr name="elegantTextHeight" format="boolean" />
<attr name="letterSpacing" format="float" />
<attr name="fontFeatureSettings" format="string" />

All the remaining stuff you set in a Style.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM