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

HarmonyOS 基礎(chǔ)之 UI 布局(一)

系統(tǒng) OpenHarmony
通過(guò)對(duì) android UI 已有知識(shí)的回顧和最近 harmony 應(yīng)用開(kāi)發(fā)的學(xué)習(xí)研究,我總結(jié)了一篇UI框架開(kāi)發(fā)文檔,記錄一些開(kāi)發(fā)中可能遇到的小問(wèn)題和有用的小技巧分享給大家。

[[417158]]

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

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

https://harmonyos.51cto.com

概述

​ 華為鴻蒙系統(tǒng)是一款全新的面向全場(chǎng)景的分布式操作系統(tǒng),基于 harmony 的應(yīng)用開(kāi)發(fā)也越來(lái)越廣泛。鴻蒙系統(tǒng)是否也能開(kāi)發(fā)出像安卓平臺(tái)一樣絢麗多彩的應(yīng)用 UI 界面呢?通過(guò)對(duì) android UI 已有知識(shí)的回顧和最近 harmony 應(yīng)用開(kāi)發(fā)的學(xué)習(xí)研究,我總結(jié)了一篇UI框架開(kāi)發(fā)文檔,記錄一些開(kāi)發(fā)中可能遇到的小問(wèn)題和有用的小技巧分享給大家。

常用布局

一、DirectionalLayout 布局

​ DirectionalLayout 布局即方向布局,該種分為兩種模式 ( vertical ) 垂直排列子元素,( horizontal ) 水平排列子元素。垂直排列子元素 height 的總和不得超過(guò)父元素否則會(huì)被截取,超過(guò)部分無(wú)法顯示。同理水平排列子元素 width 的總和如果超過(guò)父元素也會(huì)被截取。

​ 水平排列和垂直排列通過(guò)設(shè)置 ohos:orientation 屬性定義,ohos:orientation = " vertical " 為垂直排列,ohos:orientation = " horizontal" 為水平排列;

1、垂直排列

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.         xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.         ohos:width="match_parent" 
  5.         ohos:height="match_parent" 
  6.         // 垂直排列 
  7.         ohos:orientation="vertical"
  8.  
  9.     <Text 
  10.         ohos:text="$string:HelloWorld" 
  11.         ohos:width="match_content" 
  12.         ohos:height="match_content" 
  13.         ohos:text_size="50" 
  14.         ohos:top_margin="30fp" 
  15.         ohos:background_element="#f54444" 
  16.         ohos:layout_alignment="horizontal_center"/> 
  17.     <Text 
  18.         ohos:text="$string:HelloWorld" 
  19.         ohos:width="match_content" 
  20.         ohos:height="match_content" 
  21.         ohos:text_size="50" 
  22.         ohos:top_margin="30fp" 
  23.         ohos:background_element="#f54444" 
  24.         ohos:layout_alignment="horizontal_center"/> 
  25.     <Text 
  26.         ohos:text="$string:HelloWorld" 
  27.         ohos:width="match_content" 
  28.         ohos:height="match_content" 
  29.         ohos:text_size="50" 
  30.         ohos:top_margin="30fp" 
  31.         ohos:background_element="#f54444" 
  32.         ohos:layout_alignment="horizontal_center"/> 
  33. </DirectionalLayout> 

如上代碼為垂直方向的三個(gè)textview布局,效果圖如下:

【中軟國(guó)際】HarmonyOS 基礎(chǔ)之 UI 布局(一)-鴻蒙HarmonyOS技術(shù)社區(qū)

2、水平排列

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.         xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.         ohos:width="match_parent" 
  5.         ohos:height="match_parent" 
  6.         // 水平排列 
  7.         ohos:orientation="horizontal"
  8.  
  9.     <Text 
  10.         ohos:text="$string:HelloWorld" 
  11.         ohos:width="match_content" 
  12.         ohos:height="match_content" 
  13.         ohos:text_size="50" 
  14.         ohos:top_margin="30fp" 
  15.         ohos:left_margin="10fp" 
  16.         ohos:background_element="#f54444" 
  17.         ohos:layout_alignment="horizontal_center"/> 
  18.     <Text 
  19.         ohos:text="$string:HelloWorld" 
  20.         ohos:width="match_content" 
  21.         ohos:height="match_content" 
  22.         ohos:text_size="50" 
  23.         ohos:top_margin="30fp" 
  24.         ohos:left_margin="10fp" 
  25.         ohos:background_element="#f54444" 
  26.         ohos:layout_alignment="horizontal_center"/> 
  27.     <Text 
  28.         ohos:text="$string:HelloWorld" 
  29.         ohos:width="match_content" 
  30.         ohos:height="match_content" 
  31.         ohos:text_size="50" 
  32.         ohos:top_margin="30fp" 
  33.         ohos:left_margin="10fp" 
  34.         ohos:background_element="#f54444" 
  35.         ohos:layout_alignment="horizontal_center"/> 
  36. </DirectionalLayout> 

