偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

[[417158]]

想了解更多內(nèi)容,請訪問:

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

https://harmonyos.51cto.com

概述

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

常用布局

一、DirectionalLayout 布局

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

​ 水平排列和垂直排列通過設(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> 

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

【中軟國際】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> 

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

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

3、對齊方式

DirectionalLayout 中的組件使用 layout_alignment 控制自身在布局中的對齊方式,當對齊方式與排列方式方向一致時,對齊方式不會生效具體見下表。

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

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

布局展示的樣式為:

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

4、權(quán)重

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

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

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

二、DependentLayout 布局

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

1、相對于同級組件的位置布局

2、相對于父組件的位置布局

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

三、TableLayout 布局

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

1、行列的設(shè)置

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

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

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

通過屬性 ohos:alignment_type 來設(shè)置對齊方式,如下:

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

四、StackLayout

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

  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> 

以上代碼效果圖如下:

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

想了解更多內(nèi)容,請訪問:

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

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區(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頁面布局

2021-01-20 13:50:36

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

2020-11-18 09:58:53

鴻蒙

2011-06-24 16:27:41

QML UI

2021-08-30 18:34:35

鴻蒙HarmonyOS應(yīng)用

2018-06-08 15:28:31

Android開發(fā)程序

2021-08-27 07:13:52

UI計算機圖形

2021-09-09 14:49:26

鴻蒙HarmonyOS應(yīng)用

2021-09-16 10:05:09

鴻蒙HarmonyOS應(yīng)用

2021-09-30 10:04:01

鴻蒙HarmonyOS應(yīng)用

2015-06-24 10:17:24

UI流式布局

2013-01-08 16:05:23

Android開發(fā)布局ViewStub

2021-08-16 14:45:38

鴻蒙HarmonyOS應(yīng)用

2021-10-18 10:14:26

鴻蒙HarmonyOS應(yīng)用

2009-06-09 10:24:35

NetBeansStruts頁面布局
點贊
收藏

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