自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS 基礎(chǔ)之 UI組件 (二)

開發(fā) 前端 OpenHarmony
HarmonyOS的 UI 常見組件分為顯示類和交互類。顯示類負(fù)責(zé)文本圖像顯示,交互類負(fù)責(zé)交互響應(yīng)功能。組件的具體使用場景,需要根據(jù)業(yè)務(wù)需求來選擇使用。

[[428927]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

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

https://harmonyos.51cto.com

概述

上一篇【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。

  1. <Text 
  2.     ohos:id="$+id:text" 
  3.     ohos:width="match_content" 
  4.     ohos:height="match_content" 
  5.     ohos:text="Text" 
  6.     /> 

可以根據(jù)不同需要,給Text添加各種屬性值。常用的背景如常見的文本背景、按鈕背景,可以采用XML格式放置在graphic目錄下。如:創(chuàng)建“background_text.xml”,在“background_text.xml”中定義文本的背景。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     //背景形狀,oval橢圓,rectangle方形... 
  4.     ohos:shape="rectangle">   
  5.     <corners 
  6.         //背景圓角程度 
  7.         ohos:radius="20"/>  
  8.     <solid 
  9.         //背景顏色 
  10.         ohos:color="#878787"/> 
  11. </shape> 

2. Image

Image 是用來顯示圖片的組件。

2.1 創(chuàng)建 Image

在 src -> main -> resources -> base -> media,添加一個(gè)圖片至media文件夾下,既可以在XML中創(chuàng)建 Image,也可以在代碼中創(chuàng)建 Image。

  1. <Image 
  2.     ohos:id="$+id:image" 
  3.     ohos:width="match_content" 
  4.     ohos:height="match_content" 
  5.     ohos:layout_alignment="center" 
  6.     ohos:image_src="$media:plant" 
  7.     //設(shè)置透明度 
  8.     ohos:alpha="0.5" 
  9. /> 
  1. Image image = new Image(getContext()); 
  2. image.setPixelMap(ResourceTable.Media_plant); 

2.2 縮放 Image

  1. ohos:image_src="$media:plant" 
  2.  //設(shè)置縮放方式 
  3.  ohos:scale_mode="zoom_center" 
  4.  //設(shè)置縮放系數(shù) 
  5.  ohos:scale_x="0.5" 
  6.  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)度。

  1. <ProgressBar 
  2.     ohos:progress_width="10vp" 
  3.     ohos:height="80vp" 
  4.     ohos:width="280vp" 
  5.     //設(shè)置進(jìn)度條方向?yàn)樗?,?nbsp;vertical 為水平) 
  6.     ohos:orientation="horizontal" 
  7.     //設(shè)置最大值最小值 
  8.     ohos:max="100" 
  9.     ohos:min="0" 
  10.     //設(shè)置當(dāng)前進(jìn)度  
  11.     ohos:progress="60" 
  12.     //設(shè)置進(jìn)度條顏色 
  13.     ohos:progress_element="#FF9900" 
  14.     //設(shè)置進(jìn)度條組件底色 
  15.     ohos:background_instruct_element="#F23456" 
  16.     //設(shè)置分割線 
  17.     ohos:divider_lines_enabled="true" 
  18.     ohos:divider_lines_number="5" 
  19.     //設(shè)置提示文字以及提示文字顏色 
  20.     ohos:progress_hint_text="60%" 
  21.     ohos:progress_hint_text_color="#FF4989" 
  22.     /> 

如上圖,進(jìn)度條展示效果如下:

 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)

4. Button

Button是一種常見的組件,點(diǎn)擊可以觸發(fā)對(duì)應(yīng)的操作,通常由文本或圖標(biāo)組成,也可以由圖標(biāo)和文本共同組成。

  1. <Button 
  2.     ohos:id="$+id:button" 
  3.     ohos:width="match_content" 
  4.     ohos:height="match_content" 
  5.     ohos:text_size="27fp" 
  6.     ohos:text="button" 
  7.     ohos:background_element="$graphic:background_button" 
  8.     ohos:left_margin="15vp" 
  9.     ohos:bottom_margin="15vp" 
  10.     ohos:right_padding="8vp" 
  11.     ohos:left_padding="8vp" 
  12.     /> 

