Angular Material2主题 - 如何设置应用背景?

Nar*_*rxx 28 angular-material2 angular

我正在使用角度材料2构建angular2应用程序.我试图将我的应用程序的背景设置为"正确的方法",但我无法弄清楚如何.

我找到了一个可以在我的<body>元素上使用的类:mat-app-background我可以添加,它给了我一个默认颜色(取决于我是使用浅色还是暗色主题).

我希望定义这种背景颜色以使用我的品牌颜色,但我无法弄清楚如何做到这一点.

_theming.scss它中定义如下:

// Mixin that renders all of the core styles that depend on the theme.
@mixin mat-core-theme($theme) {
  @include mat-ripple-theme($theme);
  @include mat-option-theme($theme);
  @include mat-pseudo-checkbox-theme($theme);

  // Wrapper element that provides the theme background when the
  // user's content isn't inside of a `md-sidenav-container`.
  .mat-app-background {
    $background: map-get($theme, background);
    background-color: mat-color($background, background);
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

所以我认为尝试将背景颜色添加到我的自定义主题是有意义的,不知何故,但我无法理解如何这样做.

在Material2 主题文档中,它只说:

"在Angular Material中,主题是通过组合多个调色板创建的.特别是,主题包括:

  • 主要调色板:在所有屏幕和组件中使用最广泛的颜色.
  • 重点调色板:用于浮动操作按钮和交互元素的颜色.
  • 警告调色板:用于传达错误状态的颜色.
  • 前景调色板:文本和图标的颜色.
  • 背景调色板:用于元素背景的颜色."

如何将我的背景添加到主题中,或以其他任何方式执行此操作?

Jak*_*ler 44

如果要以干净的方式更改整个应用程序的主题背景颜色,可以使用以下内容覆盖主题.

// Set custom background color
$custom-background-color: map_get($mat-blue-grey, 50);

// -or- Can set colour by hex value too
$custom-background-color: #628cc9;

$background: map-get($theme, background);
$background: map_merge($background, (background: $custom-background-color));
$theme: map_merge($theme, (background: $background));
Run Code Online (Sandbox Code Playgroud)

这假设您已经设置了$theme使用mat-light-thememat-dark-theme.当然,您可以替换$mat-blue-grey您选择的颜色映射.

这是我如何使用它的完整示例.我在一个名为的文件中有以下内容theme.scss,它包含在我的angular.json"样式"条目中:

// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@include mat-core;

// Define the palettes for your theme using the Material Design palettes available in palette.scss
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
// hue.
$primary: mat-palette($mat-red, 600, 400, 900);
$accent: mat-palette($mat-blue-grey, 500, 200, 700);
$background-color: map_get($mat-blue-grey, 50);

// The warn palette is optional (defaults to red).
$warn: mat-palette($mat-blue);

// Create the theme object (a Sass map containing all of the palettes).
$theme: mat-light-theme($primary, $accent, $warn);

// Insert custom background color
$background: map-get($theme, background);
$background: map_merge($background, (background: $background-color));
$theme: map_merge($theme, (background: $background));

// Include theme styles for core and each component used in your app.
// Alternatively, you can import and @include the theme mixins for each component
// that you are using.
@include angular-material-theme($theme);
@include my-app-theme($theme);
Run Code Online (Sandbox Code Playgroud)

  • @ppumkin @Jesse 我添加了一个完整的例子来说明我是如何使用它的。如果您还没有在其他地方这样做,您可能还需要`@import '~@angular/material/theming';`。 (3认同)
  • 这正是我正在寻找的。我刚刚编辑了示例代码以展示如何直接使用十六进制颜色。从一开始这对我来说并不明显。这应该是公认的答案。 (2认同)
  • 刚刚找到一个方法。安装 `https://www.npmjs.com/package/sass-recursive-map-merge`,然后: ```// NG10+ 有一个嵌套的 `color` 映射。$调色板颜色:地图获取($主题,颜色);$background: map-get($palette-color, 背景); $背景:map_merge($背景,(背景:$背景)); $主题:map_merge($主题,(背景:$background)); $主题:递归地图合并($主题,(颜色:(背景:$背景)));``` (2认同)

小智 13

角12

// For example Orange palette
$orange-palette: mat.define-palette(mat.$orange-palette);

// Define theme
$theme: mat.define-dark-theme((
  color: (
    primary: $orange-palette,
    accent: $orange-palette
  )
));

// Get color param from our theme
$palette-color : map-get($theme, color);
// Get background param from color param
$background: map-get($palette-color, background);
// $background also has background param contains color, set it to red (for example)
$background: map_merge($background, (background: red));
// Set background param for palette
$palette-color: map_merge($palette-color, (background: $background));
// Set palette for theme
$theme: map_merge($theme, (color: $palette-color));

@include mat.all-component-themes($theme);
Run Code Online (Sandbox Code Playgroud)

或者使用函数:

@function mat-set-background($theme, $backgroundColor) {
  $palette-color : map-get($theme, color);
  $background: map-get($palette-color, background);
  $background: map_merge($background, (background: $backgroundColor));
  $palette-color: map_merge($palette-color, (background: $background));
  @return map_merge($theme, (color: $palette-color));
}

$orange-palette: mat.define-palette(mat.$orange-palette);

$theme: mat.define-dark-theme((
  color: (
    primary: $orange-palette,
    accent: $orange-palette
  )
));

$theme: mat-set-background($theme, #FF0000);
@include mat.all-component-themes($theme);
Run Code Online (Sandbox Code Playgroud)


Ben*_*dle 10

如果要设置背景颜色,您可能希望通过使用自己的替换模拟mat-light-theme或mat-dark-theme功能来自定义整个背景和前景调色板.您的替换品将包括您自己的调色板,而不是mat-light-theme-foreground和background palettes.

示例:https://stackblitz.com/edit/angular-material-custom-background?file = theme.scss

我不知道这种方法是推荐还是官方支持.

  • 这对于微调主题非常好,但是如果您要使用默认主题,但只想更改一个或两个属性,请查看下面由Jake撰写的答案/sf/answers/3460395921/。 (2认同)

小智 6

请参阅:github 上的调色板主题 scss Angular (2) 材料 (2)

代码摘录:

// 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),
);
Run Code Online (Sandbox Code Playgroud)

  • 在调用 `mat-light-theme($app-primary, $app-accent, $app-warn)` 之前,我只是用我自己的值重新定义了这些值,它起作用了:D (3认同)

Can*_*ner 6

不能完全回答您的问题,但是我想很多人最终都会在这里搜索“如何设置应用程序背景色”。

在您project/index.html的身体类别中mat-app-background

<body class="mat-app-background">
  <app-root></app-root>
</body>
Run Code Online (Sandbox Code Playgroud)

并确保project/angular.json您拥有:

        "styles": [
          "./node_modules/@angular/material/prebuilt-themes/YOUR_STYLE.css",
          ...
        ],
Run Code Online (Sandbox Code Playgroud)


小智 5

最近,从 Material 10.x 开始,主题方法正在得到改进。使用新方法mat-light-thememat-dark-theme仅接受颜色的配置对象。这是我在评论中发现的:

以前在 Angular Material 中,主题对象直接包含颜色配置。随着最近对主题系统进行重构以允许密度和版式配置,这种情况已不再是这样。

目前,这些方法也支持遗留方法,但我希望很快这将被宣布为一项重大变更。由于我们无法像以前一样将新背景合并到主题对象,因此接受的答案可能不再有效。

我所做的不是那样,而是$mat-light-theme-background在创建新主题之前用我的自定义背景更新了调色板,这完成了工作。

$app-primary: mat-palette($md-light);
$app-accent: mat-palette($mat-pink, A200, A100, A400);
$app-warn: mat-palette($mat-red);
$custom-background-color: map_get($md-light, 50);

$mat-light-theme-background: map_merge($mat-light-theme-background, (background: $custom-background-color));

$light-theme: mat-light-theme((
    color: (
        primary: $app-primary,
        accent: $app-accent,
        warn:  $app-warn
    )
));
Run Code Online (Sandbox Code Playgroud)

注意:$md-light是我为我的应用程序定义的自定义托盘,您可以使用任何其他值。我还尝试过传递“背景”以及其他颜色属性,但由于某种原因不起作用。

包含主题的方式也略有改变。现在有一种新方法需要angular-material-color包含主题。

@include angular-material-color($light-theme);
Run Code Online (Sandbox Code Playgroud)