Theme与Style笔记1

146 阅读3分钟

1.Theme更为抽象,与特定View无关,屏蔽了主题的变化(View属性引用值的变化)

Theme是更为抽象的属性key-value集合,key是Theme属性,value是指向某个资源引用; style是样式,针对某一个特定类型的View的属性集合,key是View的属性,值是资源引用。 不同的View Style包含的属性集合不一样,不同的主题知之间属性一致,但是相同的属性引用值不一样。

Theme中都是定义的语义上的属性,tyle样式中通过引用Theme中的属性间接引用资源,Theme可以让我们写出更为灵活的Style,有利于App中Style的简洁和维护。

2.怎样使用属性

  1. 首先得明白---》?attr/属性名,表示从当前的Theme中去查找该属性名所对应的属性值。需要注意的是,Context的setTheme和Theme的applyStyle方法都需要在inflate之前调用才会有效果。
  2. ?android:attr/属性名 与 ?attr/属性名有什么区别?前者是Android系统定义的属性,需要通过命名空间xmlns:android="http://schemas.android.com/apk/res/android"进行访问,比如:android:id="@+id/text_gallery",者是其它平台或者第三方库(AppCompat、MDC)中定义的属性,需要通过xmlns:app="http://schemas.android.com/apk/res-auto"进行访问,比如app:layout_constraintBottom_toTopOf
  3. 有些属性不仅在Android系统平台上定义,同时又在一些Library中定义,比如colorPrimary属性。在这种情况下,首选非Android平台的版本,因为Library版本重复定义该属性是为了兼容老版本。

3.常见属性(copy备忘)

  1. Colors
  • ?attr/colorPrimary The primary branding color for the app.
  • ?attr/colorSecondary The secondary branding color for the app, usually a bright complement to the primary branding color.
  • ?attr/colorOn[Primary, Secondary, Surface etc] A color which contrasts against the named color.
  • ?attr/color[Primary, Secondary]Variant An alternate shade of the given color.
  • ?attr/colorSurface A color for surfaces of components, such as cards, sheets, and menus.
  • ?android:attr/colorBackground The background for the screen.
  • ?attr/colorPrimarySurface switches between colorPrimary in the Light themes, colorSurface in the Dark theme.
  • ?attr/colorError A color for displaying errors.

Other handy colors:

  • ?attr/colorControlNormal The color applied to icons/controls in their normal state.
  • ?attr/colorControlActivated The color applied to icons/controls in their activated state (e.g. checked).
  • ?attr/colorControlHighlight The color applied to control highlights (e.g. ripples, list selectors).
  • ?android:attr/textColorPrimary The most prominent text color.
  • ?android:attr/textColorSecondary Secondary text color.
  1. Dimens

  2. Drawables

  3. TextAppearances,13种

  4. Shape

  5. Button Styles

  6. Floats 比如:- ?android:attr/disabledAlpha Default disabled alpha for widgets.

最佳实践

  1. Style和Layout中一般情况都引用Theme属性,除了不需要进行变化属性(比如:在light或dark模式下颜色不需要改变,可以直接引用颜色)。
  2. ColorStateList包含单个状态时,直接引用颜色,而不创建颜色资源,此外 <selector> <item android:alpha="0.50" android:color="#80ffffff" /> </selector>中可以设置alpha改变颜色,所以一般声明颜色都是完全不透明的,使用时可以像这样更改alpha值,但是需要注意的是:当颜色本身有透明度时,再次设置alpha属性,会结合颜色的透明度进行改变,比如:上面本身有50%的透明度再次设置alpha=0.5,最终其透明度将会是0.25。
  3. Theme Overlays主题叠加,Theme有2种,完整的Theme和叠加层Theme。不应在view上设置一个完整的Theme,而是应该在View是使用Theme叠加技术,使用尽可能少的属性,完成View的特定属性的更改。所以说叠加层不会孤立存在,而是配合Activity的Theme,进行属性覆盖调整特定的属性满足需求。
  4. 在程序代码中,应该使用最近应用该主题的元素的Context来获取属性对应的资源,永远不要使用Applciation Context加载Theme资源,比如someView.background = AppCompatResources.getDrawable(someView.context, R.drawable.foo)
  5. Activity中使用的是完整Theme,在manifest中为Application指定的主题仅仅是用作没有显示指定Theme的Activity的备用,Application Context并不保留任何信息。
  6. 不要过度使用android:theme,这会创建一个ContextWrapTheme,包含Theme和Resources对象,这都有一定的开销。