TextView的样式和android:textAppearance属性有什么区别?

Mir*_*cek 26 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!" />
Run Code Online (Sandbox Code Playgroud)

它与做的基本相同:

 <TextView
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
Run Code Online (Sandbox Code Playgroud)

我知道这style是一种更宽泛的限定符(即不能设置所有属性android:textAppearance)但是它提出了一个问题:为什么要打扰?使用android:textAppearance结束有什么好处style吗?

Jos*_*ema 6

似乎样式是所有视图的属性,甚至 TextView 和 textAppearance 也只应用了一些仅适用于文本的“样式组件”。您可以使用styles.xml在两者中应用自己的样式

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

文字外观

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

风格

这适用于一切


onm*_*133 6

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

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

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


小智 5

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


Leo*_*der 5

您可以将样式与 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>
Run Code Online (Sandbox Code Playgroud)

这可能对某人有用,这里是 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" />
Run Code Online (Sandbox Code Playgroud)

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