我为 style 和 theme 而狂(3)

作者: zidea | 来源:发表于2019-02-27 18:22 被阅读6次
impossible-shapes-orig.jpg

多重 style

理论上是无法再同一个视图组件应用多个 style,但是就一种列外就是 TextAppearance 作为视图(这里可能不准确)允许定义一个 style 。

TextAppearance 属性

  • textColor
  • textColorHighlight
  • textColorHint
  • textColorLink
  • textSize
  • textStyle
  • fontFamily
  • typeface
  • ...
<TextView
  style="@style/MyStyle"
  android:textAppearance="@style/MyStyle"/>

这里一个 style 定义文字的外观,另一个用于定义 style 文字以外的外观。
我们定义一个 TextAppearance 的 style,通常会继承 TextAppearance.AppCompact 然后在其中修改一个 TextAppearance 的属性。

<style name="MyText" parent="TextAppearance.AppCompat">
        <item name="android:textColor">#F000</item>
    </style>
amusement-park-9.jpg

Theme

相对于局部的 style,theme 是全局的,style 从视图相同样式提取的,主题着放眼全局。我们在 theme 定义一个属性,这个属性将作用于整个我们 Android 应用。可以在 theme 为我们所有 widget 和窗口设置默认值。在可以 Activity 层面上进行主题化。也可以设置系统级别上创建的视图。我们也可以自由更换主题。

Themes VS Styles

  • 其实并没有什么区别
<style name="Style">
        <item name="android:background">#FF0000</item>
    </style>
    <style name="Theme">
        <item name="android:statusBarColor">@color/blue1</item>
    </style>

通过上面 Style 和 Theme 定义来看并没有什么区别,在其中定义一个属性。

  • 不同的作用域

两者的作用域是不同,这个值得注意一下。

  • theme 是窗口级别的要是 manifest 使用
  • style 是窗口元素级别 布局 xml 中使用

相关文章

网友评论

    本文标题:我为 style 和 theme 而狂(3)

    本文链接:https://www.haomeiwen.com/subject/gwlvsqtx.html