openharmony 鸿蒙 ohos-arkui-advanced-SegmentButtonV2

2025-06-12 浏览 (1)

SegmentButtonV2

SegmentButtonV2 is a component used to create tab-style, single-select, and multi-select capsule-style buttons.

NOTE

This component and its child components are supported since API version 18. Updates will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { TabSegmentButtonV2, CapsuleSegmentButtonV2, MultiCapsuleSegmentButtonV2, SegmentButtonV2Items } from '@kit.ArkUI';

Child Components

Not supported

TabSegmentButtonV2

TabSegmentButtonV2({
  items: SegmentButtonV2Items,
  selectedIndex: number,
  $selectedIndex?: OnSelectedIndexChange,
  onItemClicked?: Callback<number>,
  itemMinFontScale?: number|Resource,
  itemMaxFontScale?: number|Resource,
  itemSpace?: LengthMetrics,
  itemFontSize?: LengthMetrics,
  itemSelectedFontSize?: LengthMetrics,
  itemFontColor?: ColorMetrics,
  itemSelectedFontColor?: ColorMetrics,
  itemFontWeight?: FontWeight,
  itemSelectedFontWeight?: FontWeight,
  itemBorderRadius?: LengthMetrics,
  itemSelectedBackgroundColor?: ColorMetrics,
  itemIconSize?: SizeT<LengthMetrics>,
  itemIconFillColor?: ColorMetrics,
  itemSelectedIconFillColor?: ColorMetrics,
  itemSymbolFontSize?: LengthMetrics,
  itemSymbolFontColor?: ColorMetrics,
  itemSelectedSymbolFontColor?: ColorMetrics,
  itemMinHeight?: LengthMetrics,
  itemPadding?: LocalizedPadding,
  itemShadow?: ShadowOptions|ShadowStyle,
  buttonBackgroundColor?: ColorMetrics,
  buttonBackgroundBlurStyle?: BlurStyle,
  buttonBackgroundBlurStyleOptions?: BackgroundBlurStyleOptions,
  buttonBackgroundEffect?: BackgroundEffectOptions,
  buttonBorderRadius?: LengthMetrics, 
  buttonMinHeight?: LengthMetrics, 
  buttonPadding?: LengthMetrics, 
  languageDirection?: Direction 
})

