【译】Android Styling 2: 常用主题属性
原文:Android Styling: Common Theme Attributes
作者:Nick Butcher
译者:Fly_with24
题图来自 Virginia Poltrack
B站官方视频
在 AndroidD 4 / f b styling 系e v W 9 5 E l列文章的第一篇,我们研究了主题和样式之间的区别以及主题如何使开发者写出更活络的样式和布局
详细来说,我们建议您运用主题特色来供给资源的直{ N M B接访问点,以便您可以改动它们(例如,深色主题)。 也就是说,假设{ D d k l V *发现自己在布局或样式中编写了直接的资源引用(或更糟糕的是,一个硬编码值),请考虑是否应该运用主题特色

但是可以运用哪些主题特色? 本文要点介绍了您应该了解的常见知识; 来自 Material
,AppCompao K H 6 : |t
或 platform
的内容。 这不是一个无缺的列表(为此,我建议您阅览定义在下面链接的 attrs 文件),但是这些都是我一向运用的特色(运用主题特色完结)
ColoH 4 ?rs
这儿的许多颜色来自于 Material color system,该体系定义了可在整个运U [ Q x用程序中运用的颜色名

-
?attm T & c n * W d gr/colorPrimary
app 主色 -
?attr/colorSecondary
app 次级颜色,通常作为主色的弥补 -
?attr/colorOn[Primary, SD [ 9 c 2 s j (econdary, Surface etc]
与命名颜色构成对比的颜色 -
?attr/color[Prim9 w g @ jary, Secondary]Variant
给定颜色的暗影 -
?attr/colorSurface
组件界面(卡片,表格,菜单等)的颜色 -
?android:G U r Lattr/c X ; { B a 1 T ZcolorBackground
背景 -
?attr/colorPrimarySurface
在淡色主题的colorPrimary
和深色主题的colorSurface
间切换 -
?attr/P X :colorError
过错消息的颜色
其他常用的颜色
-
?attr/colorConS u I - etrolNormal
正常状态下图标/控件的颜色 -
?attr/colorControlActivated
激活状态下图标/控件的颜色(例如 checked) -
?attr/colorControlHighlight
高亮颜色(例如 ripples, list selectors) -
?android:attr/textColorPrimary
text 杰出颜色 -
?android:attr/textColorSecondary
text 非有必要颜色
Dimens
-
?attr/listPreferredItemHeight
list item 的规范U w 0 P(最小)高度 -
?attr/actionBarSize
toolbar 的高度
Drawabl) N 7 w / M # 2 ues
-
?attr/selectable+ | ] 3 qItemBackground
当时交互项的水波纹/高亮(也为前景供给了便当) -
?attr/selectableItemBackgroundBorderless
无界的水波纹 -
?attr/divi1 D = Z ^ 8 GderVertical
一个可制造目标,可用作元素之间的笔直分隔线 -
?attr/dividerHorizontal
一个可制造目标,可用作元素之间的水平分隔线
TextAppearances
Material 定义 了一种类型比例——您应该在整个运H ! ) 4 V ,用中运用的离散文本样式集,它们作为一个主题特色(textAppeara. N ; 6 `nce
) 被供给p ^ g M * A w。运用: C B @ ` Material type scale generator 协助生成不同字体的比例

-
?attr/textAppe; ( j , H P H u aaranceHeadline1
默许的淡色 96sp 文本 -
?attr/textAppearanceHeadline2
默许的淡色 60sp 文本 -
?attr/textAppearanceHeadlo O K @ine3
默许的一般 48sp 文本 -
?attr/textAppearanceHeadline4
默许的一般 34sp 文本 -
?attr/textAppearanH 4 # a H 9 ? E oceHeadline5
默许的一般 24sp 文本 -
?attr/textAppearanceHeadline6
默许的中等 20sp 文本 -
?aC ? O P , : vttr/textAppearanceSubtitle1
默许的一般 16sp 文本 -
?attr/textAppearanceSubtitle2
默许的中等 14sp 文本 -
?attr/textAppeaM T zranceBody1
默许的一般 16sp 文本 -
?attr/textAppearanceBody2
默许[ w + 7 m Y [ 6 *的一般 14sp 文本 -
?attr/textAppearanceCaption
默许一般 12sp 文本 -
?attK 3 | ^r/textAppearanceButton
默许的中等全大写 14sp 文本 -
?attr/textApp* L ; O N E g } }earanceO; e vverline
默许的中等全大写 10sp 文本
Shape
MateQ g @ X yrial 采用了 shape system,该体系为小型,中型和大型组件 供给 了主题特色。请注意,假设要在自定义组件上设置 shape,则或许D @ {要运用 Mab / sterialShapeDrawable
作为其背景,它可以理解并完结 shape

-
?attr/shapeAppearanceSmallComponent
在 Button ,Chip,Text 的特色中运用,默许 4dp 的圆角 -
?attr/shapeAppearanceMediumComponent: 9 M 8 _ } 8
在 Card,Dialog,DatA D U |e Picker 中运用,默许 4dp 的圆角 -
?attr/shapeAppearanceLargeComponent
在 Bottom Sheet 中运用,默许 0dp 圆角
Button Styles

这看起来如同很详细,但是 Material 定义了三种类型的 button:ConQ ? K L 8 K k Q ,tained, Text 以及 Outlined。MDC 供给了主题特色,可用于设置 MaterialButton
的 style
-
?attr/materialButtonStyle
默许样式,可省略 -
?attr/borderlessButtonStyle
文本款y d e m [式的 button -
?attr/materialButtonOutlinP q { 7edStylM 5 @e
outliw U +ne| A T 样式的 button
Floats
-
?android:attrS I I /disabledAlpha
为控件禁用透明度` [ % -
?android:attr/primaryContentAlpha
前景元素的透明度N ! L % -
?androi0 # - `d:attr/secondaryContentAlpha
次级元素的透明度
App vs Android namespace
你或许注意到,有些特色由 ?android:attr/foo
引用,而其他的则为 ?attr/bar
。这是因为它们中的部分是由 Android Platform 定义的,因此您需求 android
前缀通过命名空间引用它们(就像 layout 中 view 的特色:android:id
)。那些不是来自 L ) a _ N f静态库(即 AppCompat 或 MDC ),它们已编译到您的运用程序% 2 z 7 O _ & ]中,因此不需求称号空间(u n K 4 y类似于您在布局Z . ) K s ^ 0 2 d中运用 app:baz
的方法)。一些元素在 platform 和 library 均有定义(例如z m ) g h colorPrimarm 9 ! z $ +y
)。在这种情况下,最好运用非途径版别,这样可以在所有 API 级别上运用。例如它们是在 library 中重复定义刚好意图是为了向后兼容。在这些情况下,} Z # & 9 9我已在上面列出了非途径版别
首选可以在所有API级别上运用的非途径l { % ( ~ * h & V特色
More R* R Hesources
有关可用的主题特色的无缺列表,可以直接访问以下链接
- Android plate } h Q ! L uform
- AppCompat
MatD X E w ~ D 6 !erial Design Components :
- Color
- Shape
- Type
Do It Yourself
有时,没有主题特色可以笼统出您希望随主题变化的内容(相同的 attributeQ J 6 S R ` 在不同的主题下不同),不用担心,你可以自定义!b % T u这是 Google I / O
运用程序中的一个示例,该示例在两个屏幕中显示了会议列表

它们在很大| Z程度上类似,但左屏幕有必要为时间标题留出空间,而右屏幕则不能s m x : u } ;。 我们通过笼统在主题特色后面对齐 item 的方位来完结此意图,以便我们可以依据主题来改动它们并在两个不同的屏幕上运用相同的布局:
- 在 attrs.xml 中定义主题特色
&Z S y ? Qlt;!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Af 4 p #pache-2.0 -->
<attr name="sessionListKe{ ^ t D S # L ?yline" format="dimension" />
- 为不同的主题供给 different values
<!-- Copyright 2019 Google LLC.
SPDX-License-Id1 1 : j i W FentT m J X 8 7ifier: ApC G +ache-2.0 -->
<style name="Theme.IOSP ? r O F /ched.Schedule">
…
<item name=; j 7 q `"sessionListKeyline">72dp</item>
</style>
<style name="Theme.IOSched.Speaker">
…
<item name="sessionListKeyline">16dp<X j b R % 3 Z n c;/item>
</style>
- 在同一个 layout 中 运用 主题特色,并配备在不同的界面(每个界面运用上| ? d h 2 @ y 8面的两个主题之一i g :)
<!-- Copyright 2019 Google LLC.
SPDX-License | 8-Identifier: Apache-2.0 -->
<Guideline …
app:layout_constraintGuide_begin="?attr/sessionListKey0 x 5 6 M nline" />
Question (mark) ev| = O T 3 K & *erything
了解可用的主题特色后,您便可以在编写布& # 2 a ^ 8 n .局,样式或可制造目标时运用它们。 运用主题特色使支撑主题[ g D * c I P(如深色主题)和编写更活络,保护代码变得愈加简单。 要对此进行深入研究,请看本系列的x – ( A m [ z下一篇文章
感谢 Flor[ / nina Muntenj , t 6 # x / uescu 和 Chris Banes
译文完
关于我
我是 Fly_with24
-
掘金
-
简书
-
Github
