关于android:Android-样式系统-常见的主题背景属性

7次阅读

共计 4302 个字符,预计需要花费 11 分钟才能阅读完成。

在前一篇 [Android 款式零碎文章]() 中,咱们介绍了主题背景与款式的区别,以及如何编写灵便的款式与布局代码用于抽离可变动局部。

咱们倡议应用主题背景属性来间接援用资源,您能够在不同的模式下 (比方在 深色主题背景) 实现灵便地切换。如果您发现在布局或款式代码中间接援用了资源或者是硬编码了具体的值,请思考应用主题背景属性来代替之前用法。

<!-- Copyright 2019 Google LLC.  
   SPDX-License-Identifier: Apache-2.0 -->
<ConstraintLayout ...

-  android:foreground="@drawable/some_ripple"
-  android:background="@color/blue" />
+  android:foreground="?attr/selectableItemBackground"
+  android:background="?attr/colorPrimarySurface" />

然而咱们还能够应用哪些主题背景属性的性能呢?这篇文章列举了您应该晓得的对于主题背景属性的通用性能,它们广泛应用在 Material、AppCompact,或者是平台 (Platform) 中。本文并未残缺列举所有属性,只列举了我所应用的,建议您浏览上面的属性文件的定义链接来获取更多信息。

色彩

这些色彩大部分来自于 Material 色彩零碎 (Material color system),它们给每个色彩取了语义化的名称能够让您在利用中应用它们 (体现为主题背景属性)。

  • ?attr/colorPrimary 利用的次要色彩;
  • ?attr/colorSecondary 利用的主要色彩,通常作为次要色彩补充;
  • ?attr/colorOn[Primary, Secondary, Surface etc] 对应色彩的相同色;
  • ?attr/color[Primary, Secondary]Variant 给定色彩的另一种暗影;
  • ?attr/colorSurface 部件的外表色彩,如: 卡片、表格、菜单;
  • ?android:attr/colorBackground 屏幕的背景色彩;
  • ?attr/colorPrimarySurface 在浅色主题中的 colorPrimary 与深色主题背景中的 colorSurface 中做切换;
  • ?attr/colorError 显示谬误时的色彩。

其余罕用的色彩:

  • ?attr/colorControlNormal 失常状态下设置给 icon/controls 的色彩;
  • ?attr/colorControlActivated 激活模式下设置给 icons/controls 的色彩 (如: 单选框被勾选);
  • ?attr/colorControlHighlight 设置给高亮管制界面的色彩 (如: ripples,列表选择器);
  • ?android:attr/textColorPrimary 设置给文本的次要色彩;
  • ?android:attr/textColorSecondary 设置给文本的主要色彩。

大小

  • ?attr/listPreferredItemHeight 列表项的规范高度 (最小值);
  • ?attr/actionBarSize 工具栏的高度。

Drawables

  • ?attr/selectableItemBackground 可交互条目在 ripple 或者是高亮时的背景色彩 (针对外观);
  • ?attr/selectableItemBackgroundBorderless 无边界的 ripple;
  • ?attr/dividerVertical 用于垂直宰割可视化元素的 drawable;
  • ?attr/dividerHorizontal 用于程度宰割可视化元素的 drawable。

TextAppearance

Material 定义了缩放类型,它是在整个利用中应用的一组由文本款式组成的离散汇合,汇合中的每个值都是一个主题背景属性,能够被设置为 textApperance。请点击 Material type scale generator 取得更多对于生成不同字体缩放的帮忙。

  • ?attr/textAppearanceHeadline1 默认为 96sp light 文本;
  • ?attr/textAppearanceHeadline2 默认为 60sp light 文本;
  • ?attr/textAppearanceHeadline3 默认为 48sp regular 文本;
  • ?attr/textAppearanceHeadline4 默认为 34sp regular 文本;
  • ?attr/textAppearanceHeadline5 默认为 24sp regular 文本;
  • ?attr/textAppearanceHeadline6 默认为 20sp medium 文本;
  • ?attr/textAppearanceSubtitle1 默认为 16sp regular 文本;
  • ?attr/textAppearanceSubtitle2 默认为 14sp medium 文本;
  • ?attr/textAppearanceBody1 默认为 16sp regular 文本;
  • ?attr/textAppearanceBody2 默认为 14sp regular 文本;
  • ?attr/textAppearanceCaption 默认为 12sp regular 文本;
  • ?attr/textAppearanceButton 默认为 14sp 全大写 medium 文本;
  • ?attr/textAppearanceOverline 默认为 10sp 全大写 regular 文本。