4.1 背景樣式

如上布局xml文件中可以設(shè)置 Button 字體大小,文字內(nèi)容,文字格式,背景等。其中背景可以在 graphic 文件夾下自定義需要的背景風(fēng)格。

例如:background_button.xml;

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     //設(shè)置控件形狀為方形 
  4.     ohos:shape="rectangle"
  5.     <corners 
  6.         //背景圓角程度 
  7.         ohos:radius="10"/> 
  8.     <solid 
  9.         //背景背景顏色 
  10.         ohos:color="#007CFD"/> 
  11. </shape> 
  1. 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)。

  1. Button button = (Button) findComponentById(ResourceTable.Id_button); 
  2. // 為按鈕設(shè)置點(diǎn)擊事件回調(diào) 
  3. button.setClickedListener(new Component.ClickedListener() { 
  4.     public void onClick(Component v) { 
  5.         // 此處添加點(diǎn)擊按鈕后的事件處理邏輯 
  6.     } 
  7. });  

5. Picker

Picker 提供了滑動(dòng)選擇器,允許用戶從預(yù)定義范圍中進(jìn)行選擇。常見的 Picker 有 DatePicker ( 選擇日期 ) 和 TimePicker ( 選擇時(shí)間 )。

  1. <Picker 
  2.     ohos:height="match_content" 
  3.     ohos:width="160vp" 
  4.     ohos:top_margin="60vp" 
  5.     ohos:selected_text_size="40vp" 
  6.     ohos:normal_text_size="20vp" 
  7.     ohos:layout_alignment="center" 
  8.     /> 

根據(jù) xml 布局文件配置自己需要的 Picker 種類,顯示效果如下:

Picker

 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)

DatePicker

 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)

TimePicker

 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)

6. Switch

Switch是切換單個(gè)設(shè)置開/關(guān)兩種狀態(tài)的組件。

  1. <Switch 
  2.     ohos:height="60vp" 
  3.     ohos:width="120vp" 
  4.     ohos:layout_alignment="center" 
  5.     ohos:top_margin="60vp" 
  6.     /> 

Switch相當(dāng)于一個(gè)雙相開關(guān),點(diǎn)擊開關(guān)的時(shí)候會(huì)進(jìn)行切換,效果如下:

 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)

7. RadioButton

RadioButton 是用于多選一操作的組件,需要搭配 RadioContainer 使用,實(shí)現(xiàn)單選效果,RadioContainer 是 RadioButton 的容器,在其包裹下的 RadioButton 保證只有一個(gè)被選項(xiàng)。

  1. <RadioButton 
  2.     ohos:id="$+id:harmony_UI" 
  3.     ohos:top_margin="60vp" 
  4.     ohos:height="40vp" 
  5.     ohos:width="match_content" 
  6.     ohos:text="Hello Harmony" 
  7.     ohos:layout_alignment="center" 
  8.     ohos:text_size="20fp" 
  9.     //字體顏色,text_color_onon 為選中狀態(tài),text_color_off 為未選中狀態(tài) 
  10.     ohos:text_color_on="#00BFFF" 
  11.     ohos:text_color_off="#808080" 
  12.     /> 

選中狀態(tài)

 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)

未選中狀態(tài)

 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)

以上兩張圖為 RadioButton 的選中與未選中的狀態(tài)對(duì)比。

8. Checkbox

Checkbox可以實(shí)現(xiàn)選中和取消選中的功能。

  1. <Checkbox 
  2.     ohos:top_margin="60vp" 
  3.     ohos:layout_alignment="center" 
  4.     ohos:id="$+id:check_box" 
  5.     ohos:height="match_content" 
  6.     ohos:width="match_content" 
  7.     ohos:text="harmony checkbox" 
  8.     ohos:text_color_on="#00AAEE" 
  9.     ohos:text_color_off="#000000" 
  10.     ohos:text_size="20fp" /> 

