TextView' s风格和android:textAppearance属性有什么区别?

时间:2016-07-22 02:07:17

标签: android textview android-styles

如果我将TextView定义为:

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

它基本上与做:

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

我知道style是某种更广泛的限定符(即,不能在android:textAppearance中设置所有属性)但是它提出了一个问题:为什么要这么麻烦?使用android:textAppearance优于style

有什么好处

4 个答案:

答案 0 :(得分:2)

似乎样式是所有视图的属性,即使TextView和textAppearance仅应用一些仅适用于文本的“样式组件”。您可以使用styles.xml

在两者中应用自己的样式

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

<强> textAppearance

文本的默认外观:颜色,字体,大小和样式。

<强>式

这适用于一切

答案 1 :(得分:2)

每个视图只能有一个样式属性,但使用TextAppearance可以实际上使用一组受限制的文本相关属性来定义样式。您可以在一个视图中同时使用样式和TextAppearance。

答案 2 :(得分:2)

来自样式和主题https://developer.android.com/guide/topics/ui/look-and-feel/themes#textappearance

  

样式的一个限制是只能将一种样式应用于视图。但是,在TextView中,您还可以指定TextAppearance属性,其功能类似于样式

     

TextAppearance允许您定义特定于文本的样式,同时将View的样式留作其他用途。但是请注意,如果直接在视图上或以样式定义任何文本属性,则这些值将覆盖TextAppearance值。

答案 3 :(得分:2)

您可以将样式与 TextAppearance 结合起来。
例如,在 TextAppearance 中,您可以为按钮设置所有与文本相关的逻辑。
在样式中,您可以重用它并添加额外的属性,例如填充、大小等。

例如

<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>

它可能对某人有用,这里是 TextView 的 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" />

您在样式中设置的所有剩余内容。