鸿蒙OS应用开发之——页面布局DirectionalLayout(1),2024年最新面试分享一次成功的经历

30 阅读1分钟

"deviceType": [

"tablet","phone"

],

三 DirectionalLayout排列


3.1 垂直排列

效果图

代码文件

布局文件

<DirectionalLayout

xmlns:ohos="schemas.huawei.com/res/ohos"

ohos:width="match_parent"

ohos:height="match_content"

ohos:orientation="vertical">

<Button

ohos:width="33vp"

ohos:height="20vp"

ohos:bottom_margin="3vp"

ohos:left_margin="13vp"

ohos:background_element="$graphic:color_cyan_element"

ohos:text="Button 1"/>

<Button

ohos:width="33vp"

ohos:height="20vp"

ohos:bottom_margin="3vp"

ohos:left_margin="13vp"

ohos:background_element="$graphic:color_cyan_element"

ohos:text="Button 2"/>

<Button

ohos:width="33vp"

ohos:height="20vp"

ohos:bottom_margin="3vp"

ohos:left_margin="13vp"

ohos:background_element="$graphic:color_cyan_element"

ohos:text="Button 3"/>

color_cyan_element.xml:

<shape xmlns:ohos="schemas.huawei.com/res/ohos"

ohos:shape="rectangle">

<solid

ohos:color="#00FFFD"/>

3.2 水平排列

效果图

代码

布局文件

<DirectionalLayout

xmlns:ohos="schemas.huawei.com/res/ohos"

ohos:width="match_parent"

ohos:height="match_content"

ohos:orientation="horizontal">

<Button

ohos:width="33vp"

ohos:height="20vp"

ohos:left_margin="13vp"

ohos:background_element="$graphic:color_cyan_element"

ohos:text="Button 1"/>

<Button

ohos:width="33vp"

ohos:height="20vp"

ohos:left_margin="13vp"

ohos:background_element="$graphic:color_cyan_element"

ohos:text="Button 2"/>

<Button

ohos:width="33vp"

ohos:height="20vp"

ohos:left_margin="13vp"

ohos:background_element="$graphic:color_cyan_element"

ohos:text="Button 3"/>

color_cyan_element.xml:

<shape xmlns:ohos="schemas.huawei.com/res/ohos"

ohos:shape="rectangle">

<solid

ohos:color="#00FFFD"/>

四 DirectionalLayout对齐方式


4.1 对齐方式属性表

| 参数 | 作用 | 可搭配排列方式 |

| :-: | :-: | :-: |

| left | 左对齐 | 垂直排列 |

| top | 顶部对齐 | 水平排列 |

| right | 右对齐 | 垂直排列 |

| bottom | 底部对齐 | 水平排列 |

| horizontal_center | 水平方向居中 | 垂直排列 |

| vertical_center | 垂直方向居中 | 水平排列 |

| center | 垂直与水平方向都居中 | 水平/垂直排列 |

4.2 实例

效果图

布局文件示例代码

<DirectionalLayout

xmlns:ohos="schemas.huawei.com/res/ohos"

ohos:width="match_parent"

ohos:height="60vp">

<Button

ohos:width="50vp"

ohos:height="20vp"

ohos:background_element="$graphic:color_cyan_element"

ohos:layout_alignment="left"

ohos:text="Button 1"/>

<Button

ohos:width="50vp"

ohos:height="20vp"

ohos:background_element="$graphic:color_cyan_element"

ohos:layout_alignment="horizontal_center"

ohos:text="Button 2"/>

<Button

ohos:width="50vp"

ohos:height="20vp"

ohos:background_element="$graphic:color_cyan_element"

ohos:layout_alignment="right"

ohos:text="Button 3"/>

color_cyan_element.xml:

<shape xmlns:ohos="schemas.huawei.com/res/ohos"

ohos:shape="rectangle">

<solid

ohos:color="#00FFFD"/>

五 权重


5.1 说明

  • 权重(weight)就是按比例来分配组件占用父组件的大小

  • 父布局可分配宽度=父布局宽度-所有子组件width之和;

  • 组件宽度=组件weight/所有组件weight之和*父布局可分配宽度;

5.2 实例

效果图

布局文件

<DirectionalLayout

xmlns:ohos="schemas.huawei.com/res/ohos"

ohos:width="match_parent"

ohos:height="match_content"

ohos:orientation="horizontal">

<Button

ohos:width="0vp"

ohos:height="20vp"

ohos:weight="1"

ohos:background_element="$graphic:color_cyan_element"

ohos:text="Button 1"/>

<Button

ohos:width="0vp"

ohos:height="20vp"

ohos:weight="1"

ohos:background_element="$graphic:color_gray_element"

ohos:text="Button 2"/>

img img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!