1e41f4b71Sopenharmony_ci# Select 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci提供下拉选择菜单,可以让用户在多个选项之间选择。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## 子组件 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci无 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## 接口 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciSelect(options: Array\<[SelectOption](#selectoption对象说明)\>) 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**参数:** 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 24e41f4b71Sopenharmony_ci| ------- | ---------------------------------------------- | ---- | -------------- | 25e41f4b71Sopenharmony_ci| options | Array\<[SelectOption](#selectoption对象说明)\> | 是 | 设置下拉选项。 | 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci## SelectOption对象说明 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 32e41f4b71Sopenharmony_ci| ------ | ----------------------------------- | ---- | -------------- | 33e41f4b71Sopenharmony_ci| value | [ResourceStr](ts-types.md#resourcestr) | 是 | 下拉选项内容。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 34e41f4b71Sopenharmony_ci| icon | [ResourceStr](ts-types.md#resourcestr) | 否 | 下拉选项图片。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 35e41f4b71Sopenharmony_ci| symbolIcon<sup>12+</sup> | [SymbolGlyphModifier](ts-universal-attributes-attribute-modifier.md) | 否 | 下拉选项Symbol图片。<br/>symbolIcon优先级高于icon。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci## 属性 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性: 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci### selected 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ciselected(value: number | Resource) 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci设置下拉菜单初始选项的索引,第一项的索引为0。当不设置selected属性或设置异常值时,默认选择值为-1,菜单项不选中;当设置为undefined、null时,选中第一项。 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci从API version 10开始,该属性支持[$$](../../../quick-start/arkts-two-way-sync.md)双向绑定变量。 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**参数:** 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 56e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | ------------------------ | 57e41f4b71Sopenharmony_ci| value | number \| [Resource](ts-types.md#resource)<sup>11+</sup> | 是 | 下拉菜单初始选项的索引。 | 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci### value 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_civalue(value: ResourceStr) 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci设置下拉按钮本身的文本内容。当菜单选中时默认会替换为菜单项文本内容。 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci从API version 10开始,该参数支持[$$](../../../quick-start/arkts-two-way-sync.md)双向绑定变量。 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci**参数:** 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 74e41f4b71Sopenharmony_ci| ------ | ---------------------------------------------------- | ---- | ------------------------ | 75e41f4b71Sopenharmony_ci| value | [ResourceStr](ts-types.md#resourcestr)<sup>11+</sup> | 是 | 下拉按钮本身的文本内容。 | 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci### controlSize<sup>12+</sup> 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_cicontrolSize(value: ControlSize) 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci设置Select组件的尺寸。 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci**参数:** 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 90e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ------------------------------------------------ | 91e41f4b71Sopenharmony_ci| value | [ControlSize](ts-basic-components-button.md#controlsize11枚举说明)<sup>11+</sup> | 是 | Select组件的尺寸。<br/>默认值:ControlSize.NORMAL | 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_cicontrolSize、width、height接口作用优先级: 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci 1)如果开发者只设置了width和height,当文字大小设置的是比较大的值的时候,文字超出组件大小,且以省略号方式显示; 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci 2)如果开发者只设置了controlSize,没有设置width和height,组件宽高自适应文字,文字不超出组件,并设置最小宽度minWidth和最小高度minHeight; 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci 3)如果controlSize、width、height接口都设置了,width和height设置的值生效,但如果width和height设置的值小于controlSize设置的最小宽度minWidth和最小高度minHeight,width和height设置的值不生效,宽高仍保持controlSize设置的最小宽度minWidth和最小高度minHeight。 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci### menuItemContentModifier<sup>12+</sup> 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_cimenuItemContentModifier(modifier: ContentModifier\<MenuItemConfiguration>) 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci定制Select下拉菜单项内容区的方法。 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci**参数:** 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 114e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ------------------------------------------------ | 115e41f4b71Sopenharmony_ci| modifier | [ContentModifier\<MenuItemConfiguration>](#menuitemconfiguration12对象说明) | 是 | 在Select组件上,定制下拉菜单项内容区的方法。<br/>modifier: 内容修改器,开发者需要自定义class实现ContentModifier接口。 | 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci### divider<sup>12+</sup> 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_cidivider(options: Optional\<DividerOptions> | null) 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci设置分割线样式,不设置该属性则按“默认值”展示分割线。 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**参数:** 128e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 129e41f4b71Sopenharmony_ci| ------ | ------- | ---- | --------------------------------------------------------------------- | 130e41f4b71Sopenharmony_ci| options | Optional\<[DividerOptions](ts-basic-components-textpicker.md#divideroptions12对象说明)> \| null | 是 | 1.设置DividerOptions,则按设置的样式显示分割线。<br/>默认值:<br/>{<br/>strokeWidth: '1px' , <br/>color: '#33182431'<br/>}<br/>2.设置为null时,不显示分割线。<br/>3.strokeWidth设置过宽时,会覆盖文字。分割线会从每一个Item底部开始,同时向上向下画分割线。<br/>4.startMargin和endMargin的默认值与不设置divider属性时的分割线样式保持一致。startMargin和endMargin的和与optionWidth的值相等时,不显示分割线。 startMargin和endMargin的和超过optionWidth的值时,按照默认样式显示分割线。| 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci### font 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_cifont(value: Font) 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci设置下拉按钮本身的文本样式。当size为0的时候,文本不显示,当size为负值的时候,文本的size按照默认值显示。 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci**参数:** 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 145e41f4b71Sopenharmony_ci| ------ | ------------------------ | ---- | ------------------------------------------------------------ | 146e41f4b71Sopenharmony_ci| value | [Font](ts-types.md#font) | 是 | 下拉按钮本身的文本样式。<br/>API Version 11及以前默认值:<br/>{<br/>size: $r('sys.float.ohos_id_text_size_button1'),<br/>weight: FontWeight.Medium<br/>} <br/>API Version 12以后,如果设置controlSize的值为:controlSize.SMALL,size默认值是`$r('sys.float.ohos_id_text_size_button2')`,否则为`$r('sys.float.ohos_id_text_size_button1')`。 | 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci### fontColor 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_cifontColor(value: ResourceColor) 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci设置下拉按钮本身的文本颜色。 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci**参数:** 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 161e41f4b71Sopenharmony_ci| ------ | ------------------------------------------ | ---- | ------------------------------------------------------------ | 162e41f4b71Sopenharmony_ci| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 下拉按钮本身的文本颜色。<br/>默认值:`$r('sys.color.ohos_id_color_text_primary')`混合`$r('sys.color.ohos_id_alpha_content_primary')`的透明度。 | 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci### selectedOptionBgColor 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ciselectedOptionBgColor(value: ResourceColor) 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci设置下拉菜单选中项的背景色。 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci**参数:** 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 177e41f4b71Sopenharmony_ci| ------ | ------------------------------------------ | ---- | ------------------------------------------------------------ | 178e41f4b71Sopenharmony_ci| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 下拉菜单选中项的背景色。<br/>默认值:`$r('sys.color.ohos_id_color_component_activated')`混合`$r('sys.color.ohos_id_alpha_highlight_bg')`的透明度。 | 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci### selectedOptionFont 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ciselectedOptionFont(value: Font) 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci设置下拉菜单选中项的文本样式。当size为0的时候,文本不显示,当size为负值的时候,文本的size按照默认值显示。 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci**参数:** 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 193e41f4b71Sopenharmony_ci| ------ | ------------------------ | ---- | ------------------------------------------------------------ | 194e41f4b71Sopenharmony_ci| value | [Font](ts-types.md#font) | 是 | 下拉菜单选中项的文本样式。<br/>默认值:<br/>{<br/>size: $r('sys.color.ohos_id_text_size_body1'),<br/>weight: FontWeight.Regular<br/>} | 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci### selectedOptionFontColor 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ciselectedOptionFontColor(value: ResourceColor) 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci设置下拉菜单选中项的文本颜色。 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci**参数:** 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 209e41f4b71Sopenharmony_ci| ------ | ------------------------------------------ | ---- | ------------------------------------------------------------ | 210e41f4b71Sopenharmony_ci| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 下拉菜单选中项的文本颜色。<br/>默认值:$r('sys.color.ohos_id_color_text_primary_activated') | 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci### optionBgColor 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_cioptionBgColor(value: ResourceColor) 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci设置下拉菜单项的背景色。 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci**参数:** 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 225e41f4b71Sopenharmony_ci| ------ | ------------------------------------------ | ---- | ------------------------------------------------------------ | 226e41f4b71Sopenharmony_ci| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 下拉菜单项的背景色。<br/>默认值:<br/>API version 11之前,默认值为Color.White。<br/>API version 11及之后,默认值为Color.Transparent。 | 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci### optionFont 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_cioptionFont(value: Font) 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci设置下拉菜单项的文本样式。当size为0的时候,文本不显示,当size为负值的时候,文本的size按照默认值显示。 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci**参数:** 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 241e41f4b71Sopenharmony_ci| ------ | ------------------------ | ---- | ------------------------------------------------------------ | 242e41f4b71Sopenharmony_ci| value | [Font](ts-types.md#font) | 是 | 下拉菜单项的文本样式。<br/>默认值:<br/>{<br/>size: $r('sys.float.ohos_id_text_size_body1'),<br/>weight: FontWeight.Regular<br/>} | 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci### optionFontColor 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_cioptionFontColor(value: ResourceColor) 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci设置下拉菜单项的文本颜色。 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci**参数:** 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 257e41f4b71Sopenharmony_ci| ------ | ------------------------------------------ | ---- | ------------------------------------------------------------ | 258e41f4b71Sopenharmony_ci| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 下拉菜单项的文本颜色。<br/>默认值:$r('sys.color.ohos_id_color_text_primary') | 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci### space<sup>10+</sup> 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_cispace(value: Length) 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci设置下拉菜单项的文本与箭头之间的间距。不支持设置百分比。设置为null、undefined,或者小于等于8的值,取默认值。 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci**参数:** 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 273e41f4b71Sopenharmony_ci| ------ | ---------------------------- | ---- | ------------------------------------------------ | 274e41f4b71Sopenharmony_ci| value | [Length](ts-types.md#length) | 是 | 下拉菜单项的文本与箭头之间的间距。<br/>默认值:8 | 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci### arrowPosition<sup>10+</sup> 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ciarrowPosition(value: ArrowPosition) 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci设置下拉菜单项的文本与箭头之间的对齐方式。 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci**参数:** 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 289e41f4b71Sopenharmony_ci| ------ | ----------------------------------------- | ---- | ------------------------------------------------------------ | 290e41f4b71Sopenharmony_ci| value | [ArrowPosition](#arrowposition10枚举说明) | 是 | 下拉菜单项的文本与箭头之间的对齐方式。<br/>默认值:ArrowPosition.END | 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci### menuAlign<sup>10+</sup> 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_cimenuAlign(alignType: MenuAlignType, offset?: Offset) 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci设置下拉按钮与下拉菜单间的对齐方式。 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci**参数:** 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 305e41f4b71Sopenharmony_ci| --------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 306e41f4b71Sopenharmony_ci| alignType | [MenuAlignType](#menualigntype10枚举说明) | 是 | 对齐方式类型。<br/>默认值:MenuAlignType.START | 307e41f4b71Sopenharmony_ci| offset | [Offset](ts-types.md#offset) | 否 | 按照对齐类型对齐后,下拉菜单相对下拉按钮的偏移量。<br/> 默认值:{dx: 0, dy: 0} | 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci### optionWidth<sup>11+</sup> 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_cioptionWidth(value: Dimension | OptionWidthMode ) 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci设置下拉菜单项的宽度,不支持设置百分比。OptionWidthMode类型为枚举类型,OptionWidthMode决定下拉菜单是否继承下拉按钮宽度。 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci当设置为异常值或小于最小宽度56vp时,属性不生效,菜单项宽度设为默认值,即菜单默认宽度为2栅格。 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci**参数:** 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 324e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | ------------------ | 325e41f4b71Sopenharmony_ci| value | [Dimension](ts-types.md#dimension10) \| [OptionWidthMode](ts-appendix-enums.md#optionwidthmode11) | 是 | 下拉菜单项的宽度。 | 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci### optionHeight<sup>11+</sup> 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_cioptionHeight(value: Dimension) 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ci设置下拉菜单显示的最大高度,不支持设置百分比。下拉菜单的默认最大高度是屏幕可用高度的80%,设置的菜单最大高度不能超过默认最大高度。 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci当设置为异常值或零时,属性不生效,下拉菜单最大高度设为默认值,即下拉菜单最大高度默认值为屏幕可用高度的80%。 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci如果下拉菜单所有选项的实际高度没有设定的高度大,下拉菜单的高度按实际高度显示。 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci**参数:** 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 344e41f4b71Sopenharmony_ci| ------ | ------------------------------------ | ---- | ------------------------ | 345e41f4b71Sopenharmony_ci| value | [Dimension](ts-types.md#dimension10) | 是 | 下拉菜单显示的最大高度。 | 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci### menuBackgroundColor<sup>11+</sup> 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_cimenuBackgroundColor(value: ResourceColor) 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci设置下拉菜单的背景色。 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci**参数:** 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 360e41f4b71Sopenharmony_ci| ------ | ------------------------------------------ | ---- | ------------------------------------------------------------ | 361e41f4b71Sopenharmony_ci| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 下拉菜单的背景色。<br/>默认值:<br/>API version 11之前,默认值为$r('sys.color.ohos_id_color_card_bg')。<br/>API version 11及之后,默认值为Color.Transparent。 | 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci### menuBackgroundBlurStyle<sup>11+</sup> 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_cimenuBackgroundBlurStyle(value: BlurStyle) 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci设置下拉菜单的背景模糊材质。 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ci**参数:** 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 376e41f4b71Sopenharmony_ci| ------ | -------------------------------------------- | ---- | ------------------------------------------------------------ | 377e41f4b71Sopenharmony_ci| value | [BlurStyle](ts-universal-attributes-background.md#blurstyle9) | 是 | 下拉菜单的背景模糊材质。<br/>默认值:BlurStyle.COMPONENT_ULTRA_THICK | 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ci## ArrowPosition<sup>10+</sup>枚举说明 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci| 名称 | 说明 | 386e41f4b71Sopenharmony_ci| ------------------- | ------------------ | 387e41f4b71Sopenharmony_ci| END<sup>10+</sup> | 文字在前,箭头在后。 | 388e41f4b71Sopenharmony_ci| START<sup>10+</sup> | 箭头在前,文字在后。 | 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ci## MenuAlignType<sup>10+</sup>枚举说明 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ci| 名称 | 说明 | 397e41f4b71Sopenharmony_ci| ------------------- | ------------------ | 398e41f4b71Sopenharmony_ci| START | 按照语言方向起始端对齐。 | 399e41f4b71Sopenharmony_ci| CENTER | 居中对齐。 | 400e41f4b71Sopenharmony_ci| END | 按照语言方向末端对齐。 | 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci## MenuItemConfiguration<sup>12+</sup>对象说明 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 409e41f4b71Sopenharmony_ci| ------ | -------------------------------------------- | ---- | ------------------------------------------------------------ | 410e41f4b71Sopenharmony_ci| value | [ResourceStr](ts-types.md#resourcestr) | 是 | 下拉菜单项的文本内容。 | 411e41f4b71Sopenharmony_ci| icon | [ResourceStr](ts-types.md#resourcestr) | 否 | 下拉菜单项的图片内容。 | 412e41f4b71Sopenharmony_ci| symbolIcon<sup>12+</sup> | [SymbolGlyphModifier](ts-universal-attributes-attribute-modifier.md) | 否 | 下拉选项Symbol图片内容。| 413e41f4b71Sopenharmony_ci| selected | boolean | 是 | 下拉菜单项是否被选中。<br/>默认值:false | 414e41f4b71Sopenharmony_ci| index | number | 是 | 下拉菜单项的索引。 | 415e41f4b71Sopenharmony_ci| triggerSelect | (index: number, value: string) :void | 是 | 下拉菜单选中某一项的回调函数。<br/>index: 选中菜单项的索引。<br/>value: 选中菜单项的文本。<br/>说明: index会赋值给事件[onSelect](#onselect)回调中的索引参数; value会返回给Select组件显示,同时会赋值给事件[onSelect](#onselect)回调中的文本参数。 | 416e41f4b71Sopenharmony_ci 417e41f4b71Sopenharmony_ci## 事件 418e41f4b71Sopenharmony_ci 419e41f4b71Sopenharmony_ci### onSelect 420e41f4b71Sopenharmony_ci 421e41f4b71Sopenharmony_cionSelect(callback: (index: number, value: string) => void) 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_ci下拉菜单选中某一项的回调。 424e41f4b71Sopenharmony_ci 425e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 426e41f4b71Sopenharmony_ci 427e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 428e41f4b71Sopenharmony_ci 429e41f4b71Sopenharmony_ci**参数:** 430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 432e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------- | 433e41f4b71Sopenharmony_ci| index | number | 是 | 选中项的索引。 | 434e41f4b71Sopenharmony_ci| value | string | 是 | 选中项的值。 | 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci## 示例1 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci```ts 439e41f4b71Sopenharmony_ci// xxx.ets 440e41f4b71Sopenharmony_ci@Entry 441e41f4b71Sopenharmony_ci@Component 442e41f4b71Sopenharmony_cistruct SelectExample { 443e41f4b71Sopenharmony_ci @State text: string = "TTTTT" 444e41f4b71Sopenharmony_ci @State index: number = 2 445e41f4b71Sopenharmony_ci @State space: number = 8 446e41f4b71Sopenharmony_ci @State arrowPosition: ArrowPosition = ArrowPosition.END 447e41f4b71Sopenharmony_ci build() { 448e41f4b71Sopenharmony_ci Column() { 449e41f4b71Sopenharmony_ci Select([{ value: 'aaa', icon: $r("app.media.selection") }, 450e41f4b71Sopenharmony_ci { value: 'bbb', icon: $r("app.media.selection") }, 451e41f4b71Sopenharmony_ci { value: 'ccc', icon: $r("app.media.selection") }, 452e41f4b71Sopenharmony_ci { value: 'ddd', icon: $r("app.media.selection") }]) 453e41f4b71Sopenharmony_ci .selected(this.index) 454e41f4b71Sopenharmony_ci .value(this.text) 455e41f4b71Sopenharmony_ci .font({ size: 16, weight: 500 }) 456e41f4b71Sopenharmony_ci .fontColor('#182431') 457e41f4b71Sopenharmony_ci .selectedOptionFont({ size: 16, weight: 400 }) 458e41f4b71Sopenharmony_ci .optionFont({ size: 16, weight: 400 }) 459e41f4b71Sopenharmony_ci .space(this.space) 460e41f4b71Sopenharmony_ci .arrowPosition(this.arrowPosition) 461e41f4b71Sopenharmony_ci .menuAlign(MenuAlignType.START, {dx:0, dy:0}) 462e41f4b71Sopenharmony_ci .optionWidth(200) 463e41f4b71Sopenharmony_ci .optionHeight(300) 464e41f4b71Sopenharmony_ci .onSelect((index:number, text?: string | undefined)=>{ 465e41f4b71Sopenharmony_ci console.info('Select:' + index) 466e41f4b71Sopenharmony_ci this.index = index; 467e41f4b71Sopenharmony_ci if(text){ 468e41f4b71Sopenharmony_ci this.text = text; 469e41f4b71Sopenharmony_ci } 470e41f4b71Sopenharmony_ci }) 471e41f4b71Sopenharmony_ci }.width('100%') 472e41f4b71Sopenharmony_ci } 473e41f4b71Sopenharmony_ci} 474e41f4b71Sopenharmony_ci``` 475e41f4b71Sopenharmony_ci 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci 478e41f4b71Sopenharmony_ci## 示例2 479e41f4b71Sopenharmony_ci该示例实现了一个自定义下拉菜选项的Select组件。自定义下拉菜单选项样式为“文本 + 图片 + 空白间隔 + 文本 + 绘制三角形”,点击菜单选项后Select组件显示菜单选项的文本内容。 480e41f4b71Sopenharmony_ci 481e41f4b71Sopenharmony_ci```ts 482e41f4b71Sopenharmony_ciimport { MenuItemModifier } from '@kit.ArkUI' 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ciclass MyMenuItemContentModifier implements ContentModifier<MenuItemConfiguration> { 485e41f4b71Sopenharmony_ci modifierText: string = "" 486e41f4b71Sopenharmony_ci constructor(text: string) { 487e41f4b71Sopenharmony_ci this.modifierText = text; 488e41f4b71Sopenharmony_ci } 489e41f4b71Sopenharmony_ci applyContent(): WrappedBuilder<[MenuItemConfiguration]> { 490e41f4b71Sopenharmony_ci return wrapBuilder(MenuItemBuilder) 491e41f4b71Sopenharmony_ci } 492e41f4b71Sopenharmony_ci} 493e41f4b71Sopenharmony_ci 494e41f4b71Sopenharmony_ci@Builder 495e41f4b71Sopenharmony_cifunction MenuItemBuilder(configuration: MenuItemConfiguration) { 496e41f4b71Sopenharmony_ci Row() { 497e41f4b71Sopenharmony_ci Text(configuration.value) 498e41f4b71Sopenharmony_ci Blank() 499e41f4b71Sopenharmony_ci Image(configuration.icon).size({ width: 40, height: 40 }) 500e41f4b71Sopenharmony_ci Blank(30) 501e41f4b71Sopenharmony_ci Text((configuration.contentModifier as MyMenuItemContentModifier).modifierText) 502e41f4b71Sopenharmony_ci Path() 503e41f4b71Sopenharmony_ci .width('100px') 504e41f4b71Sopenharmony_ci .height('150px') 505e41f4b71Sopenharmony_ci .commands('M40 0 L80 100 L0 100 Z') 506e41f4b71Sopenharmony_ci .fillOpacity(0) 507e41f4b71Sopenharmony_ci .stroke(Color.Black) 508e41f4b71Sopenharmony_ci .strokeWidth(3) 509e41f4b71Sopenharmony_ci } 510e41f4b71Sopenharmony_ci .onClick(() => { 511e41f4b71Sopenharmony_ci configuration.triggerSelect(configuration.index, configuration.value.valueOf().toString()) 512e41f4b71Sopenharmony_ci }) 513e41f4b71Sopenharmony_ci} 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci@Entry 516e41f4b71Sopenharmony_ci@Component 517e41f4b71Sopenharmony_cistruct SelectExample { 518e41f4b71Sopenharmony_ci @State text: string = "有modifier" 519e41f4b71Sopenharmony_ci build() { 520e41f4b71Sopenharmony_ci Column() { 521e41f4b71Sopenharmony_ci Row() { 522e41f4b71Sopenharmony_ci Select([{ value: 'item1', icon: $r("app.media.icon") }, 523e41f4b71Sopenharmony_ci { value: 'item2', icon: $r("app.media.icon") }]) 524e41f4b71Sopenharmony_ci .value(this.text) 525e41f4b71Sopenharmony_ci .onSelect((index:number, text?: string)=>{ 526e41f4b71Sopenharmony_ci console.info('Select index:' + index) 527e41f4b71Sopenharmony_ci console.info('Select text:' + text) 528e41f4b71Sopenharmony_ci }) 529e41f4b71Sopenharmony_ci .menuItemContentModifier(new MyMenuItemContentModifier("我来自Modifier")) 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ci }.alignItems(VerticalAlign.Center).height("50%") 532e41f4b71Sopenharmony_ci } 533e41f4b71Sopenharmony_ci } 534e41f4b71Sopenharmony_ci} 535e41f4b71Sopenharmony_ci``` 536e41f4b71Sopenharmony_ci 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ci## 示例3 539e41f4b71Sopenharmony_ci该示例实现了一个下拉菜单中图片为Symbol的Select组件。 540e41f4b71Sopenharmony_ci 541e41f4b71Sopenharmony_ci```ts 542e41f4b71Sopenharmony_ci// xxx.ets 543e41f4b71Sopenharmony_ciimport { SymbolGlyphModifier } from '@kit.ArkUI' 544e41f4b71Sopenharmony_ci 545e41f4b71Sopenharmony_ci@Entry 546e41f4b71Sopenharmony_ci@Component 547e41f4b71Sopenharmony_cistruct SelectExample { 548e41f4b71Sopenharmony_ci @State text: string = "TTTTT" 549e41f4b71Sopenharmony_ci @State index: number = 2 550e41f4b71Sopenharmony_ci @State space: number = 8 551e41f4b71Sopenharmony_ci @State arrowPosition: ArrowPosition = ArrowPosition.END 552e41f4b71Sopenharmony_ci @State symbolModifier1: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.ohos_wifi')).fontColor([Color.Green]); 553e41f4b71Sopenharmony_ci @State symbolModifier2: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.ohos_star')).fontColor([Color.Red]); 554e41f4b71Sopenharmony_ci @State symbolModifier3: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.ohos_trash')).fontColor([Color.Gray]); 555e41f4b71Sopenharmony_ci @State symbolModifier4: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.exposure')).fontColor([Color.Gray]); 556e41f4b71Sopenharmony_ci build() { 557e41f4b71Sopenharmony_ci Column() { 558e41f4b71Sopenharmony_ci Select([{ value: 'aaa', symbolIcon: this.symbolModifier1 }, 559e41f4b71Sopenharmony_ci { value: 'bbb', symbolIcon: this.symbolModifier2 }, 560e41f4b71Sopenharmony_ci { value: 'ccc', symbolIcon: this.symbolModifier3 }, 561e41f4b71Sopenharmony_ci { value: 'ddd', symbolIcon: this.symbolModifier4 }]) 562e41f4b71Sopenharmony_ci .selected(this.index) 563e41f4b71Sopenharmony_ci .value(this.text) 564e41f4b71Sopenharmony_ci .font({ size: 16, weight: 500 }) 565e41f4b71Sopenharmony_ci .fontColor('#182431') 566e41f4b71Sopenharmony_ci .selectedOptionFont({ size: 16, weight: 400 }) 567e41f4b71Sopenharmony_ci .optionFont({ size: 16, weight: 400 }) 568e41f4b71Sopenharmony_ci .space(this.space) 569e41f4b71Sopenharmony_ci .arrowPosition(this.arrowPosition) 570e41f4b71Sopenharmony_ci .menuAlign(MenuAlignType.START, {dx:0, dy:0}) 571e41f4b71Sopenharmony_ci .onSelect((index:number, text?: string | undefined)=>{ 572e41f4b71Sopenharmony_ci console.info('Select:' + index) 573e41f4b71Sopenharmony_ci this.index = index; 574e41f4b71Sopenharmony_ci if(text){ 575e41f4b71Sopenharmony_ci this.text = text; 576e41f4b71Sopenharmony_ci } 577e41f4b71Sopenharmony_ci }) 578e41f4b71Sopenharmony_ci }.width('100%') 579e41f4b71Sopenharmony_ci } 580e41f4b71Sopenharmony_ci} 581e41f4b71Sopenharmony_ci``` 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci 584e41f4b71Sopenharmony_ci 585e41f4b71Sopenharmony_ci## 示例4 586e41f4b71Sopenharmony_ci该示例实现了一个自定义下拉菜选项的Select组件。自定义下拉菜单选项样式为“文本 + Symbol图片 + 空白间隔 + 文本 + 绘制三角形”,点击菜单选项后Select组件显示菜单选项的文本内容。 587e41f4b71Sopenharmony_ci 588e41f4b71Sopenharmony_ci```ts 589e41f4b71Sopenharmony_ciimport { MenuItemModifier, SymbolGlyphModifier } from '@kit.ArkUI' 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ciclass MyMenuItemContentModifier implements ContentModifier<MenuItemConfiguration> { 592e41f4b71Sopenharmony_ci modifierText: string = "" 593e41f4b71Sopenharmony_ci constructor(text: string) { 594e41f4b71Sopenharmony_ci this.modifierText = text; 595e41f4b71Sopenharmony_ci } 596e41f4b71Sopenharmony_ci applyContent(): WrappedBuilder<[MenuItemConfiguration]> { 597e41f4b71Sopenharmony_ci return wrapBuilder(MenuItemBuilder) 598e41f4b71Sopenharmony_ci } 599e41f4b71Sopenharmony_ci} 600e41f4b71Sopenharmony_ci 601e41f4b71Sopenharmony_ci@Builder 602e41f4b71Sopenharmony_cifunction MenuItemBuilder(configuration: MenuItemConfiguration) { 603e41f4b71Sopenharmony_ci Row() { 604e41f4b71Sopenharmony_ci Text(configuration.value) 605e41f4b71Sopenharmony_ci Blank() 606e41f4b71Sopenharmony_ci if (configuration.symbolIcon) { 607e41f4b71Sopenharmony_ci SymbolGlyph().attributeModifier(configuration.symbolIcon).fontSize(24) 608e41f4b71Sopenharmony_ci } else if (configuration.icon) { 609e41f4b71Sopenharmony_ci Image(configuration.icon).size({ width: 24, height: 24 }) 610e41f4b71Sopenharmony_ci } 611e41f4b71Sopenharmony_ci Blank(30) 612e41f4b71Sopenharmony_ci Text((configuration.contentModifier as MyMenuItemContentModifier).modifierText) 613e41f4b71Sopenharmony_ci Blank(30) 614e41f4b71Sopenharmony_ci Path() 615e41f4b71Sopenharmony_ci .width('100px') 616e41f4b71Sopenharmony_ci .height('150px') 617e41f4b71Sopenharmony_ci .commands('M40 0 L80 100 L0 100 Z') 618e41f4b71Sopenharmony_ci .fillOpacity(0) 619e41f4b71Sopenharmony_ci .stroke(Color.Black) 620e41f4b71Sopenharmony_ci .strokeWidth(3) 621e41f4b71Sopenharmony_ci } 622e41f4b71Sopenharmony_ci .onClick(() => { 623e41f4b71Sopenharmony_ci configuration.triggerSelect(configuration.index, configuration.value.valueOf().toString()) 624e41f4b71Sopenharmony_ci }) 625e41f4b71Sopenharmony_ci} 626e41f4b71Sopenharmony_ci 627e41f4b71Sopenharmony_ci@Entry 628e41f4b71Sopenharmony_ci@Component 629e41f4b71Sopenharmony_cistruct SelectExample { 630e41f4b71Sopenharmony_ci @State text: string = "Content Modifier Select" 631e41f4b71Sopenharmony_ci @State symbolModifier1: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.ohos_trash')).fontColor([Color.Gray]); 632e41f4b71Sopenharmony_ci @State symbolModifier2: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.exposure')).fontColor([Color.Gray]); 633e41f4b71Sopenharmony_ci build() { 634e41f4b71Sopenharmony_ci Column() { 635e41f4b71Sopenharmony_ci Row() { 636e41f4b71Sopenharmony_ci Select([{ value: 'item1', icon: $r('app.media.icon'), symbolIcon: this.symbolModifier1 }, 637e41f4b71Sopenharmony_ci { value: 'item1', icon: $r('app.media.icon'), symbolIcon: this.symbolModifier2 }]) 638e41f4b71Sopenharmony_ci .value(this.text) 639e41f4b71Sopenharmony_ci .onSelect((index:number, text?: string)=>{ 640e41f4b71Sopenharmony_ci console.info('Select index:' + index) 641e41f4b71Sopenharmony_ci console.info('Select text:' + text) 642e41f4b71Sopenharmony_ci }) 643e41f4b71Sopenharmony_ci .menuItemContentModifier(new MyMenuItemContentModifier("Content Modifier")) 644e41f4b71Sopenharmony_ci 645e41f4b71Sopenharmony_ci }.alignItems(VerticalAlign.Center).height('50%') 646e41f4b71Sopenharmony_ci } 647e41f4b71Sopenharmony_ci } 648e41f4b71Sopenharmony_ci} 649e41f4b71Sopenharmony_ci``` 650e41f4b71Sopenharmony_ci 651e41f4b71Sopenharmony_ci 652e41f4b71Sopenharmony_ci## 示例5 653e41f4b71Sopenharmony_ci该示例实现了分割线样式支持自定义。 654e41f4b71Sopenharmony_ci 655e41f4b71Sopenharmony_ci```ts 656e41f4b71Sopenharmony_ci// xxx.ets 657e41f4b71Sopenharmony_ci@Entry 658e41f4b71Sopenharmony_ci@Component 659e41f4b71Sopenharmony_cistruct SelectExample { 660e41f4b71Sopenharmony_ci @State text: string = "TTTTT" 661e41f4b71Sopenharmony_ci @State index: number = -1 662e41f4b71Sopenharmony_ci @State arrowPosition: ArrowPosition = ArrowPosition.END 663e41f4b71Sopenharmony_ci build() { 664e41f4b71Sopenharmony_ci Column() { 665e41f4b71Sopenharmony_ci Select([{ value: 'aaa', icon: $r("app.media.icon") }, 666e41f4b71Sopenharmony_ci { value: 'bbb', icon: $r("app.media.icon") }, 667e41f4b71Sopenharmony_ci { value: 'ccc', icon: $r("app.media.icon") }, 668e41f4b71Sopenharmony_ci { value: 'ddd', icon: $r("app.media.icon") }]) 669e41f4b71Sopenharmony_ci .selected(this.index) 670e41f4b71Sopenharmony_ci .value(this.text) 671e41f4b71Sopenharmony_ci .font({ size: 16, weight: 500 }) 672e41f4b71Sopenharmony_ci .fontColor('#182431') 673e41f4b71Sopenharmony_ci .selectedOptionFont({ size: 16, weight: 400 }) 674e41f4b71Sopenharmony_ci .optionFont({ size: 16, weight: 400 }) 675e41f4b71Sopenharmony_ci .arrowPosition(this.arrowPosition) 676e41f4b71Sopenharmony_ci .menuAlign(MenuAlignType.START, {dx:0, dy:0}) 677e41f4b71Sopenharmony_ci .optionWidth(200) 678e41f4b71Sopenharmony_ci .optionHeight(300) 679e41f4b71Sopenharmony_ci .divider( { strokeWidth: 5, color: Color.Blue, startMargin: 10, endMargin: 10 }) 680e41f4b71Sopenharmony_ci .onSelect((index:number, text?: string | undefined)=>{ 681e41f4b71Sopenharmony_ci console.info('Select:' + index) 682e41f4b71Sopenharmony_ci this.index = index; 683e41f4b71Sopenharmony_ci if(text){ 684e41f4b71Sopenharmony_ci this.text = text; 685e41f4b71Sopenharmony_ci } 686e41f4b71Sopenharmony_ci }) 687e41f4b71Sopenharmony_ci }.width('100%') 688e41f4b71Sopenharmony_ci } 689e41f4b71Sopenharmony_ci} 690e41f4b71Sopenharmony_ci``` 691e41f4b71Sopenharmony_ci 692e41f4b71Sopenharmony_ci 693e41f4b71Sopenharmony_ci## 示例6 694e41f4b71Sopenharmony_ci该示例实现了隐藏分割线。 695e41f4b71Sopenharmony_ci 696e41f4b71Sopenharmony_ci```ts 697e41f4b71Sopenharmony_ci// xxx.ets 698e41f4b71Sopenharmony_ci@Entry 699e41f4b71Sopenharmony_ci@Component 700e41f4b71Sopenharmony_cistruct SelectExample { 701e41f4b71Sopenharmony_ci @State text: string = "TTTTT" 702e41f4b71Sopenharmony_ci @State index: number = -1 703e41f4b71Sopenharmony_ci @State arrowPosition: ArrowPosition = ArrowPosition.END 704e41f4b71Sopenharmony_ci build() { 705e41f4b71Sopenharmony_ci Column() { 706e41f4b71Sopenharmony_ci Select([{ value: 'aaa', icon: $r("app.media.icon") }, 707e41f4b71Sopenharmony_ci { value: 'bbb', icon: $r("app.media.icon") }, 708e41f4b71Sopenharmony_ci { value: 'ccc', icon: $r("app.media.icon") }, 709e41f4b71Sopenharmony_ci { value: 'ddd', icon: $r("app.media.icon") }]) 710e41f4b71Sopenharmony_ci .selected(this.index) 711e41f4b71Sopenharmony_ci .value(this.text) 712e41f4b71Sopenharmony_ci .font({ size: 16, weight: 500 }) 713e41f4b71Sopenharmony_ci .fontColor('#182431') 714e41f4b71Sopenharmony_ci .selectedOptionFont({ size: 16, weight: 400 }) 715e41f4b71Sopenharmony_ci .optionFont({ size: 16, weight: 400 }) 716e41f4b71Sopenharmony_ci .arrowPosition(this.arrowPosition) 717e41f4b71Sopenharmony_ci .menuAlign(MenuAlignType.START, {dx:0, dy:0}) 718e41f4b71Sopenharmony_ci .optionWidth(200) 719e41f4b71Sopenharmony_ci .optionHeight(300) 720e41f4b71Sopenharmony_ci .divider( null ) 721e41f4b71Sopenharmony_ci .onSelect((index:number, text?: string | undefined)=>{ 722e41f4b71Sopenharmony_ci console.info('Select:' + index) 723e41f4b71Sopenharmony_ci this.index = index; 724e41f4b71Sopenharmony_ci if(text){ 725e41f4b71Sopenharmony_ci this.text = text; 726e41f4b71Sopenharmony_ci } 727e41f4b71Sopenharmony_ci }) 728e41f4b71Sopenharmony_ci }.width('100%') 729e41f4b71Sopenharmony_ci } 730e41f4b71Sopenharmony_ci} 731e41f4b71Sopenharmony_ci``` 732e41f4b71Sopenharmony_ci 733