xml 布局文件中創(chuàng)建 Checkbox 組件,顯示效果如下:

 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)

9. TextField

TextField 是一種文本輸入框。

  1. <TextField 
  2.     ohos:height="40vp" 
  3.     ohos:width="200vp" 
  4.     //設(shè)置提示語 
  5.     ohos:hint="Please enter......" 
  6.     //設(shè)置內(nèi)邊距 
  7.     ohos:left_padding="24vp" 
  8.     ohos:right_padding="24vp" 
  9.     ohos:top_padding="8vp" 
  10.     ohos:bottom_padding="8vp" 
  11.     //設(shè)置可多行顯示 
  12.     ohos:multiple_lines="true" 
  13.  /> 

在代碼中,我們也可以對(duì)文本輸入框設(shè)置響應(yīng)事件。

  1. textField.setFocusChangedListener(((component, isFocused) -> { 
  2.     if (isFocused) {  
  3.         // 獲取到焦點(diǎn) 
  4.         ... 
  5.     }else {  
  6.         // 失去焦點(diǎn) 
  7.         ... 
  8.     } 
  9. })); 

10. ToastDialog

ToastDialog 是在窗口上方彈出的對(duì)話框,是通知操作的簡單反饋。ToastDialog 會(huì)在一段時(shí)間后消失,在此期間,用戶還可以操作當(dāng)前窗口的其他組件。

在代碼中創(chuàng)建 ToastDialog。

自定義布局 Resource 文件 Layout_layout_toast.xml 內(nèi)容如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_content" 
  5.     ohos:width="match_content" 
  6.     ohos:orientation="vertical"
  7.     <Text 
  8.         ohos:id="$+id:msg_toast" 
  9.         ohos:height="match_content" 
  10.         ohos:width="match_content" 
  11.         ohos:left_padding="16vp" 
  12.         ohos:right_padding="16vp" 
  13.         ohos:top_padding="4vp" 
  14.         ohos:bottom_padding="4vp" 
  15.         ohos:layout_alignment="center" 
  16.         ohos:text_size="16fp" 
  17.         ohos:text="This is a ToastDialog for the customized component" 
  18.         ohos:background_element="$graphic:background_toast_element"/> 
  19. </DirectionalLayout> 

11. ScrollView

ScrollView 是一種帶滾動(dòng)功能的組件,它采用滑動(dòng)的方式在有限的區(qū)域內(nèi)顯示更多的內(nèi)容。

  1. <ScrollView 
  2.     ohos:id="$+id:scrollview" 
  3.     ohos:height="300vp" 
  4.     ohos:width="300vp" 
  5.     ohos:background_element="#FFDEAD" 
  6.     ohos:top_margin="32vp" 
  7.     ohos:bottom_padding="16vp" 
  8.     ohos:layout_alignment="horizontal_center"
  9.     <DirectionalLayout 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_content"
  12.         <Image 
  13.             ohos:id="$+id:img_1" 
  14.             ohos:width="300vp" 
  15.             ohos:height="match_content" 
  16.             ohos:top_margin="16vp" 
  17.             ohos:image_src="$media:dog.png"/> 
  18.         <!--放置任意需要展示的組件--> 
  19.     </DirectionalLayout> 
  20. </ScrollView> 

12. ListContainer

ListContainer 是用來呈現(xiàn)連續(xù)、多行數(shù)據(jù)的組件,包含一系列相同類型的列表項(xiàng)。

首先需要在 layout 目錄下的 xml 布局文件中創(chuàng)建 ListContainer。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.     <ListContainer 
  3.         xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.         ohos:id="$+id:list_container" 
  5.         ohos:height="200vp" 
  6.         ohos:width="300vp" 
  7.         ohos:layout_alignment="horizontal_center"/> 

 然后在 layout 目錄下新建 xml 文件(例:item_listContainer.xml),作為 ListContainer 的子布局。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_content" 
  5.     ohos:width="match_parent" 
  6.     ohos:left_margin="16vp" 
  7.     ohos:right_margin="16vp" 
  8.     ohos:orientation="vertical"
  9.     <Text 
  10.         ohos:id="$+id:item_index" 
  11.         ohos:height="match_content" 
  12.         ohos:width="match_content" 
  13.         ohos:padding="4vp" 
  14.         ohos:text="Item0" 
  15.         ohos:text_size="20fp" 
  16.         ohos:layout_alignment="center"/> 
  17. </DirectionalLayout> 