Decorator: @ComponentV2

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeRead-OnlyOptionalDecoratorDescription
itemsSegmentButtonV2ItemsYesNo@Require
@Param
Items of the TabSegmentButtonV2 component.
selectedIndexnumberYesNo@Require
@Param
Index of the selected item of the TabSegmentButtonV2 component.
$selectedIndexOnSelectedIndexChangeNoYes@EventCallback invoked when the selected item of the TabSegmentButtonV2 component changes.
onItemClickedCallback<number>NoYes@EventCallback invoked when an item in the TabSegmentButtonV2 component is clicked.
buttonBackgroundColorColorMetricsYesYes@ParamBackground color of the TabSegmentButtonV2 component.
Default value: $r('sys.color.segment_button_v2_tab_button_background')
buttonBackgroundBlurStyleBlurStyleYesYes@ParamBackground blur style of the TabSegmentButtonV2 component.
buttonBackgroundBlurStyleOptionsBackgroundBlurStyleOptionsYesYes@ParamBackground blur style options of the TabSegmentButtonV2 component.
buttonBackgroundEffectBackgroundEffectOptionsYesYes@ParamBackground blur effect options of the TabSegmentButtonV2 component.
buttonBorderRadiusLengthMetricsYesYes@ParamRadius of the background rounded corner.
Value range: [0, +∞)
Default value: $r('sys.float.segment_button_v2_background_corner_radius')
buttonMinHeightLengthMetricsYesYes@ParamMinimum height of the TabSegmentButtonV2 component.
Value range: [0, +∞)
Default value: $r('sys.float.segment_button_v2_singleline_background_height') for text-only buttons and icon-only buttons, and $r('sys.float.segment_button_v2_doubleline_background_height') for buttons with both an icon and text.
buttonPaddingLengthMetricsYesYes@ParamPadding of the TabSegmentButtonV2 component.
Value range: [0, +∞)
Default value: $r('sys.float.padding_level1')
onItemClickedCallback<number>YesYes@EventCallback invoked when an item in the TabSegmentButtonV2 component is clicked.
itemSelectedBackgroundColorColorMetricsYesYes@ParamBackground color of the selected item of the TabSegmentButtonV2 component.
Default value: $r('sys.color.segment_button_v2_tab_selected_item_background')
itemMinHeightLengthMetricsYesYes@ParamMinimum height of the item of the TabSegmentButtonV2 component.
Value range: [0, +∞)
Default value:
$r('sys.float.segment_button_v2_singleline_selected_height') for text-only buttons and icon-only buttons, and $r('sys.float.segment_button_v2_doubleline_selected_height') for buttons with both an icon and text.
itemPaddingLocalizedPaddingYesYes@ParamPadding of the item of the TabSegmentButtonV2 component.
Default value: {top: LengthMetrics.resource ($r('sys.float.padding_level2')), bottom: LengthMetrics.resource ($r('sys.float.padding_level2')), start: LengthMetrics.resource($r('sys.float.padding_level4')), end: LengthMetrics.resource($r('sys.float.padding_level4'))}
itemShadowShadowOptions |ShadowStyleYesYes@ParamShadow of the item of the TabSegmentButtonV2 component.
Default value: ShadowStyle.OUTER_DEFAULT_XS
itemSpaceLengthMetricsYesYes@ParamSpace between items of the TabSegmentButtonV2 component.
Value range: [0, +∞)
Default value: LengthMetrics.vp(0)
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemMinFontScalenumber |ResourceYesYes@ParamMinimum font scale for the item of the TabSegmentButtonV2 component.
Value range: [0, 1]
NOTE
A value less than 0 is handled as 0. A value greater than 1 is handled as 1. Abnormal values are ineffective by default.
itemMaxFontScalenumber |ResourceYesYes@ParamMaximum font scale for the item of the TabSegmentButtonV2 component.
Value range: [1, 2]
Default value: 1
NOTE
A value less than 1 is handled as 1. A value greater than 2 is handled as 2. Abnormal values are ineffective by default.
itemFontSizeLengthMetricsYesYes@ParamFont size of the unselected item of the TabSegmentButtonV2 component.
Value range: [0, +∞)
Default value: 14fp
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemSelectedFontSizeLengthMetricsYesYes@ParamFont size of the selected item of the TabSegmentButtonV2 component.
Value range: [0, +∞)
Default value: 14fp
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemFontColorColorMetricsYesYes@ParamFont color of the unselected item of the TabSegmentButtonV2 component.
Default value: $r('sys.color.font_secondary')
itemSelectedFontColorColorMetricsYesYes@ParamFont color of the selected item of the TabSegmentButtonV2 component.
Default value: $r('sys.color.font_primary')
itemFontWeightFontWeightYesYes@ParamFont weight of the unselected item of the TabSegmentButtonV2 component.
Default value: FontWeight.Medium
itemSelectedFontWeightFontWeightYesYes@ParamFont weight of the selected item of the TabSegmentButtonV2 component.
Default value: FontWeight.Medium
itemBorderRadiusLengthMetricsYesYes@ParamRounded corner radius of the item of the TabSegmentButtonV2 component.
Value range: [0, +∞)
Default value: $r('sys.float.segment_button_v2_selected_corner_radius')
itemIconSizeSizeT<LengthMetrics>YesYes@ParamSize of the image icon of the item in the TabSegmentButtonV2 component.
Value range: [0, +∞)
Default value: { width: LengthMetrics.vp(24), height: LengthMetrics.vp(24) }
itemIconFillColorColorMetricsYesYes@ParamIcon color of the unselected item of the TabSegmentButtonV2 component.
Default value: $r('sys.color.font_secondary')
itemSelectedIconFillColorColorMetricsYesYes@ParamIcon color of the selected item of the TabSegmentButtonV2 component.
Default value: $r('sys.color.font_primary')
itemSymbolFontSizeLengthMetricsYesYes@ParamSize of the HM Symbol icon of the item in the TabSegmentButtonV2 component.
Value range: [0, +∞)
Default value: 20fp
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemSymbolFontColorColorMetricsYesYes@ParamColor of the HM Symbol icon of the unselected items in the TabSegmentButtonV2 component.
Default value: $r('sys.color.font_secondary')
itemSelectedSymbolFontColorColorMetricsYesYes@ParamColor of the HM Symbol icon of the selected item in the TabSegmentButtonV2 component.
Default value: $r('sys.color.font_primary')
languageDirectionDirectionYesYes@ParamLanguage direction of the TabSegmentButtonV2 component.
Default value: Direction.Auto

CapsuleSegmentButtonV2

CapsuleSegmentButtonV2({
  items: SegmentButtonV2Items,
  selectedIndex: number,
  $selectedIndex?: OnSelectedIndexChange,
  onItemClicked?: Callback<number>,
  itemMinFontScale?: number|Resource,
  itemMaxFontScale?: number|Resource,
  itemSpace?: LengthMetrics,
  itemFontSize?: LengthMetrics,
  itemSelectedFontSize?: LengthMetrics,
  itemFontColor?: ColorMetrics,
  itemSelectedFontColor?: ColorMetrics,
  itemFontWeight?: FontWeight,
  itemSelectedFontWeight?: FontWeight,
  itemBorderRadius?: LengthMetrics,
  itemSelectedBackgroundColor?: ColorMetrics,
  itemIconSize?: SizeT<LengthMetrics>,
  itemIconFillColor?: ColorMetrics,
  itemSelectedIconFillColor?: ColorMetrics,
  itemSymbolFontSize?: LengthMetrics,
  itemSymbolFontColor?: ColorMetrics,
  itemSelectedSymbolFontColor?: ColorMetrics,
  itemMinHeight?: LengthMetrics,
  itemPadding?: LocalizedPadding,
  itemShadow?: ShadowOptions|ShadowStyle,
  buttonBackgroundColor?: ColorMetrics,
  buttonBackgroundBlurStyle?: BlurStyle,
  buttonBackgroundBlurStyleOptions?: BackgroundBlurStyleOptions,
  buttonBackgroundEffect?: BackgroundEffectOptions,
  buttonBorderRadius?: LengthMetrics,
  buttonMinHeight?: LengthMetrics,
  buttonPadding?: LengthMetrics,
  languageDirection?: Direction 
})

