鸿蒙HarmonyOS Java UI之DirectionalLayout布局

开发 后端 OpenHarmony
从本节开始,将使用XML构建UI界面。使用XML构建UI(默认你已经会在项目中创建XML布局文件)界面相对Java代码构建的好处是:结构清晰,代码简洁。

[[352667]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

在之前的章节中我使用的是Java 代码构建UI界面,从本节开始,将使用XML构建UI界面。

使用XML构建UI(默认你已经会在项目中创建XML布局文件)界面相对Java代码构建的好处是:结构清晰,代码简洁。

DirectionalLayout(单一方向排列布局)是Java UI的一种重要的组件布局,用于将一组组件按照水平或垂直方向排布,能够方便地对齐布局内的组件。与Android中的线性布局相似。可以通过设置orientation属性来控制组件的排列方式,默认为垂直排列。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.                    ohos:width="match_parent" 
  4.                    ohos:height="match_parent" 
  5.                    ohos:background_element="#FFCCCCCC" 
  6.                    ohos:orientation="vertical"
  7.     <Text 
  8.         ohos:id="$+id:txtOne" 
  9.         ohos:width="match_content" 
  10.         ohos:height="match_content" 
  11.         ohos:padding="20vp" 
  12.         ohos:margin="20vp" 
  13.         ohos:text_size="30vp" 
  14.         ohos:text_color="#FFFFFFFF" 
  15.         ohos:background_element="#FFFF00FF" 
  16.         ohos:text="我是第一个"/> 
  17.  
  18.     <Text 
  19.         ohos:id="$+id:txtTwo" 
  20.         ohos:width="match_content" 
  21.         ohos:height="match_content" 
  22.         ohos:padding="20vp" 
  23.         ohos:margin="20vp" 
  24.         ohos:text_size="30vp" 
  25.         ohos:text_color="#FFFFFFFF" 
  26.         ohos:background_element="#FFFF00FF" 
  27.         ohos:text="我是第二个"/> 
  28.  
  29.     <Text 
  30.         ohos:id="$+id:txtThird" 
  31.         ohos:width="match_content" 
  32.         ohos:height="match_content" 
  33.         ohos:padding="20vp" 
  34.         ohos:margin="20vp" 
  35.         ohos:text_size="30vp" 
  36.         ohos:text_color="#FFFFFFFF" 
  37.         ohos:background_element="#FFFF00FF" 
  38.         ohos:text="我是第三个"/> 
  39.  
  40. </DirectionalLayout> 

 

将上面代码中的ohos:orientation="vertical"换成ohos:orientation="horizontal" 然后运行查看效果如下所示。

DirectionalLayout布局中的组件不会自动换行,会按照设定的方向依次排列,若超出布局本身大小,超出布局大小的部分将不会显示。我们将上面示例代码中的Text组件宽度设定为400vp,然后运行效果如下所示,我们可以看到,第三个Text组件显示了一部分。

DirectionalLayout中的组件使用layout_alignment控制自身在布局中的对齐方式,当对齐方式与排列方式一致时,对齐方式不会生效,如布局为水平方法排列,则其下组件左对齐、右对齐将不会生效。因为布局中可以嵌套布局来丰富UI样式,我们可以使用这个方式来演示一下对齐样式。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.                    ohos:width="match_parent" 
  4.                    ohos:height="match_parent" 
  5.                    ohos:orientation="vertical"
  6.     <DirectionalLayout 
  7.             ohos:width="match_parent" 
  8.             ohos:height="0vp" 
  9.             ohos:weight="2" 
  10.             ohos:margin="10vp" 
  11.             ohos:background_element="#FFDDDDDD" 
  12.             ohos:orientation="vertical"
  13.         <Text 
  14.                 ohos:width="match_content" 
  15.                 ohos:height="match_content" 
  16.                 ohos:padding="10vp" 
  17.                 ohos:margin="10vp" 
  18.                 ohos:text_size="20vp" 
  19.                 ohos:text_color="#FFFFFFFF" 
  20.                 ohos:layout_alignment="left" 
  21.                 ohos:background_element="#FFFF00FF" 
  22.                 ohos:text="左对齐"/> 
  23.  
  24.         <Text 
  25.                 ohos:width="match_content" 
  26.                 ohos:height="match_content" 
  27.                 ohos:padding="10vp" 
  28.                 ohos:margin="10vp" 
  29.                 ohos:text_size="20vp" 
  30.                 ohos:text_color="#FFFFFFFF" 
  31.                 ohos:layout_alignment="horizontal_center" 
  32.                 ohos:background_element="#FFFF00FF" 
  33.                 ohos:text="水平方向居中"/> 
  34.  
  35.         <Text 
  36.                 ohos:width="match_content" 
  37.                 ohos:height="match_content" 
  38.                 ohos:padding="10vp" 
  39.                 ohos:margin="10vp" 
  40.                 ohos:text_size="20vp" 
  41.                 ohos:text_color="#FFFFFFFF" 
  42.                 ohos:layout_alignment="right" 
  43.                 ohos:background_element="#FFFF00FF" 
  44.                 ohos:text="右对齐"/> 
  45.         <Text 
  46.                 ohos:width="match_content" 
  47.                 ohos:height="match_content" 
  48.                 ohos:padding="10vp" 
  49.                 ohos:margin="10vp" 
  50.                 ohos:text_size="20vp" 
  51.                 ohos:text_color="#FFFFFFFF" 
  52.                 ohos:layout_alignment="center" 
  53.                 ohos:background_element="#FFFF00FF" 
  54.                 ohos:text="垂直与水平方向都居中"/> 
  55.     </DirectionalLayout> 
  56.     <DirectionalLayout 
  57.             ohos:width="match_parent" 
  58.             ohos:height="0vp" 
  59.             ohos:margin="10vp" 
  60.             ohos:weight="1" 
  61.             ohos:background_element="#FFCCCCCC" 
  62.             ohos:orientation="horizontal"
  63.         <Text 
  64.                 ohos:width="match_content" 
  65.                 ohos:height="match_content" 
  66.                 ohos:padding="10vp" 
  67.                 ohos:margin="10vp" 
  68.                 ohos:text_size="20vp" 
  69.                 ohos:text_color="#FFFFFFFF" 
  70.                 ohos:layout_alignment="top" 
  71.                 ohos:background_element="#FFFF00FF" 
  72.                 ohos:text="顶部对齐"/> 
  73.  
  74.         <Text 
  75.                 ohos:width="match_content" 
  76.                 ohos:height="match_content" 
  77.                 ohos:padding="10vp" 
  78.                 ohos:margin="10vp" 
  79.                 ohos:text_size="20vp" 
  80.                 ohos:text_color="#FFFFFFFF" 
  81.                 ohos:layout_alignment="vertical_center" 
  82.                 ohos:background_element="#FFFF00FF" 
  83.                 ohos:text="垂直方向居中"/> 
  84.  
  85.         <Text 
  86.                 ohos:width="match_content" 
  87.                 ohos:height="match_content" 
  88.                 ohos:padding="10vp" 
  89.                 ohos:margin="10vp" 
  90.                 ohos:text_size="20vp" 
  91.                 ohos:text_color="#FFFFFFFF" 
  92.                 ohos:layout_alignment="bottom" 
  93.                 ohos:background_element="#FFFF00FF" 
  94.                 ohos:text="底部对齐"/> 
  95.         <Text 
  96.                 ohos:width="match_content" 
  97.                 ohos:height="match_content" 
  98.                 ohos:padding="10vp" 
  99.                 ohos:margin="10vp" 
  100.                 ohos:text_size="20vp" 
  101.                 ohos:text_color="#FFFFFFFF" 
  102.                 ohos:layout_alignment="center" 
  103.                 ohos:background_element="#FFFF00FF" 
  104.                 ohos:text="垂直与水平方向都居中"/> 
  105.     </DirectionalLayout> 
  106. </DirectionalLayout> 

 

在上面代码中我们看到两个DirectionalLayout子布局中有ohos:weight="1"属性,这个属性就是设置组件在布局中的权重,按照比例来分配组件占用父组件的大小。

DirectionalLayout布局需要掌握的知识点也就这么多,接下来再说说题外话。

设置UI显示界面问题

我们使用XML方式构建UI,在AbilitySlice中设置界面入口的时候,一般会报错,找不到布局文件。官方推荐使用Build -> Build App(s)/Hap(s) > Build Debug Hap(s)重新编译一次即可。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2020-11-25 12:02:02

TableLayout

2021-08-12 15:01:09

鸿蒙HarmonyOS应用

2020-11-30 14:09:17

HarmonyOS

2021-01-20 13:50:36

鸿蒙HarmonyOS应用开发

2021-05-19 08:41:11

鸿蒙HarmonyOS应用

2021-08-30 18:34:35

鸿蒙HarmonyOS应用

2021-09-14 09:34:05

鸿蒙HarmonyOS应用

2021-05-17 14:37:02

鸿蒙HarmonyOS应用

2021-10-18 10:14:26

鸿蒙HarmonyOS应用

2021-10-14 15:14:36

鸿蒙HarmonyOS应用

2021-08-16 14:45:38

鸿蒙HarmonyOS应用

2011-06-24 16:27:41

QML UI

2021-06-29 14:48:58

鸿蒙HarmonyOS应用

2021-07-01 09:19:56

鸿蒙HarmonyOS应用

2020-11-18 09:58:53

鸿蒙

2021-01-04 11:44:05

鸿蒙HarmonyOSAbilitySlic

2021-07-27 11:39:40

鸿蒙HarmonyOS应用

2021-06-28 14:41:36

鸿蒙HarmonyOS应用

2021-06-25 15:04:31

鸿蒙HarmonyOS应用

2020-11-10 10:00:10

HarmonyOS
点赞
收藏

51CTO技术栈公众号