openharmony 鸿蒙 js-apis-arkui-theme

2025-06-12 浏览 (1)

@ohos.arkui.theme(Theme)

You can define a custom theme to apply to components in your application.

NOTE

The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { Theme, ThemeControl, CustomColors, Colors, CustomTheme } from '@kit.ArkUI';

Theme

Defines the Theme object in use, which can be obtained through onWillApplyTheme.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeReadableWritableDescription
colorsColorsNoNoColor resources of the theme.

Colors

Defines the color resources of a theme.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeReadableWritableDescription
brandResourceColorNoNoBrand color.
warningResourceColorNoNoWarning color.
alertResourceColorNoNoAlert color.
confirmResourceColorNoNoConfirmation color.
fontPrimaryResourceColorNoNoPrimary font color.
fontSecondaryResourceColorNoNoSecondary font color.
fontTertiaryResourceColorNoNoTertiary font color.
fontFourthResourceColorNoNoFourth-level font color.
fontEmphasizeResourceColorNoNoEmphasis font color.
fontOnPrimaryResourceColorNoNoPrimary inverted font color used on color background.
fontOnSecondaryResourceColorNoNoSecondary inverted font color used on color background.
fontOnTertiaryResourceColorNoNoTertiary inverted font color used on color background.
fontOnFourthResourceColorNoNoFourth-level inverted font color used on color background.
iconPrimaryResourceColorNoNoPrimary icon color.
iconSecondaryResourceColorNoNoSecondary icon color.
iconTertiaryResourceColorNoNoTertiary icon color.
iconFourthResourceColorNoNoFourth-level icon color.
iconEmphasizeResourceColorNoNoEmphasis icon color.
iconSubEmphasizeResourceColorNoNoColor of the emphasis auxiliary icon.
iconOnPrimaryResourceColorNoNoPrimary inverted icon color used on color background.
iconOnSecondaryResourceColorNoNoSecondary inverted icon color used on color background.
iconOnTertiaryResourceColorNoNoTertiary inverted icon color used on color background.
iconOnFourthResourceColorNoNoFourth-level inverted icon color used on color background.
backgroundPrimaryResourceColorNoNoPrimary background color (solid, opaque).
backgroundSecondaryResourceColorNoNoSecondary background color (solid, opaque).
backgroundTertiaryResourceColorNoNoTertiary background color (solid, opaque).
backgroundFourthResourceColorNoNoFourth-level background color (solid, opaque).
backgroundEmphasizeResourceColorNoNoEmphasis background color (solid, opaque).
compForegroundPrimaryResourceColorNoNoForeground.
compBackgroundPrimaryResourceColorNoNoWhite background.
compBackgroundPrimaryTranResourceColorNoNoWhite transparent background.
compBackgroundPrimaryContraryResourceColorNoNoAlways-on background.
compBackgroundGrayResourceColorNoNoGray background.
compBackgroundSecondaryResourceColorNoNoSecondary background.
compBackgroundTertiaryResourceColorNoNoTertiary background.
compBackgroundEmphasizeResourceColorNoNoEmphasis background.
compBackgroundNeutralResourceColorNoNoBlack, neutral, emphasis background.
compEmphasizeSecondaryResourceColorNoNo20% emphasis background color.
compEmphasizeTertiaryResourceColorNoNo10% emphasis background color.
compDividerResourceColorNoNoCommon divider color.
compCommonContraryResourceColorNoNoCommon inverted color.
compBackgroundFocusResourceColorNoNoBackground color in the focused state.
compFocusedPrimaryResourceColorNoNoPrimary inverted color in the focused state.
compFocusedSecondaryResourceColorNoNoSecondary inverted color in the focused state.
compFocusedTertiaryResourceColorNoNoTertiary inverted color in the focused state.
interactiveHoverResourceColorNoNoCommon interactive color for the hover state.
interactivePressedResourceColorNoNoCommon interactive color for the pressed state.
interactiveFocusResourceColorNoNoCommon interactive color for the focused state.
interactiveActiveResourceColorNoNoCommon interactive color for the active state.
interactiveSelectResourceColorNoNoCommon interactive color for the selected state.
interactiveClickResourceColorNoNoCommon interactive color for the clicked state.

CustomTheme

Defines a custom theme object.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeRead OnlyOptionalDescription
colorsCustomColorsNoYesColor resources of the custom theme.

CustomColors

type CustomColors = Partial<Colors>

Defines the color resources of a custom theme.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

TypeDescription
Partial<Colors>Color resources of the custom theme.

ThemeControl

Implements a ThemeControl object to apply the custom theme to the components in the application.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

setDefaultTheme

setDefaultTheme(theme: CustomTheme): void

Sets a custom theme as the default, application-level theme so that it is applied to components in the application.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
themeCustomThemeYesCustom theme to set.

Example

import { CustomTheme, CustomColors, ThemeControl } from '@kit.ArkUI';
// Custom theme color
class BlueColors implements CustomColors {
  fontPrimary = Color.White;
  backgroundPrimary = Color.Blue;
  brand = Color.Blue; // Brand color
}

class PageCustomTheme implements CustomTheme {
  colors?: CustomColors;

  constructor(colors: CustomColors) {
    this.colors = colors;
  }
}
// Create an instance.
const BlueColorsTheme = new PageCustomTheme(new BlueColors());
// Call ThemeControl.setDefaultTheme before page build to set the default application style to BlueColorsTheme.
ThemeControl.setDefaultTheme(BlueColorsTheme);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkUI

harmony 鸿蒙ARKUI_TextPickerCascadeRangeContent

harmony 鸿蒙ARKUI_TextPickerRangeContent

harmony 鸿蒙ArkUI_AnimateCompleteCallback

harmony 鸿蒙ArkUI_AttributeItem

harmony 鸿蒙ArkUI_ColorStop

harmony 鸿蒙ArkUI_ContextCallback

harmony 鸿蒙ArkUI_EventModule

harmony 鸿蒙ArkUI_ExpectedFrameRateRange

harmony 鸿蒙ArkUI_IntOffset

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/e3TiZb