Decorator: @ComponentV2

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeRead-OnlyMandatoryDecoratorDescription
itemsSegmentButtonV2ItemsYesNo@Require
@Param
Items of the CapsuleSegmentButtonV2 component.
selectedIndexnumberYesNo@Require
@Param
Index of the selected item of the CapsuleSegmentButtonV2 component.
$selectedIndexOnSelectedIndexChangeNoYes@EventCallback invoked when the selected item of the CapsuleSegmentButtonV2 component changes.
onItemClickedCallback<number>NoYes@EventCallback invoked when an item in the CapsuleSegmentButtonV2 component is clicked.
buttonBackgroundColorColorMetricsYesYes@ParamBackground color of the CapsuleSegmentButtonV2 component.
Default value: $r('sys.color.segment_button_v2_tab_button_background')
buttonBackgroundBlurStyleBlurStyleYesYes@ParamBackground blur style of the CapsuleSegmentButtonV2 component.
buttonBackgroundBlurStyleOptionsBackgroundBlurStyleOptionsYesYes@ParamBackground blur style options of the TabSegmentButtonV2 component.
buttonBackgroundEffectBackgroundEffectOptionsYesYes@ParamBackground blur effect options of the TabSegmentButtonV2 component.
buttonBorderRadiusLengthMetricsYesYes@ParamRadius of the background rounded corner of the CapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: $r('sys.float.segment_button_v2_background_corner_radius')
buttonMinHeightLengthMetricsYesYes@ParamMinimum height of the CapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: $r('sys.float.segment_button_v2_singleline_background_height') for text-only buttons and icon-only buttons, and $r('sys.float.segment_button_v2_doubleline_background_height') for buttons with both an icon and text.
buttonPaddingLengthMetricsYesYes@ParamPadding of the CapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: $r('sys.float.padding_level1')
onItemClickedCallback<number>YesYes@EventCallback invoked when an item in the CapsuleSegmentButtonV2 component is clicked.
itemSelectedBackgroundColorColorMetricsYesYes@ParamBackground color of the selected item of the CapsuleSegmentButtonV2 component.
Default value: $r('sys.color.comp_background_emphasize')
itemMinHeightLengthMetricsYesYes@ParamMinimum height of the item of the CapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value:
$r('sys.float.segment_button_v2_singleline_selected_height') for text-only buttons and icon-only buttons, and $r('sys.float.segment_button_v2_doubleline_selected_height') for buttons with both an icon and text.
itemPaddingLocalizedPaddingYesYes@ParamPadding of the item of the CapsuleSegmentButtonV2 component.
Default value: {top: LengthMetrics.resource ($r('sys.float.padding_level2')), bottom: LengthMetrics.resource ($r('sys.float.padding_level2')), start: LengthMetrics.resource($r('sys.float.padding_level4')), end: LengthMetrics.resource($r('sys.float.padding_level4'))}
itemShadowShadowOptions |ShadowStyleYesYes@ParamShadow of the item of the CapsuleSegmentButtonV2 component.
Default value: ShadowStyle.OUTER_DEFAULT_XS
itemSpaceLengthMetricsYesYes@ParamSpace between items of the CapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: LengthMetrics.vp(0)
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemMinFontScalenumber |ResourceYesYes@ParamMinimum font scale for the item of the CapsuleSegmentButtonV2 component.
Value range: [0, 1]
NOTE
A value less than 0 is handled as 0. A value greater than 1 is handled as 1. Abnormal values are ineffective by default.
itemMaxFontScalenumber |ResourceYesYes@ParamMaximum font scale for the item of the CapsuleSegmentButtonV2 component.
Value range: [1, 2]
Default value: 1
NOTE
A value less than 1 is handled as 1. A value greater than 2 is handled as 2. Abnormal values are ineffective by default.
itemFontSizeLengthMetricsYesYes@ParamFont size of the unselected item of the CapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: 14fp
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemSelectedFontSizeLengthMetricsYesYes@ParamFont size of the selected item of the CapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: 14fp
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemFontColorColorMetricsYesYes@ParamFont color of the unselected item of the CapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_secondary')
itemSelectedFontColorColorMetricsYesYes@ParamFont color of the selected item of the CapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_on_primary')
itemFontWeightFontWeightYesYes@ParamFont weight of the unselected item of the CapsuleSegmentButtonV2 component.
Default value: FontWeight.Medium
itemSelectedFontWeightFontWeightYesYes@ParamFont weight of the selected item of the CapsuleSegmentButtonV2 component.
Default value: FontWeight.Medium
itemBorderRadiusLengthMetricsYesYes@ParamRounded corner radius of the item of the CapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: $r('sys.float.segment_button_v2_selected_corner_radius')
itemIconSizeSizeT<LengthMetrics>YesYes@ParamSize of the image icon of the item in the CapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: { width: LengthMetrics.vp(24), height: LengthMetrics.vp(24) }
itemIconFillColorColorMetricsYesYes@ParamIcon color of the unselected item of the CapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_secondary')
itemSelectedIconFillColorColorMetricsYesYes@ParamIcon color of the selected item of the CapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_on_primary')
itemSymbolFontSizeLengthMetricsYesYes@ParamSize of the HM Symbol icon of the item in the CapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: 20fp
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemSymbolFontColorColorMetricsYesYes@ParamColor of the HM Symbol icon of the unselected items in the CapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_secondary')
itemSelectedSymbolFontColorColorMetricsYesYes@ParamColor of the HM Symbol icon of the selected item in the CapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_on_primary')
languageDirectionDirectionYesYes@ParamLanguage direction of the CapsuleSegmentButtonV2 component.
Default value: Direction.Auto

