2017-05-11 22 views
1

我正在使用角材料2構建angular2應用程序。我試圖設置我的應用程序的背景「正確的方式」,但我無法弄清楚如何。Angular Material2主題 - 如何設置應用背景?

我發現我可以在我的<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); 
    } 
    ... 
} 

所以我認爲這將是有意義的嘗試添加背景顏色爲我的自定義主題,不知怎麼的,但我不明白爲什麼這樣做。

在材料2 theming文檔它僅表示:

「以角材料,一個主題是通過合成多個調色板創建特別地,主題的組成:。

  • 初級調色板:顏色最常用於所有屏幕和組件
  • 重音調色板:用於浮動動作按鈕和交互元素的顏色
  • 警告調色板:用於傳達錯誤狀態的顏色
  • 前景調色板:文字和圖標的顏色。
  • 背景調色板:用於元素背景的顏色。 「

我怎樣才能增加我的背景主題,或做任何其他方式

回答

1

也有這樣的顏色混合:

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

當看着角料組件,您可以指定一種顏色像這樣。

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

那將使你的工具欄主的顏色顏色。

也一定要查看角度材料中的_theming.scss文件。

所以你可以使用這些mixins從你的調色板中拉出一種顏色。

+0

感謝您的輸入!你是否建議我爲背景創建一個全局CSS類?我認爲將它添加到主題中會更好,所以我可以通過'color'指令訪問(就像你建議的:'color =「background」')。有可能這樣做嗎? – Narxx

+0

以及如果您正在嘗試爲您的網站添加背景,那麼您可能應該只使用身體標記,並且可以使用mixin提取顏色。如果您使用材質或材料編寫自定義組件,則應該將自動顏色的主題顏色合併,假設其他人會使用您編寫的組件(日期選擇器等)。如果使用內置材質組件,則會暴露[color] = primary,不是以正常的角度或HTML開箱即可存在。 – Zuriel

+0

如何爲''標籤設置背景?除了在'index.html'內部進行硬編碼之外,例如:''? ''元素在''... – Narxx

0

我建議你在變量文件中聲明一個背景顏色(假設你使用的是sass),然後在需要的地方簡單地導入它。例如: -

@import '../config/variables'; 

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

感謝您的輸入!是的,我確實有一個變量文件,我可以將自定義顏色添加爲變量。我應該在哪裏添加'body' CSS規則,因此它可以在'index.html'上訪問?由於Material2將樣式封裝到適當的組件中,所以''元素超出了任何組件範圍。這就是'.mat-app-background'進入圖片的地方,因爲它可以工作,但是我不確定如何覆蓋它... – Narxx

+0

這不起作用 – Ashish

0

見: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), 
); 
+0

你能提供一步一步的解釋嗎?可以定義我的新主題調色板,並將其用作整個應用程序的背景(最好在''元素上,如問題所述? – Narxx

+1

我剛剛在我調用'mat-light-theme(之前調用了這些值) $ app-primary,$ app-accent,$ app-warn)',它的工作原理是:D –

+0

請提供關於實現這個的信息 – Sreekumar

1

有沒有辦法做到這一點 「正常」 - 只有解決辦法和黑客。您的主題可以定義主要,次要和警告調色板,但不能定義前景和背景調色板。爲什麼在Angular Material中沒有直接/簡單的方法來做這件事的可能原因是根據Material Design,你不應該這樣做。顏色旨在以特定方式根據您的主題使用各種元素進行突出顯示和設計,但對於普通內容而言,背景和前景顏色可能是黑色或淺灰色,而不是彩色。如果你真的需要這樣做,關於重新定義背景主題或使用自己的類的其他建議應該是足夠的。