形态

Material 采纳了形态零碎 (Shape system),它是由主题背景属性 实现 了 small、medium、large 等不同的部件。请留神,如果您想给自定义的部件设置形态外观,您应该应用 MaterialShapeDrawable 作为它的背景,因为它可能了解并能实现具体形态。

  • ?attr/shapeAppearanceSmallComponent 默认圆角为 4dp,用于 Buttons、Chips、TextFields 等;
  • ?attr/shapeAppearanceMediumComponent 默认圆角为 4dp,用于 Cards、Dialogs、Date Pickers 等;
  • ?attr/shapeAppearanceLargeComponent 默认圆角为 0dp (其实是方形),用于 Bottom Sheets 等。

按钮格调

Material 提供了三种不同类型的按钮: Contained、Text 以及 Outlined。MDC 提供了主题背景属性,您能够应用它们给 MaterialButton 设置款式:

  • ?attr/materialButtonStyle defaults 默认是 Contained 类型 (或者间接省略款式);
  • ?attr/borderlessButtonStyle 设置为 Text 款式的按钮;
  • ?attr/materialButtonOutlinedStyle 设置为 Outlined 款式的按钮。

Floats

  • ?android:attr/disabledAlpha 默认敞开 Widget 的 alpha;
  • ?android:attr/primaryContentAlpha 设置给 foreground 元素的 alpha 值;
  • ?android:attr/secondaryContentAlpha 设置给 secondary 元素的 alpha 值。

利用命名空间 vs Android 命名空间

您可能留神到有些属性的援用是通过 ?android:attr/foo 而有些只是通过 ?attr/bar。这是因为一些属性是由 Android 平台定义的,所以您须要应用 android 命名空间来援用由它们本人定义的属性 (相似于布局中应用 View 属性 android:id)。编译到您的利用但不是来自于动态库的属性 (AppCompact 或者 MDC),应用它们时不须要命名空间 (相似于布局中应用 app:baz)。平台跟库有时候定义了雷同的属性,如 colorPrimary。这时候零碎优先应用非平台版本的属性,它们能够被所有级别的 API 应用。为了向后兼容,它们会被残缺的复制到库中。我在下面列举的都是非平台版本的案例。

优先应用非平台版本的属性,它们能够被所有级别的 API 应用

更多资源

为了获取能够应用的全副主题背景属性,请查阅以下信息:

  • Android platform
  • AppCompat

Material 设计的部件:

  • Color
  • Shape
  • Type

本人入手

当您想应用主题背景性能形象某个货色的时候,发现没有现成的主题背景可用时,您能够自定义一个。您能够参考 Google I/O 利用,它实现了在两个界面中显示主题演讲的列表:

这两个界面大部分看起来比拟类似,除了右边界面有个显示工夫的性能而左边是没有的。

将 item 的对齐局部形象成一个主题背景属性,给不同界面应用的同一个布局中应用主题背景来辨别它们的差别:

1. 在 attrs.xml 中定义主题背景属性:

<!-- Copyright 2019 Google LLC.  
   SPDX-License-Identifier: Apache-2.0 -->
<attr name="sessionListKeyline" format="dimension" />

2. 在不同的主题背景中应用 不同 的值:

<!-- 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>

3. 给两个界面应用的布局文件中应用 主题背景属性:

<!-- Copyright 2019 Google LLC.  
   SPDX-License-Identifier: Apache-2.0 -->
<Guideline …
  app:layout_constraintGuide_begin="?attr/sessionListKeyline" />

放弃摸索

理解了可能应用的主题背景属性性能后,您能够在编写布局、款式、drawables 时应用它们。

应用主题背景属性性能更容易实现主题性能 (如 深色主题背景),而且让您编写出更灵便,更易于保护的代码。更多对于此局部的内容,敬请关注本系列的下一篇的文章。

正文完
 0