MultiCapsuleSegmentButtonV2

MultiCapsuleSegmentButtonV2({
  items: SegmentButtonV2Items,
  selectedIndexes: number[],
  $selectedIndexes: OnSelectedIndexesChange,
  onItemClicked?: Callback<number>,
  itemMinFontScale?: number|Resource,
  itemMaxFontScale?: number|Resource,
  itemSpace?: LengthMetrics,
  itemFontColor?: ColorMetrics,
  itemSelectedFontColor?: ColorMetrics,
  itemFontSize?: LengthMetrics,
  itemSelectedFontSize?: LengthMetrics,
  itemFontWeight?: FontWeight,
  itemSelectedFontWeight?: FontWeight,
  itemBorderRadius?: LengthMetrics,
  itemBackgroundColor?: ColorMetrics,
  itemBackgroundEffect?: BackgroundEffectOptions,
  itemBackgroundBlurStyle?: BlurStyle,
  itemBackgroundBlurStyleOptions?: BackgroundBlurStyleOptions,
  itemSelectedBackgroundColor?: ColorMetrics,
  itemIconSize?: SizeT<LengthMetrics>,
  itemIconFillColor?: ColorMetrics,
  itemSelectedIconFillColor?: ColorMetrics,
  itemSymbolFontSize?: LengthMetrics,
  itemSymbolFontColor?: ColorMetrics,
  itemSelectedSymbolFontColor?: ColorMetrics,
  itemMinHeight?: LengthMetrics,
  itemPadding?: LocalizedPadding,
  languageDirection?: Direction
})

