Android Material Design Theme 分析

对于系统原生的Theme,有篇文章写的非常好。
Android开发之Theme、Style探索及源码浅析_工匠若水-CSDN博客

官网文档
应用资源概览 | Android 开发者 | Android Developers

讲解 ?attr:/
@android, ?attr/ 和 ?android 的区别 - 简书 (jianshu.com)

资源引用

引用资源(reference a resource)

@[<package_name>:]<resource_type>/<resource_name>

引用样式属性(referencing style attributes)

?[<package_name>:][<resource_type>/]<resource_name>

其中 package_nameresource_type 可以省略,例如 ?attr/colorOnSurface 可以简写成 ?colorOnSurface?android:attr/colorBackground 可以简写成 ?android:colorBackground

<declare-styleable /> 和 <style/> 有什么关系?

declare-styleable 中声明的 attr 可以 在 style 中使用。declare-styleable 类似一个接口,声明了属性的名称和类型。style 是 declare-styleable 的实现,声明了具体的值或者引用。

<attr /> 声明在<resources />和声明在<declare-styleable />中有何异同?

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <attr name="attrib1" format="string" />
   <declare-styleable name="blahblah">
      <attr name="attrib2" format="string" />
   </declare-styleable>
</resources>

相同点:
是两者声明的 attr 都可以在布局 xml 中被使用。

不同点:

  1. declare-styleable 中的 attr 可以声明在 <style /> 中,可以作为theme或style的属性。
  2. 在自定义 View 构造函数中,获取两者的方式不同。
public SomeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // declare-styleable 的 attr
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.SomeView);
        mColor = typedArray.getColor(R.styleable.ColorSelectorView_edit_color, TRANSPARENT);
        typedArray.recycle();
        // resources 中的 attr
        String attrib1 = attrs.getAttributeValue(null, "attrib1");
}

Material Design 主题

  1. MD 提供的默认主题是什么?和系统提供的主题有什么区别和联系?
    系统预制的主题的位置在 /Android/sdk/platforms/android-30/data/res/values/,里面声明了所有的预制属性。

然后我们以Theme.MaterialComponents.DayNight.NoActionBar为例,先分析一下 MD 的主题,这个主题在库中 /material-1.4.0/res/values/values.xml 中,跳转过去后看到"Theme.MaterialComponents.DayNight.NoActionBar",这个主题有个父主题parent="Theme.MaterialComponents.Light.NoActionBar"。我们搜索"Theme.MaterialComponents.Light.NoActionBar",会发现这个主题没有parent属性了,那这个主题的其他属性是从何而来的呢?

这里讲一下主题的继承方式,主题有两种继承方式,一种是通过声明parent属性,另一种是通过一个点.来声明。

通过parent声明的主题,它的 name 可以和父主题不同。
而通过.继承的主题,名字一定是parent.child_theme的形式。

所以对于"Theme.MaterialComponents.Light.NoActionBar"主题,他的父主题是"Theme.MaterialComponents.Light",它通过 parent 属性往上又有两层,"Base.Theme.MaterialComponents.Light"->"Base.V14.Theme.MaterialComponents.Light"

找到这里,我们发现"Base.V14.Theme.MaterialComponents.Light"下有了大量的我们熟知属性。我们看看其中包含的属性,
style 类型的属性名字是@style/Widget.MaterialComponents.*,
theme 类型的名字是@style/ThemeOverlay.MaterialComponents.*
我们还能看到一个属性,<item name="textAppearanceLargePopupMenu">?attr/textAppearanceSubtitle1</item>

重写 Theme 和 Style

Matrial Design 的主题都以 "Base.V14.Theme.MaterialComponents.Light.Bridge" 主题为基类。这个主题是个起桥接作用的主题,它桥接了 AppCompat 主题和自定义的 Matrial Desig 主题,它是 MD 主题的基类,本质上 Material Design 的主题就是 AppCompat 主题。AppCompat 主题又继承了 Android 系统预制的主题 "android:Theme.Holo.Light",而所有的主题的共同父类是一个叫做 ”Theme“ 的主题,它的属性有 400 多行,包含了所有系统预制的属性和 style,所以用 ”Theme“ 进行查阅是最好不过的。

重写的方式很简单,只需要在 <style> 中添加 <item>,申明一个和父元素中同名的属性即可完成重写。

  1. 重写预制属性
    预制属性是android开头的属性,它们定义在 /sdk/platforms/android-30/data/res/values/attrs.xml下,每个属性都有一个 format,表示类型。
  • 假如我们要改变 Activity 的默认背景色,我们可以这么写:
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:colorBackground">@color/color_window_background</item>
</style>
  • 假如我们要改变 TextView 控件的字体颜色,我们可以这么写:
<style name="Widget.MaterialComponents.CheckedTextView" parent="Base.Widget.MaterialComponents.CheckedTextView">
    <item name="android:textColor">@color/red</item>
</style>

很多控件的变量都有预制的属性与之对应,比如字体颜色,大小等,这些属性与xml布局中的属性是等同的。

  1. 重写控件自定义属性
    自定义属性和预制属性的区别在于没有前缀 android:,重写方式等同于重写预制属性。
  2. 重写控件style
    先找到控件默认的 style,对于style 的重写最好基于已有的style。
    例如对于 MD 主题中的 popupMenuStyle 进行重写
// 先找到 popupMenuStyle 定义的位置,在父主题中这里
<style name="Base.V14.Theme.MaterialComponents.Light" parent="Base.V14.Theme.MaterialComponents.Light.Bridge">
    <item name="popupMenuStyle">@style/Widget.MaterialComponents.PopupMenu</item>
    ...
<style/>
// 先创建一个新的样式"Widget.App.PopupMenu"继承父样式 "Widget.MaterialComponents.PopupMenu"
// 然后替换成新样式
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="popupMenuStyle">@style/Widget.App.PopupMenu</item>
    <style name="Widget.App.PopupMenu" parent="Widget.MaterialComponents.PopupMenu">
      <item name="android:popupBackground">@drawable/shape_popupmenu_background</item>
    </style>
</style>
  1. 重写theme
    theme的重写等同于 style 重写
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容