HarmonyOS 基礎(chǔ)之 UI組件 (二)
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
概述
上一篇【HarmonyOS 基礎(chǔ)之 UI 布局(一)】我們一起學(xué)習(xí)了HarmonyOS的常用 UI 布局和一些基礎(chǔ)屬性,一個(gè)界面除了UI布局,組件也是非常重要的組成部分。HarmonyOS的 UI 常見組件分為顯示類和交互類。顯示類負(fù)責(zé)文本圖像顯示,交互類負(fù)責(zé)交互響應(yīng)功能。組件的具體使用場景,需要根據(jù)業(yè)務(wù)需求來選擇使用。今天這篇文章我將跟大家分享一下常見組件的使用場景和特性。
常見組件
根據(jù)組件的功能,可以將組件分為顯示類、交互類:
1. Text
Text是用來顯示字符串的組件,在界面上顯示為一塊文本區(qū)域。
在layout目錄下的xml文件中創(chuàng)建Text。
- <Text
- ohos:id="$+id:text"
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:text="Text"
- />
可以根據(jù)不同需要,給Text添加各種屬性值。常用的背景如常見的文本背景、按鈕背景,可以采用XML格式放置在graphic目錄下。如:創(chuàng)建“background_text.xml”,在“background_text.xml”中定義文本的背景。
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
- //背景形狀,oval橢圓,rectangle方形...
- ohos:shape="rectangle">
- <corners
- //背景圓角程度
- ohos:radius="20"/>
- <solid
- //背景顏色
- ohos:color="#878787"/>
- </shape>
2. Image
Image 是用來顯示圖片的組件。
2.1 創(chuàng)建 Image
在 src -> main -> resources -> base -> media,添加一個(gè)圖片至media文件夾下,既可以在XML中創(chuàng)建 Image,也可以在代碼中創(chuàng)建 Image。
- <Image
- ohos:id="$+id:image"
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:layout_alignment="center"
- ohos:image_src="$media:plant"
- //設(shè)置透明度
- ohos:alpha="0.5"
- />
- Image image = new Image(getContext());
- image.setPixelMap(ResourceTable.Media_plant);
2.2 縮放 Image
- ohos:image_src="$media:plant"
- //設(shè)置縮放方式
- ohos:scale_mode="zoom_center"
- //設(shè)置縮放系數(shù)
- ohos:scale_x="0.5"
- ohos:scale_y="0.5"
2.3 裁剪 Image
當(dāng) Image 尺寸小于圖片尺寸時(shí),可以對(duì)圖片進(jìn)行裁剪,仍以 Image 的寬高為 200 vp 為例,小于圖片尺寸。以左對(duì)齊裁剪為例,設(shè)置 clip_alignment = “256”。
3. ProgressBar
ProgressBar 用于顯示內(nèi)容或操作的進(jìn)度。
- <ProgressBar
- ohos:progress_width="10vp"
- ohos:height="80vp"
- ohos:width="280vp"
- //設(shè)置進(jìn)度條方向?yàn)樗?,?nbsp;vertical 為水平)
- ohos:orientation="horizontal"
- //設(shè)置最大值最小值
- ohos:max="100"
- ohos:min="0"
- //設(shè)置當(dāng)前進(jìn)度
- ohos:progress="60"
- //設(shè)置進(jìn)度條顏色
- ohos:progress_element="#FF9900"
- //設(shè)置進(jìn)度條組件底色
- ohos:background_instruct_element="#F23456"
- //設(shè)置分割線
- ohos:divider_lines_enabled="true"
- ohos:divider_lines_number="5"
- //設(shè)置提示文字以及提示文字顏色
- ohos:progress_hint_text="60%"
- ohos:progress_hint_text_color="#FF4989"
- />
如上圖,進(jìn)度條展示效果如下:

