Android开发

【译】Android Styling 2: 常用主题属性

本文主要是介绍【译】Android Styling 2: 常用主题属性,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

原文:Android Styling: Common Theme Attributes
作者:Nick Butcher
译者:Fly_with24


题图来自 Virginia Poltrack


B站官方视频


在 Android styling 系列文章的第一篇,我们研究了主题和样式之间的区别以及主题如何使开发者写出更灵活的样式和布局

具体来说,我们建议您使用主题属性来提供资源的间接访问点,以便您可以改变它们(例如,深色主题)。 也就是说,如果发现自己在布局或样式中编写了直接的资源引用(或更糟糕的是,一个硬编码值😱),请考虑是否应该使用主题属性

但是可以使用哪些主题属性? 本文重点介绍了您应该了解的常见知识; 来自 MaterialAppCompatplatform 的内容。 这不是一个完整的列表(为此,我建议您浏览定义在下面链接的 attrs 文件),但是这些都是我一直使用的属性(使用主题属性实现)

Colors

这里的很多颜色来自于 Material color system,该系统定义了可在整个应用程序中使用的颜色名

  • ?attr/colorPrimary app 主色
  • ?attr/colorSecondary app 次级颜色,通常作为主色的补充
  • ?attr/colorOn[Primary, Secondary, Surface etc] 与命名颜色形成对比的颜色
  • ?attr/color[Primary, Secondary]Variant 给定颜色的阴影
  • ?attr/colorSurface 组件界面(卡片,表格,菜单等)的颜色
  • ?android:attr/colorBackground 背景
  • ?attr/colorPrimarySurface 在浅色主题的 colorPrimary 和深色主题的 colorSurface 间切换
  • ?attr/colorError 错误消息的颜色

其他常用的颜色

  • ?attr/colorControlNormal 正常状态下图标/控件的颜色
  • ?attr/colorControlActivated 激活状态下图标/控件的颜色(例如 checked)
  • ?attr/colorControlHighlight 高亮颜色(例如 ripples, list selectors)
  • ?android:attr/textColorPrimary text 突出颜色
  • ?android:attr/textColorSecondary text 次要颜色

Dimens

  • ?attr/listPreferredItemHeight list item 的标准(最小)高度
  • ?attr/actionBarSize toolbar 的高度

Drawables

  • ?attr/selectableItemBackground 当前交互项的水波纹/高亮(也为前景提供了便利)
  • ?attr/selectableItemBackgroundBorderless 无界的水波纹
  • ?attr/dividerVertical 一个可绘制对象,可用作元素之间的垂直分隔线
  • ?attr/dividerHorizontal 一个可绘制对象,可用作元素之间的水平分隔线

TextAppearances

Material 定义 了一种类型比例——您应该在整个应用中使用的离散文本样式集,它们作为一个主题属性(textAppearance) 被提供。使用 Material type scale generator 帮助生成不同字体的比例

  • ?attr/textAppearanceHeadline1 默认的浅色 96sp 文本
  • ?attr/textAppearanceHeadline2 默认的浅色 60sp 文本
  • ?attr/textAppearanceHeadline3 默认的普通 48sp 文本
  • ?attr/textAppearanceHeadline4 默认的普通 34sp 文本
  • ?attr/textAppearanceHeadline5 默认的普通 24sp 文本
  • ?attr/textAppearanceHeadline6 默认的中等 20sp 文本
  • ?attr/textAppearanceSubtitle1 默认的普通 16sp 文本
  • ?attr/textAppearanceSubtitle2 默认的中等 14sp 文本
  • ?attr/textAppearanceBody1 默认的普通 16sp 文本
  • ?attr/textAppearanceBody2 默认的普通 14sp 文本
  • ?attr/textAppearanceCaption 默认普通 12sp 文本
  • ?attr/textAppearanceButton 默认的中等全大写 14sp 文本
  • ?attr/textAppearanceOverline 默认的中等全大写 10sp 文本

Shape

