Angular Material 更改应用程序背景和前景主题

2024-01-04

这个问题 https://stackoverflow.com/questions/43919927/关于在 Angular Material 中设置背景颜色的问题于 2017 年发布,答案已过时。最新版本的 Angular Material (12.0.4) 似乎对 scss mixin 进行了很大的改变。

在更新到当前版本之前,我能够实现这个答案 https://stackoverflow.com/a/45028242/来自先前链接的问题,如下所示:

// Background palette for light themes.
$mat-light-theme-background: (
  status-bar: map_get($mat-grey, 300),
  app-bar:    map_get($mat-grey, 100),
  background: map_get($mat-grey, 50),
  hover:      rgba(black, 0.04), // TODO(kara): check style with Material Design UX
  card:       white,
  dialog:     white,
  disabled-button: $black-12-opacity,
  raised-button: white,
  focused-button: $black-6-opacity,
  selected-button: map_get($mat-grey, 300),
  selected-disabled-button: map_get($mat-grey, 400),
  disabled-button-toggle: map_get($mat-grey, 200),
);

// Background palette for dark themes.
$mat-dark-theme-background: (
  status-bar: black,
  app-bar:    map_get($mat-grey, 900),
  background: #303030,
  hover:      rgba(white, 0.04), // TODO(kara): check style with Material Design UX
  card:       map_get($mat-grey, 800),
  dialog:     map_get($mat-grey, 800),
  disabled-button: $white-12-opacity,
  raised-button: map-get($mat-grey, 800),
  focused-button: $white-6-opacity,
  selected-button: map_get($mat-grey, 900),
  selected-disabled-button: map_get($mat-grey, 800),
  disabled-button-toggle: map_get($mat-grey, 1000),
);

// Foreground palette for light themes.
$mat-light-theme-foreground: (
  base:              black,
  divider:           $black-12-opacity,
  dividers:          $black-12-opacity,
  disabled:          rgba(black, 0.38),
  disabled-button:   rgba(black, 0.38),
  disabled-text:     rgba(black, 0.38),
  hint-text:         rgba(black, 0.38),
  secondary-text:    rgba(black, 0.54),
  icon:              rgba(black, 0.54),
  icons:             rgba(black, 0.54),
  text:              rgba(black, 0.87),
  slider-off:        rgba(black, 0.26),
  slider-off-active: rgba(black, 0.38),
);

// Foreground palette for dark themes.
$mat-dark-theme-foreground: (
  base:              white,
  divider:           $white-12-opacity,
  dividers:          $white-12-opacity,
  disabled:          rgba(white, 0.3),
  disabled-button:   rgba(white, 0.3),
  disabled-text:     rgba(white, 0.3),
  hint-text:         rgba(white, 0.3),
  secondary-text:    rgba(white, 0.7),
  icon:              white,
  icons:             white,
  text:              white,
  slider-off:        rgba(white, 0.3),
  slider-off-active: rgba(white, 0.3),
);

此代码需要在调用之前放置mat-light-theme($app-primary, $app-accent, $app-warn).

变量名称似乎已更改为:

$light-theme-background-palette: (...)
$dark-theme-background-palette: (...)
$light-theme-foreground-palette: (...)
$dark-theme-foreground-palette: (...)