Decorator: @ComponentV2

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeRead-OnlyMandatoryDecoratorDescription
itemsSegmentButtonV2ItemsYesNo@Require
@Param
Items of the MultiCapsuleSegmentButtonV2 component.
selectedIndexesnumber[]YesNo@Require
@Param
Index of the selected item of the MultiCapsuleSegmentButtonV2 component.
$selectedIndexesOnSelectedIndexesChangeNoYes@EventCallback invoked when the selected item of the MultiCapsuleSegmentButtonV2 component changes.
onItemClickedCallback<number>NoYes@EventCallback invoked when an item in the MultiCapsuleSegmentButtonV2 component is clicked.
itemBackgroundColorColorMetricsYesYes@ParamBackground color of the unselected item of the MultiCapsuleSegmentButtonV2 component.
Default value: $r('sys.color.segment_button_v2_multi_capsule_button_background')
itemBackgroundEffectBackgroundEffectOptionsYesYes@ParamBackground effect of the unselected item of the MultiCapsuleSegmentButtonV2 component.
itemBackgroundBlurStyleBlurStyleYesYes@ParamBackground blur effect of the unselected item of the MultiCapsuleSegmentButtonV2 component.
itemBackgroundBlurStyleOptionsBackgroundBlurStyleOptionsYesYes@ParamBackground blur style options of the unselected item of the MultiCapsuleSegmentButtonV2 component.
itemSelectedBackgroundColorColorMetricsYesYes@ParamBackground color of the selected item of the MultiCapsuleSegmentButtonV2 component.
Default value: $r('sys.color.comp_background_emphasize')
itemMinHeightLengthMetricsYesYes@ParamMinimum height of the item of the MultiCapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value:
$r('sys.float.segment_button_v2_singleline_selected_height') for text-only buttons and icon-only buttons, and $r('sys.float.segment_button_v2_doubleline_selected_height') for buttons with both an icon and text.
itemPaddingLocalizedPaddingYesYes@ParamPadding of the item of the MultiCapsuleSegmentButtonV2 component.
Default value: {top: LengthMetrics.resource ($r('sys.float.padding_level2')), bottom: LengthMetrics.resource ($r('sys.float.padding_level2')), start: LengthMetrics.resource($r('sys.float.padding_level4')), end: LengthMetrics.resource($r('sys.float.padding_level4'))}
itemShadowShadowOptions |ShadowStyleYesYes@ParamShadow of the item of the MultiCapsuleSegmentButtonV2 component.
Default value: ShadowStyle.OUTER_DEFAULT_XS
itemSpaceLengthMetricsYesYes@ParamSpace between items of the MultiCapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: LengthMetrics.vp(1)
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemMinFontScalenumber |ResourceYesYes@ParamMinimum font scale for the item of the MultiCapsuleSegmentButtonV2 component.
Value range: [0, 1]
NOTE
A value less than 0 is handled as 0. A value greater than 1 is handled as 1. Abnormal values are ineffective by default.
itemMaxFontScalenumber |ResourceYesYes@ParamMaximum font scale for the item of the MultiCapsuleSegmentButtonV2 component.
Value range: [1, 2]
Default value: 1
NOTE
A value less than 1 is handled as 1. A value greater than 2 is handled as 2. Abnormal values are ineffective by default.
itemSelectedFontSizeLengthMetricsYesYes@ParamFont size of the selected item of the MultiCapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: 14fp
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemFontColorColorMetricsYesYes@ParamFont color of the unselected item of the MultiCapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_secondary')
itemSelectedFontColorColorMetricsYesYes@ParamFont color of the selected item of the MultiCapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_on_primary')
itemFontWeightFontWeightYesYes@ParamFont weight of the unselected item of the MultiCapsuleSegmentButtonV2 component.
Default value: FontWeight.Medium
itemSelectedFontWeightFontWeightYesYes@ParamFont weight of the selected item of the MultiCapsuleSegmentButtonV2 component.
Default value: FontWeight.Medium
itemBorderRadiusLengthMetricsYesYes@ParamRounded corner radius of the item of the MultiCapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: $r('sys.float.segment_button_v2_multi_corner_radius')
itemIconSizeSizeT<LengthMetrics>YesYes@ParamSize of the image icon of the item in the MultiCapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: { width: LengthMetrics.vp(24), height: LengthMetrics.vp(24) }
itemIconFillColorColorMetricsYesYes@ParamIcon color of the unselected item of the MultiCapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_secondary')
itemSelectedIconFillColorColorMetricsYesYes@ParamIcon color of the selected item of the MultiCapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_on_primary')
itemSymbolFontSizeLengthMetricsYesYes@ParamSize of the HM Symbol icon of the item in the MultiCapsuleSegmentButtonV2 component.
Value range: [0, +∞)
Default value: 20fp
NOTE
This parameter cannot be set in percentage. If an invalid value is set, the default value is used.
itemSymbolFontColorColorMetricsYesYes@ParamColor of the HM Symbol icon of the unselected items in the MultiCapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_secondary')
itemSelectedSymbolFontColorColorMetricsYesYes@ParamColor of the HM Symbol icon of the selected item in the MultiCapsuleSegmentButtonV2 component.
Default value: $r('sys.color.font_on_primary')
languageDirectionDirectionYesYes@ParamLanguage direction of the MultiCapsuleSegmentButtonV2 component.
Default value: Direction.Auto

SegmentButtonV2Items

Represents items of the SegmentButtonV2 component.

This parameter is inherited from Array<SegmentButtonV2Item>.

Decorator type: @ObservedV2

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Properties

Decorator type: @ObservedV2

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

System capability: SystemCapability.ArkUI.ArkUI.Full

constructor

constructor(items: SegmentButtonV2ItemOptions[])

Constructs a SegmentButtonV2ItemOptions instance.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
itemsArray<SegmentButtonV2ItemOptions>YesOptions of the item of the SegmentButtonV2 component.

isHybrid

get hasHybrid()

Obtains whether there is an item with both an icon and text.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

TypeDescription
booleanWhether there is an item with both an icon and text.
The value true indicates that there is an item with both an icon and text; the value false indicates the opposite.

SegmentButtonV2Item

Properties

