成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

HarmonyOS 基礎之 UI 布局(一)

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

[[417158]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

概述

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

常用布局

一、DirectionalLayout 布局

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

​ 水平排列和垂直排列通過設置 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 基礎之 UI 布局(一)-鴻蒙HarmonyOS技術社區

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 基礎之 UI 布局(一)-鴻蒙HarmonyOS技術社區

3、對齊方式

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

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

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

布局展示的樣式為:

【中軟國際】HarmonyOS 基礎之 UI 布局(一)-鴻蒙HarmonyOS技術社區

4、權重

權重( weight )就是按比例來分配組件占用父組件的大小,通過 ohos:weight 屬性來定義。布局計算公式為:組件寬度=組件weight/所有組件weight之和*父布局可分配寬度;如 ohos:weight 分別設置為 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 基礎之 UI 布局(一)-鴻蒙HarmonyOS技術社區

二、DependentLayout 布局

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

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

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

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

三、TableLayout 布局

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

1、行列的設置

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

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

2、設置對齊方式

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

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

四、StackLayout

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

  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 基礎之 UI 布局(一)-鴻蒙HarmonyOS技術社區

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

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應用

2021-09-14 09:34:05

鴻蒙HarmonyOS應用

2012-05-14 21:08:47

Android頁面布局

2020-11-18 09:58:53

鴻蒙

2021-01-20 13:50:36

鴻蒙HarmonyOS應用開發

2011-06-24 16:27:41

QML UI

2018-06-08 15:28:31

Android開發程序

2021-08-30 18:34:35

鴻蒙HarmonyOS應用

2021-08-27 07:13:52

UI計算機圖形

2021-09-09 14:49:26

鴻蒙HarmonyOS應用

2015-06-24 10:17:24

UI流式布局

2013-01-08 16:05:23

Android開發布局ViewStub

2009-06-09 10:24:35

NetBeansStruts頁面布局

2013-06-17 16:12:23

iOS 7人機交互UI設計基礎

2010-08-05 13:27:06

Flex布局

2011-06-03 09:05:18

Android iphone tab

2013-07-24 18:14:36

Android開發學習Android UIButton
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91视频大全 | 国产a视频 | 亚洲欧洲在线视频 | 久草在线 | 91porn成人精品 | 久草在线青青草 | 国产三级精品视频 | 精品伊人 | 国产精品一二区 | 精品乱码一区二区 | 精品中文在线 | 在线观看中文字幕 | 国产日韩亚洲欧美 | 粉嫩av在线 | 欧美久久一区二区三区 | 风间由美一区二区三区在线观看 | 久久久成人免费视频 | 在线观看成年视频 | 国产亚洲精品91 | 91视频一88av| 国产成人亚洲精品 | 国产高清在线 | 久久小视频| 亚洲国产aⅴ精品 | 亚洲黄色一区二区三区 | 老妇激情毛片免费 | 久久精品欧美一区二区三区麻豆 | 精品一区电影 | 精品福利在线 | 国产目拍亚洲精品99久久精品 | 国产四虎 | 午夜影视大全 | 美女爽到呻吟久久久久 | 免费观看的av | 国产综合网址 | 久久久久国产精品免费免费搜索 | 香蕉一区二区 | 日韩中文字幕在线观看视频 | 视频一区中文字幕 | 久久成人人人人精品欧 | 午夜视频网站 |