(这可以在Github 仓库 https://github.com/angular/components/blob/master/src/material/core/theming/_palette.scss对于角度材料)。

我尝试在调用之前设置这些变量@include mat.all-component-themes($app-theme);,但这似乎并没有改变任何应用程序的背景颜色。

与往常一样,有关的文档角度材质页面 https://material.angular.io/guide/theming缺乏这方面的任何信息。

任何有关如何设置背景和前景变量的建议将不胜感激。


我遇到了同样的问题并找到了解决方案。我在评论里解释一下。

@use 'sass:map';
@use '~@angular/material' as mat;
@include mat.core();

// My version of the dark and light background and foreground palettes
// I copied the ones in the _palette.scss file (the Github repo link you posted) 
// and tweaked the values to my liking.
@use './my-styles' as my;

$my-theme-primary: mat.define-palette(mat.$grey-palette, 900);
$my-theme-accent: mat.define-palette(mat.$orange-palette, 500);
$my-theme-warn: mat.define-palette(mat.$red-palette, 800);

// This function ALWAYS sets the background and foreground using _palette.scss
// It will write over any background and foreground you set before it.
$my-theme: mat.define-dark-theme(
  (
    color: (
      primary: $my-theme-primary,
      accent: $my-theme-accent,
      warn: $my-theme-warn,
    ),
  )
);

// If we look at the result of this debug, we can see the map it created.
// And the structure to copy when setting our background and foreground :)
// Note: the properties are repeated. First inside the color property and then after it. 
// Weird, though there might be a reason. I tested and for now I think 
// only the ones under color are the necessary ones.  
@debug $my-theme;

// Write over background and foreground with my versions. 
$my-theme: map.set(
  $my-theme,
  color,
  background,
  my.$dark-background
);

$my-theme: map.set(
  $my-theme,
  color,
  foreground,
  my.$dark-foreground
);

// Change the repeated ones after color just in case
$my-theme: map.set(
  $my-theme,
  background,
  my.$dark-background
);

$my-theme: map.set(
  $my-theme,
  foreground,
  my.$dark-foreground
);

// use my theme everywhere
@include mat.all-component-colors($my-theme);

您始终可以从头开始创建自己的主题,而无需使用 Define-dark-theme / Define-light-theme 函数。这些函数添加前景、背景和暗图。所以你可以只添加它们而不使用该功能。只需确保它具有与这些函数返回的结构相同的结构即可。

您还可以更改单个属性,如前面的解决方案所示。我正在使用map.set(),因为我喜欢它。

$my-theme: map.set(
  $my-theme,
  color,
  background,
  background, 
  pink
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular Material 更改应用程序背景和前景主题 的相关文章

随机推荐

  • 设置远程共享继承其父目录权限的 C# 代码

    我有两台计算机 分别称为客户端和服务器 位于 Windows 域中 服务器有一个可以从客户端计算机访问的共享目录 我想在客户端上运行一个 C 应用程序 该应用程序设置此共享的权限以继承服务器上共享的父目录的权限 我该怎么做呢 我已经尝试过以
  • 在 Google Cloud Storage 存储桶上设置缓存控制

    我有一个存储桶 其中包含经常更新的公共图像 并且希望禁用默认缓存持续时间 3600 例如 Cache Control private max age 0 no transform 使用 PHP 上传文件时可以设置缓存控制吗 有没有办法为存储
  • git 如何检测文件已被修改?

    git 如何如此快地检测到文件修改 它是否对存储库中的每个文件进行哈希处理并比较 SHA1 这会花费很多时间 不是吗 或者说比较atime ctime or mtime Git 努力仅从 lstat 值中确信工作树与索引匹配 因为回退到文件
  • 如何从 TableList POI 中移除/删除表格

    我正在使用模板 docx 文件来填充每个表上的数据 但在某些情况下我不想要同一个表 是否有使用可以删除 删除的 XWPFTable 你可以试试 int position document getPosOfTable table docume
  • 如何让 .NET 取消未使用的 RAM?

    以下是我的程序在极其密集地使用内存后的统计数据 在峰值时消耗 6 GB 但随后将所有内容保存到磁盘并在范围内留下很少的内容 观察到几乎所有内容都超出了范围并已被垃圾收集 堆大小很小 然而 NET 保留了 181 MB坚定的 我不介意保留字节
  • 如何将 warnings() 输出转换为字符串

    当我在控制台输入 warnings 时 我回来了 Warning message In fread my directory C function strtod returned ERANGE for one or more fields
  • 在 google colab 中找不到 kaggle.json 文件

    我正在尝试将 kaggle imagenet 对象本地化挑战数据下载到 google colab 中 以便我可以用它来训练我的模型 Kaggle 使用 API 来轻松快速地访问其数据集 https github com Kaggle kag
  • 当我添加对 Cargo.toml 文件的依赖项时,阻止等待包缓存上的文件锁定

    我向 Rust 项目添加了依赖项 然后运行了 Cargo Run 这给了我这个错误 阻止等待包缓存上的文件锁定 我尝试了在网上找到的解决方案 即删除 cargo package cache 文件 但这对我不起作用 当我输入类似 cargo
  • 计划任务调用的批处理文件在计划时抛出错误,双击时运行正常

    我有一个映射网络驱动器的批处理文件 大约一周前 密码过期了 因此调用批处理文件的程序开始抛出错误 我已经更新了批处理文件中的密码 当我双击批处理文件时 驱动器映射正常 但是 当计划任务开始时 我收到以下错误 Logon failure un
  • 我们可以使用 match 来检查类的类型吗

    我是 scala 新手 我正在学习match现在关键字 我想知道我们是否可以使用关键字match检查类的类型 我的代码是 object Main def main args Array String val x AA checkType x
  • Java EE/JPA 向数据库添加新表/实体的方法

    我有一个mysql数据库 我想添加向数据库添加新表的功能 我可能可以轻松找到 JPQL 的示例 但是如何自动为这个新表生成实体 以便我可以在 JPA 代码的其余部分中引用它 以便从表中更新和删除我通常引用该实体不是实际的表本身 我现在拥有的
  • 如何在 Windows 上使用 PyInstaller 创建 Linux 和 MacOS 可执行文件?

    我使用 Pyinstaller 从 python 代码制作了一个独立的 Windows 便携式应用程序 并且它工作正常 我知道要为某个操作系统创建可执行文件 必须在该特定操作系统上完成 有没有一种方法可以直接从 Windows 创建适用于其
  • 阻止 TFS 添加临时文件

    我正在开发一个在 TFS 中绑定的项目 该项目为我提供了一个文件夹 Log 当我执行我的项目并进行一些测试时 它会生成文本文件并存储在 Log 文件夹中 之后 当我尝试签入文件时 这些文件将作为 TFS 中的新文件签入 我希望 TFS 排除
  • python 将 10 位日期时间戳转换为 13 位 GMT 时间戳 [重复]

    这个问题在这里已经有答案了 我从像这样的服务器收到时间戳 1512543958 当我在标头中发回请求时 我看到像这样的 13 位 GMT 时间戳 1512544485819 通过使用下面的代码将时间更改为本地时间 我得到2017 12 06
  • 无状态 Web 应用程序,都市传奇?

    我试图理解token based authentication这些天 它声称是stateless authentication方法 我遇到了这个概念stateless web application 以下是我读到的一些主题 使用 Sprin
  • Ionic4 未知浏览器查询

    我已经练习了很多 Ionic 所以这些问题对我来说并不新鲜 但我无法解决这个问题 我目前正在尝试将我的 Ionic3 项目迁移到 Ionic4 为此 我创建了一个新的 Ionic4 选项卡项目 每次我尝试 Ionic 服务命令时 我都会得到
  • 如何在浏览器客户端使用nunjucks宏?

    当我从 node js 预编译并公开 JS 模板文件时 我可以使用 nunjucks 的客户端模板 我这样称呼客户端模板 nunjucks render partials some template html abc 123 并拿回一根绳子
  • 2.10 中 Scala REPL breakIf 的替代方案

    我正在读书here https stackoverflow com questions 2160355 drop into interpreter during arbitrary scala code location关于使用breakI
  • IE7清除浮动问题

    您好 这是我在使用 IE7 时遇到的问题的简化版本 基本上 清除的 div 绿色 之后的 div 不会按预期运行 在 IE7 中 它在 Safari FF 等和 IE8 中按预期工作 有人有任何修复建议吗 谢谢你的帮助
  • Angular Material 更改应用程序背景和前景主题

    这个问题 https stackoverflow com questions 43919927 关于在 Angular Material 中设置背景颜色的问题于 2017 年发布 答案已过时 最新版本的 Angular Material 1