ListContainer 每一行可以為不同的數(shù)據(jù),因此需要適配不同的數(shù)據(jù)結(jié)構(gòu),使其都能添加到 ListContainer 上。

創(chuàng)建 ListcontainerItemProvider.java,繼承自RecycleItemProvider。

ListContainer 的樣式設(shè)置

看看效果:

 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)

13. PageSlider

PageSlider 是一個(gè)交互類組件。

main_pageSlider.xml

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:background_element="blue" 
  7.     ohos:orientation="vertical"
  8.  
  9.     <PageSlider 
  10.         ohos:id="$+id:pager_slider" 
  11.         ohos:height="0vp" 
  12.         ohos:width="match_parent" 
  13.         ohos:background_element="#ffffff" 
  14.         ohos:weight="1"/> 
  15.  
  16.     <RadioContainer 
  17.         ohos:id="$+id:radio_container" 
  18.         ohos:height="60vp" 
  19.         ohos:width="match_parent" 
  20.         ohos:alignment="horizontal_center" 
  21.         ohos:orientation="horizontal"
  22.  
  23.         <RadioButton 
  24.             ohos:height="match_parent" 
  25.             ohos:width="match_content" 
  26.             ohos:text_size="20fp" 
  27.             /> 
  28.  
  29.         <RadioButton 
  30.             ohos:height="match_parent" 
  31.             ohos:width="match_content" 
  32.             ohos:text_size="20fp" 
  33.             /> 
  34.  
  35.     </RadioContainer> 
  36. </DirectionalLayout> 

pageSlider1.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DependentLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent"
  6.  
  7.     <Text 
  8.         ohos:height="match_content" 
  9.         ohos:width="match_content" 
  10.         ohos:center_in_parent="true" 
  11.         ohos:text="PageSlider1" 
  12.         ohos:text_size="25fp"/> 
  13. </DependentLayout> 

pageSlider2.xml

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DependentLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent"
  6.  
  7.     <Text 
  8.         ohos:height="match_content" 
  9.         ohos:width="match_content" 
  10.         ohos:center_in_parent="true" 
  11.         ohos:text="PageSlider2" 
  12.         ohos:text_size="25fp"/> 
  13. </DependentLayout> 

然后啟動(dòng)程序看一下控件效果:

 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)
 #星光計(jì)劃1.0# HarmonyOS 基礎(chǔ)之 UI組件 (二)-鴻蒙HarmonyOS技術(shù)社區(qū)

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

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

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-09-14 09:34:05

鴻蒙HarmonyOS應(yīng)用

2021-08-12 15:01:09

鴻蒙HarmonyOS應(yīng)用

2021-08-24 14:57:27

鴻蒙HarmonyOS應(yīng)用

2020-11-17 11:48:44

HarmonyOS

2011-08-01 15:57:58

2021-01-20 13:50:36

鴻蒙HarmonyOS應(yīng)用開發(fā)

2023-12-29 08:37:59

2011-04-14 10:05:16

BlackBerry

2011-04-14 10:03:32

UI組件BlackBerry

2020-11-25 12:02:02

TableLayout

2021-09-15 10:19:15

鴻蒙HarmonyOS應(yīng)用

2011-06-03 09:34:14

Android iphone tab

2022-04-24 14:56:53

容器組件StackTS

2021-11-01 10:21:36

鴻蒙HarmonyOS應(yīng)用

2021-09-09 14:49:26

鴻蒙HarmonyOS應(yīng)用

2021-07-27 11:39:40

鴻蒙HarmonyOS應(yīng)用

2020-11-30 14:09:17

HarmonyOS

2011-07-14 17:17:21

C++指針

2009-06-24 17:05:10

2011-03-09 16:10:34

MAC地址二層組播IGMP
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)