Decorator type: @ObservedV2

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeDecoratorMandatoryDescription
textResourceStr@TraceNoItem text of the SegmentButtonV2 component.
iconResourceStr@TraceNoImage icon of the item of the SegmentButtonV2 component.
symbolResource@TraceNoHM symbol icon of the item of the SegmentButtonV2 component.
enabledboolean@TraceNoWhether the item of the SegmentButtonV2 component is enabled.
Default value: true
The value true indicates that the item is enabled; the value false indicates the opposite.
textModifierTextModifier@TraceNoText modifier for the item of the SegmentButtonV2 component.
iconModifierImageModifier@TraceNoImage icon modifier for the item of the SegmentButtonV2 component.
symbolModifierSymbolGlyphModifier@TraceNoHM Symbol icon modifier for the item of the SegmentButtonV2 component.
accessibilityTextResourceStr@TraceNoAccessibility text of the item of the SegmentButtonV2 component.
accessibilityDescriptionResourceStr@TraceNoAccessibility description of the item of the SegmentButtonV2 component.
accessibilityLevelstring@TraceNoAccessibility level of the SegmentButtonV2 component.

Description

  1. When both symbol and icon are set, symbol has a higher display priority.
  2. When both symbol and symbolModifier are set with HM Symbol, symbolModifier has a higher display priority.

constructor

constructor(options: SegmentButtonV2ItemOptions)

Constructs a SegmentButtonV2ItemOptions instance.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
optionsSegmentButtonV2ItemOptionsYesOptions of the item of the SegmentButtonV2 component.

isHybrid

get isHybrid()

Obtains whether the item is set with both an icon and text.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

TypeDescription
booleanWhether the item is set with both an icon and text.

SegmentButtonV2ItemOptions

Represents options of the item of the SegmentButtonV2 component.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Properties

NameTypeMandatoryDescription
textResourceStrNoItem text of the SegmentButtonV2 component.
iconResourceStrNoImage icon of the item of the SegmentButtonV2 component.
symbolResourceNoHM symbol icon of the item of the SegmentButtonV2 component.
enabledbooleanNoWhether the item of the SegmentButtonV2 component is enabled.
textModifierTextModifierNoText modifier for the item of the SegmentButtonV2 component.
iconModifierImageModifierNoImage icon modifier for the item of the SegmentButtonV2 component.
symbolModifierSymbolGlyphModifierNoHM Symbol icon modifier for the item of the SegmentButtonV2 component.
accessibilityTextResourceStrNoAccessibility text of the item of the SegmentButtonV2 component.
accessibilityDescriptionResourceStrNoAccessibility description of the item of the SegmentButtonV2 component.
accessibilityLevelstringNoAccessibility level of the SegmentButtonV2 component.

Description

  1. When both symbol and icon are set, symbol has a higher display priority.
  2. When both symbol and symbolModifier are set with HM Symbol, symbolModifier has a higher display priority.

OnSelectedIndexChange

type OnSelectedIndexChange = (selectedIndex: number) => void

Defines a callback invoked when the selected item of the SegmentButtonV2 component changes.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
selectedIndexnumberYesIndex of the selected item of the SegmentButtonV2 component.

OnSelectedIndexesChange

type OnSelectedIndexesChange = (selectedIndexes: number[]) => void

Defines a callback invoked when selected items of the SegmentButtonV2 component changes.

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

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
selectedIndexesnumber[]YesIndexes of selected items of the SegmentButtonV2 component.

Example

Example 1 (TabSegmentButtonV2)

The following example describes how to use the TabSegmentButtonV2 component.

import { SegmentButtonV2Items, TabSegmentButtonV2 } from '@kit.ArkUI';

@Entry
@ComponentV2
struct TabSegmentButtonV2Example {
  @Local textItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { text: 'Phone'},
    { text: 'Tablet' },
    { text: '2-in-1' }, 
    { text: 'Wearable' },
  ]);
  @Local textSelectedIndex: number = 0;
  @Local imageItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { icon: $r('sys.media.ohos_ic_public_device_phone') },
    { icon: $r('sys.media.ohos_ic_public_device_pad') },
    { icon: $r('sys.media.ohos_ic_public_device_matebook') },
    { icon: $r('sys.media.ohos_ic_public_device_watch') },
  ]);
  @Local imageSelectedIndex: number = 0;
  @Local symbolItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { symbol: $r('sys.symbol.phone') },
    { symbol: $r('sys.symbol.pad') },
    { symbol: $r('sys.symbol.matebook') },
    { symbol: $r('sys.symbol.watch') },
  ]);
  @Local symbolSelectedIndex: number = 0;
  @Local hybridItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { text: 'Phone', symbol: $r('sys.symbol.phone') },
    { text: 'Tablet', symbol: $r('sys.symbol.pad') },
    { text: '2-in-1', symbol: $r('sys.symbol.matebook') },
    { text: 'Wearable', symbol: $r('sys.symbol.watch') },
  ]);
  @Local hybridSelectedIndex: number = 0;
  @Local freeItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { text: 'Year' },
    { text: 'Month' },
    { text: 'Week' },
    { text: 'Day' },
    { icon: $r('sys.media.ohos_ic_public_search_filled') },
  ]);
  @Local freeSelectedIndex: number = 0;

  build() {
    Scroll() {
      Column({ space: 12 }) {
        VCard({ title: 'Text Button' }) {
          TabSegmentButtonV2({
            items: this.textItems,
            selectedIndex: this.textSelectedIndex!!,
          })
        }

        VCard({ title: 'Image Button' }) {
          TabSegmentButtonV2({
            items: this.imageItems,
            selectedIndex: this.imageSelectedIndex!!,
          })
        }

        VCard({ title: 'Symbol Button' }) {
          TabSegmentButtonV2({
            items: this.symbolItems,
            selectedIndex: this.symbolSelectedIndex!!,
          })
        }

        VCard({ title: 'Text and Icon Button' }) {
          TabSegmentButtonV2({
            items: this.hybridItems,
            selectedIndex: this.hybridSelectedIndex!!,
          })
        }

        VCard({ title: 'Custom Button' }) {
          TabSegmentButtonV2({
            items: this.freeItems,
            selectedIndex: this.freeSelectedIndex!!,
          })
        }
      }
      .constraintSize({ minHeight: '100%' })
      .justifyContent(FlexAlign.Start)
      .padding(16)
    }
    .backgroundColor('#f1f3f5')
    .width('100%')
    .height('100%')
  }
}