Material 采用了 shape system,该系统为小型,中型和大型组件 提供 了主题属性。请注意,如果要在自定义组件上设置 shape,则可能要使用 MaterialShapeDrawable 作为其背景,它可以理解并实现 shape

  • ?attr/shapeAppearanceSmallComponent 在 Button ,Chip,Text 的属性中使用,默认 4dp 的圆角
  • ?attr/shapeAppearanceMediumComponent 在 Card,Dialog,Date Picker 中使用,默认 4dp 的圆角
  • ?attr/shapeAppearanceLargeComponent 在 Bottom Sheet 中使用,默认 0dp 圆角

Button Styles

这看起来似乎很具体,但是 Material 定义了三种类型的 button:Contained, Text 以及 Outlined。MDC 提供了主题属性,可用于设置 MaterialButtonstyle

  • ?attr/materialButtonStyle 默认样式,可省略
  • ?attr/borderlessButtonStyle 文本样式的 button
  • ?attr/materialButtonOutlinedStyle outline 样式的 button

Floats

  • ?android:attr/disabledAlpha 为控件禁用透明度
  • ?android:attr/primaryContentAlpha 前景元素的透明度
  • ?android:attr/secondaryContentAlpha 次级元素的透明度

App vs Android namespace

你可能注意到,有些属性由 ?android:attr/foo 引用,而其他的则为 ?attr/bar 。这是因为它们中的部分是由 Android Platform 定义的,因此您需要 android前缀通过命名空间引用它们(就像 layout 中 view 的属性:android:id)。那些不是来自静态库(即 AppCompat 或 MDC ),它们已编译到您的应用程序中,因此不需要名称空间(类似于您在布局中使用 app:baz 的方式)。一些元素在 platform 和 library 均有定义(例如 colorPrimary)。在这种情况下,最好使用非平台版本,这样可以在所有 API 级别上使用。例如它们是在 library 中重复定义恰好目的是为了向后兼容。在这些情况下,我已在上面列出了非平台版本

首选可以在所有API级别上使用的非平台属性

More Resources

有关可用的主题属性的完整列表,可以直接访问以下链接

  • Android platform
  • AppCompat

Material Design Components :

  • Color
  • Shape
  • Type

Do It Yourself

有时,没有主题属性可以抽象出您希望随主题变化的内容(同样的 attribute 在不同的主题下不同),不必担心,你可以自定义!这是 Google I / O 应用程序中的一个示例,该示例在两个屏幕中显示了会议列表

Two screens listing conference sessions

它们在很大程度上相似,但左屏幕必须为时间标题留出空间,而右屏幕则不能。 我们通过抽象在主题属性后面对齐 item 的位置来实现此目的,以便我们可以根据主题来改变它们并在两个不同的屏幕上使用相同的布局:

  1. 在 attrs.xml 中定义主题属性
<!-- Copyright 2019 Google LLC.	
   SPDX-License-Identifier: Apache-2.0 -->
<attr name="sessionListKeyline" format="dimension" />
复制代码
  1. 为不同的主题提供 different values
<!-- Copyright 2019 Google LLC.	
   SPDX-License-Identifier: Apache-2.0 -->
<style name="Theme.IOSched.Schedule">
  …
  <item name="sessionListKeyline">72dp</item>
</style>

<style name="Theme.IOSched.Speaker">
  …
  <item name="sessionListKeyline">16dp</item>
</style>
复制代码
  1. 在同一个 layout 中 使用 主题属性,并配置在不同的界面(每个界面使用上面的两个主题之一)
<!-- Copyright 2019 Google LLC.	
   SPDX-License-Identifier: Apache-2.0 -->
<Guideline …
  app:layout_constraintGuide_begin="?attr/sessionListKeyline" />
复制代码

Question (mark) everything

了解可用的主题属性后,您便可以在编写布局,样式或可绘制对象时使用它们。 使用主题属性使支持主题(如深色主题)和编写更灵活,维护代码变得更加容易。 要对此进行深入研究,请看本系列的下一篇文章

感谢 Florina Muntenescu 和 Chris Banes

译文完

关于我

我是 Fly_with24

  • 掘金

  • 简书

  • Github

这篇关于【译】Android Styling 2: 常用主题属性的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!