4. Button
Button是一種常見的組件,點(diǎn)擊可以觸發(fā)對(duì)應(yīng)的操作,通常由文本或圖標(biāo)組成,也可以由圖標(biāo)和文本共同組成。
- <Button
- ohos:id="$+id:button"
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:text_size="27fp"
- ohos:text="button"
- ohos:background_element="$graphic:background_button"
- ohos:left_margin="15vp"
- ohos:bottom_margin="15vp"
- ohos:right_padding="8vp"
- ohos:left_padding="8vp"
- />
4.1 背景樣式
如上布局xml文件中可以設(shè)置 Button 字體大小,文字內(nèi)容,文字格式,背景等。其中背景可以在 graphic 文件夾下自定義需要的背景風(fēng)格。
例如:background_button.xml;
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
- //設(shè)置控件形狀為方形
- ohos:shape="rectangle">
- <corners
- //背景圓角程度
- ohos:radius="10"/>
- <solid
- //背景背景顏色
- ohos:color="#007CFD"/>
- </shape>
- ohos:background_element="$graphic:oval_button_element"
復(fù)制通過在 graphic 文件夾下自定義 Button 樣式文件,可以自定義不同類型的 Button,按照 Button 的形狀,按鈕可以分為:普通,橢圓,膠囊,圓形等。
4.2 點(diǎn)擊事件
用戶點(diǎn)擊Button時(shí),Button對(duì)象將收到一個(gè)點(diǎn)擊事件,然后自定義響應(yīng)點(diǎn)擊事件的方法。如:通過創(chuàng)建一個(gè) Component.ClickedListener 對(duì)象,然后通過調(diào)用 setClickedListener 將其分配給按鈕,再收到該點(diǎn)擊事件后,執(zhí)行相應(yīng)操作對(duì)該事件做出響應(yīng)。
- Button button = (Button) findComponentById(ResourceTable.Id_button);
- // 為按鈕設(shè)置點(diǎn)擊事件回調(diào)
- button.setClickedListener(new Component.ClickedListener() {
- public void onClick(Component v) {
- // 此處添加點(diǎn)擊按鈕后的事件處理邏輯
- }
- });
5. Picker
Picker 提供了滑動(dòng)選擇器,允許用戶從預(yù)定義范圍中進(jìn)行選擇。常見的 Picker 有 DatePicker ( 選擇日期 ) 和 TimePicker ( 選擇時(shí)間 )。
- <Picker
- ohos:height="match_content"
- ohos:width="160vp"
- ohos:top_margin="60vp"
- ohos:selected_text_size="40vp"
- ohos:normal_text_size="20vp"
- ohos:layout_alignment="center"
- />
根據(jù) xml 布局文件配置自己需要的 Picker 種類,顯示效果如下:
Picker

DatePicker

TimePicker

6. Switch
Switch是切換單個(gè)設(shè)置開/關(guān)兩種狀態(tài)的組件。
- <Switch
- ohos:height="60vp"
- ohos:width="120vp"
- ohos:layout_alignment="center"
- ohos:top_margin="60vp"
- />
Switch相當(dāng)于一個(gè)雙相開關(guān),點(diǎn)擊開關(guān)的時(shí)候會(huì)進(jìn)行切換,效果如下:

7. RadioButton
RadioButton 是用于多選一操作的組件,需要搭配 RadioContainer 使用,實(shí)現(xiàn)單選效果,RadioContainer 是 RadioButton 的容器,在其包裹下的 RadioButton 保證只有一個(gè)被選項(xiàng)。
- <RadioButton
- ohos:id="$+id:harmony_UI"
- ohos:top_margin="60vp"
- ohos:height="40vp"
- ohos:width="match_content"
- ohos:text="Hello Harmony"
- ohos:layout_alignment="center"
- ohos:text_size="20fp"
- //字體顏色,text_color_onon 為選中狀態(tài),text_color_off 為未選中狀態(tài)
- ohos:text_color_on="#00BFFF"
- ohos:text_color_off="#808080"
- />
選中狀態(tài)

未選中狀態(tài)

以上兩張圖為 RadioButton 的選中與未選中的狀態(tài)對(duì)比。
8. Checkbox
Checkbox可以實(shí)現(xiàn)選中和取消選中的功能。
- <Checkbox
- ohos:top_margin="60vp"
- ohos:layout_alignment="center"
- ohos:id="$+id:check_box"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:text="harmony checkbox"
- ohos:text_color_on="#00AAEE"
- ohos:text_color_off="#000000"
- ohos:text_size="20fp" />
xml 布局文件中創(chuàng)建 Checkbox 組件,顯示效果如下:

9. TextField
TextField 是一種文本輸入框。
- <TextField
- ohos:height="40vp"
- ohos:width="200vp"
- //設(shè)置提示語
- ohos:hint="Please enter......"
- //設(shè)置內(nèi)邊距
- ohos:left_padding="24vp"
- ohos:right_padding="24vp"
- ohos:top_padding="8vp"
- ohos:bottom_padding="8vp"
- //設(shè)置可多行顯示
- ohos:multiple_lines="true"
- />
在代碼中,我們也可以對(duì)文本輸入框設(shè)置響應(yīng)事件。
- textField.setFocusChangedListener(((component, isFocused) -> {
- if (isFocused) {
- // 獲取到焦點(diǎn)
- ...
- }else {
- // 失去焦點(diǎn)
- ...
- }
- }));
10. ToastDialog
ToastDialog 是在窗口上方彈出的對(duì)話框,是通知操作的簡單反饋。ToastDialog 會(huì)在一段時(shí)間后消失,在此期間,用戶還可以操作當(dāng)前窗口的其他組件。
在代碼中創(chuàng)建 ToastDialog。
自定義布局 Resource 文件 Layout_layout_toast.xml 內(nèi)容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:orientation="vertical">
- <Text
- ohos:id="$+id:msg_toast"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:left_padding="16vp"
- ohos:right_padding="16vp"
- ohos:top_padding="4vp"
- ohos:bottom_padding="4vp"
- ohos:layout_alignment="center"
- ohos:text_size="16fp"
- ohos:text="This is a ToastDialog for the customized component"
- ohos:background_element="$graphic:background_toast_element"/>
- </DirectionalLayout>
11. ScrollView
ScrollView 是一種帶滾動(dòng)功能的組件,它采用滑動(dòng)的方式在有限的區(qū)域內(nèi)顯示更多的內(nèi)容。
- <ScrollView
- ohos:id="$+id:scrollview"
- ohos:height="300vp"
- ohos:width="300vp"
- ohos:background_element="#FFDEAD"
- ohos:top_margin="32vp"
- ohos:bottom_padding="16vp"
- ohos:layout_alignment="horizontal_center">
- <DirectionalLayout
- ohos:height="match_content"
- ohos:width="match_content">
- <Image
- ohos:id="$+id:img_1"
- ohos:width="300vp"
- ohos:height="match_content"
- ohos:top_margin="16vp"
- ohos:image_src="$media:dog.png"/>
- <!--放置任意需要展示的組件-->
- </DirectionalLayout>
- </ScrollView>
12. ListContainer
ListContainer 是用來呈現(xiàn)連續(xù)、多行數(shù)據(jù)的組件,包含一系列相同類型的列表項(xiàng)。
首先需要在 layout 目錄下的 xml 布局文件中創(chuàng)建 ListContainer。
- <?xml version="1.0" encoding="utf-8"?>
- <ListContainer
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:id="$+id:list_container"
- ohos:height="200vp"
- ohos:width="300vp"
- ohos:layout_alignment="horizontal_center"/>
然后在 layout 目錄下新建 xml 文件(例:item_listContainer.xml),作為 ListContainer 的子布局。
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:left_margin="16vp"
- ohos:right_margin="16vp"
- ohos:orientation="vertical">
- <Text
- ohos:id="$+id:item_index"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:padding="4vp"
- ohos:text="Item0"
- ohos:text_size="20fp"
- ohos:layout_alignment="center"/>
- </DirectionalLayout>
ListContainer 每一行可以為不同的數(shù)據(jù),因此需要適配不同的數(shù)據(jù)結(jié)構(gòu),使其都能添加到 ListContainer 上。
創(chuàng)建 ListcontainerItemProvider.java,繼承自RecycleItemProvider。
ListContainer 的樣式設(shè)置
看看效果:

13. PageSlider
PageSlider 是一個(gè)交互類組件。
main_pageSlider.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:background_element="blue"
- ohos:orientation="vertical">
- <PageSlider
- ohos:id="$+id:pager_slider"
- ohos:height="0vp"
- ohos:width="match_parent"
- ohos:background_element="#ffffff"
- ohos:weight="1"/>
- <RadioContainer
- ohos:id="$+id:radio_container"
- ohos:height="60vp"
- ohos:width="match_parent"
- ohos:alignment="horizontal_center"
- ohos:orientation="horizontal">
- <RadioButton
- ohos:height="match_parent"
- ohos:width="match_content"
- ohos:text_size="20fp"
- />
- <RadioButton
- ohos:height="match_parent"
- ohos:width="match_content"
- ohos:text_size="20fp"
- />
- </RadioContainer>
- </DirectionalLayout>
pageSlider1.xml
- <?xml version="1.0" encoding="utf-8"?>
- <DependentLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent">
- <Text
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:center_in_parent="true"
- ohos:text="PageSlider1"
- ohos:text_size="25fp"/>
- </DependentLayout>
pageSlider2.xml
- <?xml version="1.0" encoding="utf-8"?>
- <DependentLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent">
- <Text
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:center_in_parent="true"
- ohos:text="PageSlider2"
- ohos:text_size="25fp"/>
- </DependentLayout>
然后啟動(dòng)程序看一下控件效果:


51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)