@Builder
function Noop() {
}

@Component
export struct VCard {
  @Prop
  title: ResourceStr;
  @BuilderParam
  content: () => void = Noop;

  build() {
    Column({ space: 8 }) {
      if (this.title) {
        Text(this.title)
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .constraintSize({ maxWidth: '80%' })
      }
      this.content()
    }
    .backgroundColor(Color.White)
    .borderRadius(8)
    .padding(8)
    .width('100%')
  }
}

TabSegmentButtonV2

Example 2 (CapsuleSegmentButtonV2)

The following example describes how to use the CapsuleSegmentButtonV2 button.

import { CapsuleSegmentButtonV2, SegmentButtonV2Items } from '@kit.ArkUI';

@Entry
@ComponentV2
struct CapsuleSegmentButtonV2Example {
  @Local textItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { text: 'Phone'},
    { text: 'Tablet'},
    { text: '2-in-1' }, 
    { text: 'Wearable' },
  ]);
  @Local textSelectedIndex: number = 0;
  @Local imageItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { icon: $r('sys.media.ohos_ic_public_device_phone') },
    { icon: $r('sys.media.ohos_ic_public_device_pad') },
    { icon: $r('sys.media.ohos_ic_public_device_matebook') },
    { icon: $r('sys.media.ohos_ic_public_device_watch') },
  ]);
  @Local imageSelectedIndex: number = 0;
  @Local symbolItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { symbol: $r('sys.symbol.phone') },
    { symbol: $r('sys.symbol.pad') },
    { symbol: $r('sys.symbol.matebook') },
    { symbol: $r('sys.symbol.watch') },
  ]);
  @Local symbolSelectedIndex: number = 0;
  @Local hybridItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { text: 'Phone', symbol: $r('sys.symbol.phone') },
    { text: 'Tablet', symbol: $r('sys.symbol.pad') },
    { text: '2-in-1', symbol: $r('sys.symbol.matebook') },
    { text: 'Wearable', symbol: $r('sys.symbol.watch') },
  ]);
  @Local hybridSelectedIndex: number = 0;
  @Local freeItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { text: 'Year' },
    { text: 'Month' },
    { text: 'Week' },
    { text: 'Day' },
    { icon: $r('sys.media.ohos_ic_public_search_filled') },
  ]);
  @Local freeSelectedIndex: number = 0;

  build() {
    Scroll() {
      Column({ space: 12 }) {
        VCard({ title: 'Text Button' }) {
          CapsuleSegmentButtonV2({
            items: this.textItems,
            selectedIndex: this.textSelectedIndex!!,
          })
        }

        VCard({ title: 'Image Button' }) {
          CapsuleSegmentButtonV2({
            items: this.imageItems,
            selectedIndex: this.imageSelectedIndex!!,
          })
        }

        VCard({ title: 'Symbol Button' }) {
          CapsuleSegmentButtonV2({
            items: this.symbolItems,
            selectedIndex: this.symbolSelectedIndex!!,
          })
        }

        VCard({ title: 'Text and Icon Button' }) {
          CapsuleSegmentButtonV2({
            items: this.hybridItems,
            selectedIndex: this.hybridSelectedIndex!!,
          })
        }

        VCard({ title: 'Custom Button' }) {
          CapsuleSegmentButtonV2({
            items: this.freeItems,
            selectedIndex: this.freeSelectedIndex!!,
          })
        }
      }
      .constraintSize({ minHeight: '100%' })
      .justifyContent(FlexAlign.Start)
      .padding(16)
    }
    .backgroundColor('#f1f3f5')
    .width('100%')
    .height('100%')
  }
}

