HarmonyOS开始玩耍:设计支持多种场景(三)

59 阅读1分钟

在 HarmonyOS(鸿蒙操作系统)中编写布局,你将使用类似于 XML 的声明式 UI 语言来定义用户界面。HarmonyOS 的 UI 设计支持多种场景,包括但不限于:

  1. 单列布局:适用于列表和表单等垂直排列的界面。

  2. 网格布局:适用于图标和宫格等需要网格排列的界面。

  3. 水平布局:适用于需要水平排列元素的界面。

  4. 堆叠布局:适用于层叠视图或卡片堆叠的界面。

  5. 自适应布局:适用于响应式设计,能够适应不同屏幕尺寸和方向。

  6. 分栏布局:适用于需要将内容分为多栏的界面。

  7. 导航栏和底部导航:适用于带有导航功能的界面。

  8. 对话框和弹出窗口:适用于需要模态交互的界面。

场景案例

1. 单列列表布局

<!-- 单列列表布局 -->
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <DirectionalLayout ohos:id="$+id:main_content" ohos:height="match_content" ohos:width="match_parent">
        <DirectionalLayout ohos:height="50vp" ohos:width="match_parent" ohos:background_element="$media:background_element">
            <Text ohos:id="$+id:text_item" ohos:height="match_content" ohos:width="match_content" ohos:text="$string:text_item" ohos:text_size="16fp"/>
        </DirectionalLayout>
        <!-- 可以添加更多列表项 -->
    </DirectionalLayout>
</DirectionalLayout>

2. 网格布局

<!-- 网格布局 -->
<DependentLayout xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <GridLayout ohos:id="$+id:grid_layout" ohos:height="match_content" ohos:width="match_parent" ohos:alignment="horizontal" ohos:column_count="3">
        <Image ohos:id="$+id:image1" ohos:height="100vp" ohos:width="100vp" ohos:background_element="$media:image1"/>
        <Image ohos:id="$+id:image2" ohos:height="100vp" ohos:width="100vp" ohos:background_element="$media:image2"/>
        <Image ohos:id="$+id:image3" ohos:height="100vp" ohos:width="100vp" ohos:background_element="$media:image3"/>
        <!-- 可以添加更多宫格项 -->
    </GridLayout>
</DependentLayout>

3. 水平布局

<!-- 水平布局 -->
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <Image ohos:id="$+id:image_left" ohos:height="50vp" ohos:width="50vp" ohos:background_element="$media:image_left"/>
    <Text ohos:id="$+id:text" ohos:height="50vp" ohos:width="match_parent" ohos:text="$string:text" ohos:text_size="16fp" ohos:multiple_lines="true" ohos:left_padding="5vp"/>
</DirectionalLayout>

4. 自适应布局

<!-- 自适应布局 -->
<DependentLayout xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <Text ohos:id="$+id:text" ohos:height="wrap_content" ohos:width="match_parent" ohos:text="$string:text" ohos:text_size="16fp" ohos:multiple_lines="true"/>
    <Image ohos:id="$+id:image" ohos:height="wrap_content" ohos:width="match_content" ohos:above="$id:text" ohos:alignment="horizontal_center"/>
</DependentLayout>

5. 分栏布局

<!-- 分栏布局 -->
<TableLayout xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <TableRow>
        <Text ohos:id="$+id:text1" ohos:height="wrap_content" ohos:width="match_content" ohos:text="$string:text1"/>
        <Text ohos:id="$+id:text2" ohos:height="wrap_content" ohos:width="match_content" ohos:text="$string:text2"/>
    </TableRow>
    <!-- 可以添加更多行 -->
</TableLayout>

6. 导航栏和底部导航

<!-- 导航栏 -->
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <NavigationLayout ohos:id="$+id:nav_layout" ohos:height="50vp" ohos:width="match_parent">
        <Text ohos:id="$+id:text" ohos:height="match_content" ohos:width="match_content" ohos:text="$string:text"/>
    </NavigationLayout>
    <!-- 底部导航 -->
    <DirectionalLayout ohos:id="$+id:bottom_nav" ohos:height="50vp" ohos:width="match_parent" ohos:bottom="0vp">
        <Text ohos:id="$+id:bottom_text" ohos:height="match_content" ohos:width="match_content" ohos:text="$string:bottom_text"/>
    </DirectionalLayout>
</DirectionalLayout>

这些案例展示了如何在 HarmonyOS 中使用不同的布局来构建常见的用户界面。在实际开发中,你可能需要根据应用的具体需求和设计来选择合适的布局。

我的博客只写前端博文,点击我去看更多喜欢的前端博文,欢迎大家一起讨论学习!【https://blog.csdn.net/qq_29101285?spm=1011.2266.3001.5343】