使用Theme的正确方式

本文详细探讨了Android中Theme和Style的区别,包括Style作为视图属性集合的作用和Theme作为资源集合的影响范围。介绍了常用主题属性,如Colors、Dimens、Drawables等,并讲解了ThemeOverlay的使用和内存开销。此外,还分享了一些实践技巧,如深色模式适配、设置透明度以及在代码中获取attr颜色。最后,讨论了如何在不同场景下正确使用Theme和Style,以及面临的问题和解决方案。
摘要由CSDN通过智能技术生成

前面一部分主要是摘录自Flywith24 的 Android Styling系列文章(主要为了整理和理解方便😂),强烈建议大家看大佬的原文

一、Theme 和 Style的区别

Style是什么?

Style是view的属性的集合 ,你可以将style视为Map<view attribute,resource>。这里的key是view的所有属性,都是可以在layout文件中配置的

一个style只能作用于其应用的view,不包含它的子view。使用时用style标签

Style 作用范围
Theme是什么?

Theme是资源的集合,它可以被style、layout或者其他引用。它为android资源提供语义明确的命名,如下:

<style name="Theme.Plaid" parent="">
  <item name="colorPrimary">@color/teal_500</item>
  <item name="colorSecondary">@color/pink_200</item>
  <item name="android:windowBackground">@color/white</item>
</style>

Theme为这些已命名的资源提供正确的值,应用在整个app中。在引用主题属性时,可以使用?attr/语法,其中?代表在当前主题中搜索。

Theme可以在applicationActivityView中设置,可以作为Context的属性被获取,也可以通过ContextThemeWrapper包装现有的Context来在代码中设置theme,然后将其用于inflate布局,具体使用方法看第三节

定义的theme会生成一个树,在此树的任何级别上指定主题都会影响到后代节点。使用时用theme标签

theme 作用范围

总结下:

概念 作用范围
Style view的属性集合 只作用于应用的view,没有继承关系
Theme 资源的集合 会作用于树中所有子节点,有继承关系

二、常用主题属性

上面说到,?attr/表示在当前主题中搜索资源,?attr/后面的内容需要在attr.xml文件中定义。系统已经定义了一些常用的主题属性名,我们可以直接拿来用,看这里:Color Theming

Colors
  • ?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

放在type.xml文件中,定义常用字体大小,在TextView中可以使用如下方式引用

android:textAppearance="?attr/textAppearanceBody1"
  • ?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

放在shape.xml文件中,定义一些常用形状的圆角等属性,具体使用方式可以看这里:Shape Theming。要在自定义组件上设置shape,可能需要使用MaterialShapeDrawable,它可以理解并实现shape

  • ?attr/shapeAppearanceSmallComponent 在 Button ,Chip,Text 的属性中使用,默认 4dp 的圆角

  • ?attr/shapeAppearanceMediumComponent 在 Card,Dialog,Date Picker 中使用,默认 4dp 的圆角

  • ?attr/shapeAppearanceLargeComponent 在 Bottom Sheet 中使用,默认 0dp 圆角

Button Styles
"Button types - text, outlined, contained and toggle."

Material 定义了四种类型的Button:Text、Outlined、Contained、Toggle,可用于设置MaterialButton的 style,具体使用看这里:Buttons

  • ?attr/materialButtonStyle 默认样式,可省略

  • ?attr/borderlessButtonStyle 文本样式的 button

  • ?attr/materialButtonOutlinedStyle outline 样式的 button

  • ?attr/materialButtonToggleGroupStyle Toggle样式的button。Toggle类型按钮使用MaterialButtonToggleGroup,示例如下:

    <com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"
            style="?attr/materialButtonOutlinedStyle"
        />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要激活Material Theme UI,您需要按照以下步骤操作: 1. 打开Visual Studio Code编辑器 2. 点击左侧菜单栏中的“扩展”按钮 3. 在搜索框中输入“Material Theme UI” 4. 点击“安装”按钮,等待安装完成 5. 安装完成后,点击“启用”按钮 6. 现在您已经成功激活了Material Theme UI主题 希望这能帮助您激活Material Theme UI主题。 ### 回答2: Material Theme UI 是一款非常受欢迎的 Sublime Text 主题。该主题与 Google Material Design Theme 风格相似,为代码编辑器带来了可爱的、明亮的和现代的外观。Material Theme UI 提供了丰富的功能,如高亮显示、代码内注释、代码折叠等等。但是,很多用户会遇到激活 Material Theme UI 的问题。本文将为您介绍如何激活 Material Theme UI。 许多用户下载了 Material Theme UI 之后,可能会发现主题不能正确显示。这是因为主题需要激活才能正常工作。要激活 Material Theme UI,需要按照以下步骤进行操作: 1. 打开 Sublime Text 编辑器。 2. 在菜单栏中点击 Preferences(首选项)-> Package Control -> Install Package(安装插件)。 3. 在弹出的搜索框中输入“Material Theme UI”,并单击搜索按钮。 4. 在搜索结果中找到“Material Theme UI”插件并单击进入。 5. 单击“Install”按钮,等待插件的安装完成。 6. 安装完成后,在菜单栏中选择 Preferences -> Settings(首选项 -> 设置)。 7. 在弹出的文件编辑器中找到“Settings-User.sublime-settings”文件,在该文件中复制以下代码: { "theme": "Material-Theme.sublime-theme", "color_scheme": "Packages/Material Theme/schemes/Material-Theme.tmTheme" } 8. 将代码粘贴到“Settings-User.sublime-settings”文件中。 9. 点击保存并关闭该文件。 10. 在菜单栏中选择 Preferences -> Color Scheme(首选项 -> 颜色方案)。 11. 在弹出的菜单中选择“Material-Theme -> Material-Theme”选项。 12. 关闭并重新打开 Sublime Text 编辑器,Material Theme UI 就会生效了。 通过以上步骤,您就可以安装并激活 Material Theme UI 主题了。Material Theme UI 可以增强您的开发体验,提高编辑器的可读性和美观程度。这款主题是 Sublime Text 编辑器使用者必不可少的工具之一。 ### 回答3: Material Theme UI是一款支持多种IDE的插件,它可以在开发者编写代码的时候,为IDE的UI界面提供更加美观、现代化的外观风格。使用Material Theme UI插件后,开发者可以享受到更加舒适、人性化的编程体验。 激活Material Theme UI插件的方法如下: 1.在IDE中选择“Plugins”菜单,然后在搜索栏中输入“Material Theme UI”插件; 2.找到插件后,点击“Install”按钮进行安装; 3.安装完成后,重启IDE; 4.在IDE的“Settings”->“Appearance & Behavior”->“Material Theme UI”页面中选择外观主题和颜色。开发者可以从多个预设主题中选择、或自定义主题颜色; 5.设置完毕后,点击“Apply”按钮保存设置。 需要注意的是,Material Theme UI插件可能会影响IDE的性能,特别是在处理大型项目中的时候。因此,开发者需要根据项目实际需求来选择是否使用该插件。此外,不同的IDE版本和操作系统可能会对插件的安装和使用产生一定影响,因此开发者在使用Material Theme UI插件时需注意查看相关文档或咨询技术支持人员,以确保插件能够正常运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值