什么是Switch
Switch是一種用于顯示開(kāi)關(guān)狀態(tài)的UI控件.

基礎(chǔ)樣例
1.普通開(kāi)關(guān)
效果圖

代碼
<Switch
ohos:height="match_content"
ohos:width="match_content"/>
2.選中開(kāi)關(guān)
效果圖

代碼
<Switch
ohos:height="match_content"
ohos:width="match_content"
ohos:marked="true"/>
3.設(shè)置開(kāi)關(guān)上文字
效果圖

代碼
<Switch
ohos:height="match_content"
ohos:width="match_content"
ohos:text_state_off="OFF"
ohos:text_state_on="ON"/>
4.設(shè)置開(kāi)關(guān)上文字顏色和字體大小
效果圖

代碼
<Switch
ohos:height="match_content"
ohos:width="match_content"
ohos:text_color="red"
ohos:text_size="15fp"
ohos:text_state_off="OFF"
ohos:text_state_on="ON"/>
基礎(chǔ)樣例完整源代碼
https://gitee.com/hspbc/harmonyos_demos/tree/master/switchDemo
常用屬性說(shuō)明
| 屬性名 | 用途 |
|---|---|
| ohos:width | 設(shè)置控件寬度,可設(shè)置為:match_parent(和父控件一樣),match_content(按照內(nèi)容自動(dòng)伸縮),設(shè)置固定值(如200vp) |
| ohos:height | 設(shè)置控件高度,可設(shè)置為:match_parent(和父控件一樣),match_content(按照內(nèi)容自動(dòng)伸縮),設(shè)置固定值(如200vp) |
| ohos:layout_alignment | 在父控件內(nèi)對(duì)齊方式,可選值:left:居左;start:居左;center:居中;right:居右;end:居右;top:居上;bottom:居下;horizontal_center:水平居中;vertical_center:垂直居中 |
| ohos:background_element | 設(shè)置背景,可以是色值(如#FF0000)或圖片等 |
| ohos:visibility | 可選值: visible(顯示), invisible(隱藏,但是仍占據(jù)UI空間),hide(隱藏,且不占UI空間) |
| ohos:text_size | 設(shè)置字號(hào) |
| ohos:text_color | 設(shè)置文字顏色,樣例:ohos:text_color="#FF0000", |
| ohos:text_state_on | 設(shè)置選中時(shí)文字,樣例:ohos:text_state_on="ON" |
| ohos:text_state_off | 設(shè)置未選中時(shí)文字,樣例: ohos:text_state_off="OFF" |
| ohos:marked | 設(shè)置是否選中,可選值:true:選中;false:未選中 |
更多屬性及實(shí)際效果,可以在開(kāi)發(fā)工具里自行體驗(yàn).
關(guān)于我
廈門大學(xué)計(jì)算機(jī)專業(yè) | 前華為工程師
專注分享編程技術(shù),沒(méi)啥深度,但是易懂。
Java | 安卓 | 前端 | 小程序 | 鴻蒙
公眾號(hào):花生皮編程