如上代碼為水平方向的三個(gè)textview布局,效果圖如下:

【中軟國(guó)際】HarmonyOS 基礎(chǔ)之 UI 布局(一)-鴻蒙HarmonyOS技術(shù)社區(qū)

3、對(duì)齊方式

DirectionalLayout 中的組件使用 layout_alignment 控制自身在布局中的對(duì)齊方式,當(dāng)對(duì)齊方式與排列方式方向一致時(shí),對(duì)齊方式不會(huì)生效具體見(jiàn)下表。

三種基本對(duì)齊方式:左對(duì)齊,右對(duì)齊,居中。分別對(duì)應(yīng) layout_alignment 屬性的

  1. ohos:layout_alignment=“left” 
  2.  
  3. ohos:layout_alignment=“horizontal_center” 
  4.  
  5. ohos:layout_alignment=“right” 

布局展示的樣式為:

【中軟國(guó)際】HarmonyOS 基礎(chǔ)之 UI 布局(一)-鴻蒙HarmonyOS技術(shù)社區(qū)

4、權(quán)重

權(quán)重( weight )就是按比例來(lái)分配組件占用父組件的大小,通過(guò) ohos:weight 屬性來(lái)定義。布局計(jì)算公式為:組件寬度=組件weight/所有組件weight之和*父布局可分配寬度;如 ohos:weight 分別設(shè)置為 ohos:weight = “1”,ohos:weight = “2”,ohos:weight = "3"的三個(gè)空間,布局則分別占父空間的1/6 , 2/6 , 3/6 。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:width="match_parent" 
  5.     ohos:height="match_parent" 
  6.     ohos:orientation="horizontal"
  7.  
  8.     <Text 
  9.         ohos:text="TEST" 
  10.         ohos:weight="1" 
  11.         ohos:width="match_content" 
  12.         ohos:height="match_content" 
  13.         ohos:text_size="50" 
  14.         ohos:top_margin="30fp" 
  15.         ohos:background_element="#f78731"/> 
  16.     <Text 
  17.         ohos:text="TEST" 
  18.         ohos:weight="2" 
  19.         ohos:width="match_content" 
  20.         ohos:height="match_content" 
  21.         ohos:text_size="50" 
  22.         ohos:top_margin="30fp" 
  23.         ohos:background_element="#f54444"/> 
  24.     <Text 
  25.         ohos:text="TEST" 
  26.         ohos:weight="3" 
  27.         ohos:width="match_content" 
  28.         ohos:height="match_content" 
  29.         ohos:text_size="50" 
  30.         ohos:top_margin="30fp" 
  31.         ohos:background_element="#f78731"/> 
  32. </DirectionalLayout> 

以上代碼展示的布局效果圖如下:

【中軟國(guó)際】HarmonyOS 基礎(chǔ)之 UI 布局(一)-鴻蒙HarmonyOS技術(shù)社區(qū)

二、DependentLayout 布局

DependentLayout 與 DirectionalLayout相比,擁有更多的排布方式,每個(gè)組件可以指定相對(duì)于其他同級(jí)元素的位置,或者指定相對(duì)于父組件的位置。

1、相對(duì)于同級(jí)組件的位置布局

2、相對(duì)于父組件的位置布局

DependentLayout 布局類(lèi)似于 Android 的 RelativeLayout 比較靈活,具體怎么展示和調(diào)整組件布局可自行測(cè)試。

三、TableLayout 布局

TableLayout使用表格的方式劃分子組件, 也就是行和列的方式,TableLayout可配置表格的排列方式,行數(shù)和列數(shù),以及組件的位置。

1、行列的設(shè)置

