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

问题描述 投票:27回答:8

我正在使用angular material2构建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);
  }
  ...
}

因此,我认为尝试以某种方式将背景色添加到我的自定义主题中是有道理的,但我不知道该怎么做。

在Material2 theming文档上,它只说:

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

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

如何将背景添加到主题中,或以其他任何方式?

angular angular-material2
8个回答
43
投票

如果您想以整洁的方式更改整个应用程序的主题背景颜色,则可以使用以下内容覆盖主题。

// 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));

这是假设您已经使用$thememat-light-theme设置了mat-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);

10
投票

编辑:此策略涉及替换Material功能。在大多数情况下,我会建议以上杰克·斯托弗勒的回答。

如果您要设置背景颜色,则可能需要通过模拟mat-light-theme或mat-dark-theme函数替换您自己的自定义整个背景和前景调色板。您的替代品将包括您自己的调色板,而不是mat-light-theme-foreground和背景调色板。

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

我不知道是否推荐或正式支持此方法。


6
投票

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

project/index.html中,将您的身体类别设置为mat-app-background

<body class="mat-app-background">
  <app-root></app-root>
</body>

并确保在project/angular.json中有:

        "styles": [
          "./node_modules/@angular/material/prebuilt-themes/YOUR_STYLE.css",
          ...
        ],

2
投票

还有这样的颜色混合:

.your-class-here {
   background: mat-color($mat-grey, 700, 0.9);
}

查看角形材料成分时,您可以分配这样的颜色。

<md-toolbar color="primary">
</md-toolbar>

将使您的工具栏成为您的原色。

还要确保以斜角材质查看_theming.scss文件。

因此您可以使用这些混合来从调色板中提取颜色。


2
投票

请参阅:palette theme scss on github Angular (2) Material (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),
);

2
投票

没有办法“正确”地做到这一点-只有解决方法和破解。您的主题可以定义主要,次要和警告调色板,但不能定义前景和背景调色板。在Angular Material中没有直接/简单的方法来执行此操作的可能原因是,根据Material Design,您不应执行此操作。颜色应以特定方式用于根据您的主题突出显示和设置各种元素的样式,而普通内容的背景色和前景色应为深色或浅灰色-不着色。如果您确实需要这样做,则有关重新定义背景主题或使用您自己的类的其他建议就足够了。


0
投票

我建议您在变量文件中声明背景色(假设您使用的是sass),然后只需在需要时将其导入即可。例如:-

@import '../config/variables';

body {
    height: 100%;
    margin: 0;
    background-color: $brand-primary;
}

0
投票

用于其他解决方案,如果以前的方法不起作用,我将角材料2与封装样式结合使用,那么无论您要怎么做,都不能从外部自定义角材料组件,因此,我一直使用的理想解决方案请尝试使用此方法] >

1-如果您想通过设置ViewEncapsulation.None,则可以使用此技巧,使您可以自定义角形材质组件>

//test.component.ts
import { ViewEncapsulation } from '@angular/core';
@Component({
  selector: 'app-test',
  templateUrl: '.....',
  styleUrls: ['....'],
  encapsulation: ViewEncapsulation.None
})

2-如果您只想更改背景,可以简单尝试一下

//style.css (main style file)
html, body {
  background-color: #fafafa;
}

这将更改您所有应用程序的整个背景颜色

© www.soinside.com 2019 - 2024. All rights reserved.