@Builder
function Noop() {
}

@Component
export struct VCard {
  @Prop
  title: ResourceStr;
  @BuilderParam
  content: () => void = Noop;

  build() {
    Column({ space: 8 }) {
      if (this.title) {
        Text(this.title)
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .constraintSize({ maxWidth: '80%' })
      }
      this.content()
    }
    .backgroundColor(Color.White)
    .borderRadius(8)
    .padding(8)
    .width('100%')
  }
}

CapsuleSegmentButtonV2

Example 3 (MultiCapsuleSegmentButtonV2)

The following example describes how to use the MultiCapsuleSegmentButtonV2 button.

import { MultiCapsuleSegmentButtonV2, SegmentButtonV2Items } from '@kit.ArkUI';

@Entry
@ComponentV2
struct MultiCapsuleSegmentButtonV2Example {
  @Local textItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { text: 'Phone'},
    { text: 'Tablet'},
    { text: '2-in-1' }, 
    { text: 'Wearable' },
  ]);
  @Local textSelectedIndexes: number[] = [0];
  @Local imageItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { icon: $r('sys.media.ohos_ic_public_device_phone') },
    { icon: $r('sys.media.ohos_ic_public_device_pad') },
    { icon: $r('sys.media.ohos_ic_public_device_matebook') },
    { icon: $r('sys.media.ohos_ic_public_device_watch') },
  ]);
  @Local imageSelectedIndexes: number[] = [0];
  @Local symbolItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { symbol: $r('sys.symbol.phone') },
    { symbol: $r('sys.symbol.pad') },
    { symbol: $r('sys.symbol.matebook') },
    { symbol: $r('sys.symbol.watch') },
  ]);
  @Local symbolSelectedIndexes: number[] = [0];
  @Local hybridItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { text: 'Phone', symbol: $r('sys.symbol.phone') },
    { text: 'Tablet', symbol: $r('sys.symbol.pad') },
    { text: '2-in-1', symbol: $r('sys.symbol.matebook') },
    { text: 'Wearable', symbol: $r('sys.symbol.watch') },
  ]);
  @Local hybridSelectedIndexes: number[] = [0];
  @Local freeItems: SegmentButtonV2Items = new SegmentButtonV2Items([
    { text: 'Year' },
    { text: 'Month' },
    { text: 'Week' },
    { text: 'Day' },
    { icon: $r('sys.media.ohos_ic_public_search_filled') },
  ]);
  @Local freeSelectedIndexes: number[] = [0];

  build() {
    Scroll() {
      Column({ space: 12 }) {
        VCard({ title: 'Text Button' }) {
          MultiCapsuleSegmentButtonV2({
            items: this.textItems,
            selectedIndexes: this.textSelectedIndexes!!,
          })
        }

        VCard({ title: 'Image Button' }) {
          MultiCapsuleSegmentButtonV2({
            items: this.imageItems,
            selectedIndexes: this.imageSelectedIndexes!!,
          })
        }

        VCard({ title: 'Symbol Button' }) {
          MultiCapsuleSegmentButtonV2({
            items: this.symbolItems,
            selectedIndexes: this.symbolSelectedIndexes!!,
          })
        }

        VCard({ title: 'Text and Icon Button' }) {
          MultiCapsuleSegmentButtonV2({
            items: this.hybridItems,
            selectedIndexes: this.hybridSelectedIndexes!!,
          })
        }

        VCard({ title: 'Custom Button' }) {
          MultiCapsuleSegmentButtonV2({
            items: this.freeItems,
            selectedIndexes: this.freeSelectedIndexes!!,
          })
        }
      }
      .constraintSize({ minHeight: '100%' })
      .justifyContent(FlexAlign.Start)
      .padding(16)
    }
    .backgroundColor('#f1f3f5')
    .width('100%')
    .height('100%')
  }
}

@Builder
function Noop() {
}

@Component
export struct VCard {
  @Prop
  title: ResourceStr;
  @BuilderParam
  content: () => void = Noop;

  build() {
    Column({ space: 8 }) {
      if (this.title) {
        Text(this.title)
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .constraintSize({ maxWidth: '80%' })
      }
      this.content()
    }
    .backgroundColor(Color.White)
    .borderRadius(8)
    .padding(8)
    .width('100%')
  }
}

MultiCapsuleSegmentButtonV2.gif

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArcButton

harmony 鸿蒙ArcSlider

harmony 鸿蒙Chip

harmony 鸿蒙ChipGroup

harmony 鸿蒙ComposeListItem

harmony 鸿蒙ComposeTitleBar

harmony 鸿蒙advanced.Counter

harmony 鸿蒙Dialog Box (Dialog)

harmony 鸿蒙DialogV2

harmony 鸿蒙DownloadFileButton

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