ohos:row_count 表示設(shè)置網(wǎng)格布局中行數(shù),ohos:column_count 表示設(shè)置網(wǎng)格布局中的列數(shù)。如果沒(méi)有為子布局設(shè)置行列數(shù),則自動(dòng)繼承父布局的行數(shù)和列數(shù)。在網(wǎng)格布局中若子組件的數(shù)量超出列數(shù)設(shè)置,則會(huì)自動(dòng)添加行數(shù)。比如設(shè)置一行,兩列,但是是三個(gè)子組件,行數(shù)設(shè)置失效,就會(huì)自動(dòng)增加一行。如下設(shè)置三行兩列。則布局就是如下展示。

  1. <TableLayout 
  2.     ... 
  3.     ohos:row_count="3" 
  4.     ohos:column_count="2" 
  5.     /> 
【中軟國(guó)際】HarmonyOS 基礎(chǔ)之 UI 布局(一)-鴻蒙HarmonyOS技術(shù)社區(qū)

2、設(shè)置對(duì)齊方式

通過(guò)屬性 ohos:alignment_type 來(lái)設(shè)置對(duì)齊方式,如下:

  1. <TableLayout 
  2.     ... 
  3.     ohos:alignment_type="align_contents"
  4.     ... 
  5. </TableLayout> 

四、StackLayout

StackLayout 直接在屏幕上開(kāi)辟出一塊空白的區(qū)域,添加到這個(gè)布局中的視圖都是以層疊的方式顯示,而它會(huì)把這些視圖默認(rèn)放到這塊區(qū)域的左上角,第一個(gè)添加到布局中視圖顯示在最底層,最后一個(gè)被放在最頂層。上一層的視圖會(huì)覆蓋下一層的視圖。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <StackLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:id="$+id:stack_layout" 
  5.     ohos:height="match_parent" 
  6.     ohos:width="match_parent"
  7.  
  8.     <Text 
  9.         ohos:id="$+id:text_blue" 
  10.         ohos:text_alignment="bottom|horizontal_center" 
  11.         ohos:text_size="24fp" 
  12.         ohos:text="第一層" 
  13.         ohos:height="400vp" 
  14.         ohos:width="400vp" 
  15.         ohos:background_element="#3F56EA" /> 
  16.  
  17.     <Text 
  18.         ohos:id="$+id:text_light_purple" 
  19.         ohos:text_alignment="bottom|horizontal_center" 
  20.         ohos:text_size="24fp" 
  21.         ohos:text="第二層" 
  22.         ohos:height="300vp" 
  23.         ohos:width="300vp" 
  24.         ohos:background_element="#00AAEE" /> 
  25.  
  26.     <Text 
  27.         ohos:id="$+id:text_orange" 
  28.         ohos:text_alignment="center" 
  29.         ohos:text_size="24fp" 
  30.         ohos:text="第三層" 
  31.         ohos:height="80vp" 
  32.         ohos:width="80vp" 
  33.         ohos:background_element="#00BFC9" /> 
  34. </StackLayout> 

以上代碼效果圖如下:

【中軟國(guó)際】HarmonyOS 基礎(chǔ)之 UI 布局(一)-鴻蒙HarmonyOS技術(shù)社區(qū)

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

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

https://harmonyos.51cto.com

 

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

2020-11-17 11:48:44

HarmonyOS

2020-11-25 12:02:02

TableLayout

2020-11-30 14:09:17

HarmonyOS

2021-10-14 15:14:36

鴻蒙HarmonyOS應(yīng)用

2021-09-14 09:34:05

鴻蒙HarmonyOS應(yīng)用

2012-05-14 21:08:47

Android頁(yè)面布局

2021-01-20 13:50:36

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

2020-11-18 09:58:53

鴻蒙

2021-08-30 18:34:35

鴻蒙HarmonyOS應(yīng)用

2018-06-08 15:28:31

Android開(kāi)發(fā)程序

2011-06-24 16:27:41

QML UI

2021-08-27 07:13:52

UI計(jì)算機(jī)圖形

2021-09-09 14:49:26

鴻蒙HarmonyOS應(yīng)用

2021-08-16 14:45:38

鴻蒙HarmonyOS應(yīng)用

2009-06-09 10:24:35

NetBeansStruts頁(yè)面布局

2013-06-17 16:12:23

iOS 7人機(jī)交互UI設(shè)計(jì)基礎(chǔ)

2015-06-24 10:17:24

UI流式布局

2013-01-08 16:05:23

Android開(kāi)發(fā)布局ViewStub

2013-07-24 18:14:36

Android開(kāi)發(fā)學(xué)習(xí)Android UIButton

2010-08-05 13:27:06

Flex布局
點(diǎn)贊
收藏

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