树叶云鸿蒙OS教程:鸿蒙OS DependentLayout

DependentLayout 是 Java UI 系统里的一种常见布局。与 DirectionalLayout 相比,拥有更多的排布方式,每个组件可以指定相对于其他同级元素的位置,或者指定相对于父组件的位置。

图1 DependentLayout 示意图

排列方式

DependentLayout 的排列方式是相对于其他同级组件或者父组件的位置进行布局。

  • 相对于同级组件

相对于同级组件的位置布局见[表1]。

位置布局描述
above处于同级组件的上侧。
below处于同级组件的下侧。
start_of处于同级组件的起始侧。
end_of处于同级组件的结束侧。
left_of处于同级组件的左侧。
right_of处于同级组件的右侧。

end_of:

  <?xml version="1.0" encoding="utf-8"?>
  <DependentLayout
      xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:width="match_content"
      ohos:height="match_content"
      ohos:background_element="$graphic:color_light_gray_element">
      <Text
          ohos:id="$+id:text1"
          ohos:width="match_content"
          ohos:height="match_content"
          ohos:left_margin="15vp"
          ohos:top_margin="15vp"
          ohos:bottom_margin="15vp"
          ohos:text="text1"
          ohos:text_size="20fp"
          ohos:background_element="$graphic:color_cyan_element"/>
      <Text
          ohos:id="$+id:text2"
          ohos:width="match_content"
          ohos:height="match_content"
          ohos:left_margin="15vp"
          ohos:top_margin="15vp"
          ohos:right_margin="15vp"
          ohos:bottom_margin="15vp"
          ohos:text="end_of text1"
          ohos:text_size="20fp"
          ohos:background_element="$graphic:color_cyan_element"
          ohos:end_of="$id:text1"/>
  </DependentLayout>

color_light_gray_element.xml:

  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:shape="rectangle">
      <solid
          ohos:color="#ffeeeeee"/>
  </shape>

color_cyan_element.xml:

  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:shape="rectangle">
      <solid
          ohos:color="#ff00ffff"/>
  </shape>

below:

  <?xml version="1.0" encoding="utf-8"?>
  <DependentLayout
      xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:width="match_content"
      ohos:height="match_content"
      ohos:background_element="$graphic:color_light_gray_element">
      <Text
          ohos:id="$+id:text1"
          ohos:width="match_content"
          ohos:height="match_content"
          ohos:left_margin="15vp"
          ohos:top_margin="15vp"
          ohos:right_margin="40vp"
          ohos:text="text1"
          ohos:text_size="20fp"
          ohos:background_element="$graphic:color_cyan_element"/>
      <Text
          ohos:id="$+id:text3"
          ohos:width="match_content"
          ohos:height="match_content"
          ohos:left_margin="15vp"
          ohos:top_margin="15vp"
          ohos:right_margin="40vp"
          ohos:bottom_margin="15vp"
          ohos:text="below text1"
          ohos:text_size="20fp"
          ohos:background_element="$graphic:color_cyan_element"
          ohos:below="$id:text1"/>
  </DependentLayout>

color_light_gray_element.xml:

  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:shape="rectangle">
      <solid
          ohos:color="#ffeeeeee"/>
  </shape>

color_cyan_element.xml:

  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:shape="rectangle">
      <solid
          ohos:color="#ff00ffff"/>
  </shape>

其他的 above、start_of、left_of、right_of 等参数可分别实现类似的布局。

  • 相对于父组件

相对于父组件的位置布局见[表2]。

位置布局描述
align_parent_left处于父组件的左侧。
align_parent_right处于父组件的右侧。
align_parent_start处于父组件的起始侧。
align_parent_end处于父组件的结束侧。
align_parent_top处于父组件的上侧。
align_parent_bottom处于父组件的下侧。
center_in_parent处于父组件的中间。

以上位置布局可以组合,形成处于左上角、左下角、右上角、右下角的布局。

  <?xml version="1.0" encoding="utf-8"?>
  <DependentLayout
      xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:width="300vp"
      ohos:height="100vp"
      ohos:background_element="$graphic:color_background_gray_element">
      <Text
          ohos:id="$+id:text6"
          ohos:width="match_content"
          ohos:height="match_content"
          ohos:text="align_parent_right"
          ohos:text_size="12fp"
          ohos:background_element="$graphic:color_cyan_element"
          ohos:align_parent_right="true"
          ohos:center_in_parent="true"/>
      <Text
          ohos:id="$+id:text7"
          ohos:width="match_content"
          ohos:height="match_content"
          ohos:text="align_parent_bottom"
          ohos:text_size="12fp"
          ohos:background_element="$graphic:color_cyan_element"
          ohos:align_parent_bottom="true"
          ohos:center_in_parent="true"/>
      <Text
          ohos:id="$+id:text8"
          ohos:width="match_content"
          ohos:height="match_content"
          ohos:text="center_in_parent"
          ohos:text_size="12fp"
          ohos:background_element="$graphic:color_cyan_element"
          ohos:center_in_parent="true"/>
      <Text
          ohos:id="$+id:text9"
          ohos:width="match_content"
          ohos:height="match_content"
          ohos:text="align_parent_left_top"
          ohos:text_size="12fp"
          ohos:background_element="$graphic:color_cyan_element"
          ohos:align_parent_left="true"
          ohos:align_parent_top="true"/>
  </DependentLayout>

color_background_gray_element.xml:

  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:shape="rectangle">
      <solid
          ohos:color="#ffbbbbbb"/>
  </shape>

color_cyan_element.xml:

  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
      ohos:shape="rectangle">
      <solid
          ohos:color="#ff00ffff"/>
  </shape>

场景示例

使用 DependentLayout 可以轻松实现内容丰富的布局。

<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:width="match_parent"
    ohos:height="match_content"
    ohos:background_element="$graphic:color_background_gray_element">
    <Text
        ohos:id="$+id:text1"
        ohos:width="match_parent"
        ohos:height="match_content"
        ohos:text_size="25fp"
        ohos:top_margin="15vp"
        ohos:left_margin="15vp"
        ohos:right_margin="15vp"
        ohos:background_element="$graphic:color_gray_element"
        ohos:text="Title"
        ohos:text_weight="1000"
        ohos:text_alignment="horizontal_center"
    />
    <Text
        ohos:id="$+id:text2"
        ohos:width="match_content"
        ohos:height="120vp"
        ohos:text_size="10vp"
        ohos:background_element="$graphic:color_gray_element"
        ohos:text="Catalog"
        ohos:top_margin="15vp"
        ohos:left_margin="15vp"
        ohos:right_margin="15vp"
        ohos:bottom_margin="15vp"
        ohos:align_parent_left="true"
        ohos:text_alignment="center"
        ohos:multiple_lines="true"
        ohos:below="$id:text1"
        ohos:text_font="serif"/>
    <Text
        ohos:id="$+id:text3"
        ohos:width="match_parent"
        ohos:height="120vp"
        ohos:text_size="25fp"
        ohos:background_element="$graphic:color_gray_element"
        ohos:text="Content"
        ohos:top_margin="15vp"
        ohos:right_margin="15vp"
        ohos:bottom_margin="15vp"
        ohos:text_alignment="center"
        ohos:below="$id:text1"
        ohos:end_of="$id:text2"
        ohos:text_font="serif"/>
    <Button
        ohos:id="$+id:button1"
        ohos:width="70vp"
        ohos:height="match_content"
        ohos:text_size="15fp"
        ohos:background_element="$graphic:color_gray_element"
        ohos:text="Previous"
        ohos:right_margin="15vp"
        ohos:bottom_margin="15vp"
        ohos:below="$id:text3"
        ohos:left_of="$id:button2"
        ohos:italic="false"
        ohos:text_weight="5"
        ohos:text_font="serif"/>
    <Button
        ohos:id="$+id:button2"
        ohos:width="70vp"
        ohos:height="match_content"
        ohos:text_size="15fp"
        ohos:background_element="$graphic:color_gray_element"
        ohos:text="Next"
        ohos:right_margin="15vp"
        ohos:bottom_margin="15vp"
        ohos:align_parent_end="true"
        ohos:below="$id:text3"
        ohos:italic="false"
        ohos:text_weight="5"
        ohos:text_font="serif"/>
</DependentLayout>

color_background_gray_element.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:shape="rectangle">
    <solid
        ohos:color="#ffbbbbbb"/>
</shape>

color_gray_element.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:shape="rectangle">
    <solid
        ohos:color="#ff888888"/>
</shape>

文章来源网络,作者:管理,如若转载,请注明出处:https://shuyeidc.com/wp/215783.html<

(0)
管理的头像管理
上一篇2025-04-12 10:08
下一篇 2025-04-12 10:10

相关推荐

  • 云服务器和云虚拟主机怎么选?云服务器和虚拟主机区别

    云服务器适合业务增长快、需弹性扩展的场景,而云虚拟主机适合预算有限、技术门槛低的小型静态网站或测试环境,二者核心区别在于资源独享性与运维复杂度,核心差异解析:从底层架构到使用体验很多人容易混淆这两者,觉得它们都是“买空间建站”,它们的底层逻辑完全不同,云服务器(ECS)就像是你租了一整栋别墅,水电网络独立,你想……

    2026-06-29
    0
  • 赣州智慧旅游招聘是真的吗?赣州旅游人才招聘信息

    中级岗位(3-5年经验)月薪范围通常在6000-10000元,这类岗位需要独立负责项目模块,如独立运营一个抖音账号,或维护一个景区小程序的功能迭代,具备成功案例的候选人议价能力较强,高级岗位(5年以上经验)月薪范围通常在10000-20000元,部分核心管理岗可达更高,这类人才需要具备战略规划能力,如制定整个景……

    2026-06-29
    0
  • 赣州智能物联网车位锁如何管理?智能车位锁管理系统多少钱

    赣州智能物联网车位锁管理的核心在于通过云端平台实现远程控锁、状态实时监控及自动计费,彻底解决传统车位“被占难管”与“找位难”的痛点,在赣州这样的城市,随着机动车保有量的持续增长,老旧小区、商业综合体以及私人固定车位的资源矛盾日益凸显,传统的机械地锁或简易遥控锁,不仅操作繁琐,更无法实现数据化管理,引入智能物联网……

    2026-06-29
    0
  • 赣州智能消防栓好用吗,智能消防栓多少钱一个

    赣州智能消防栓通过物联网技术实现实时监测与远程报警,能显著降低火灾响应时间并提升城市消防安全管理水平,是目前智慧城市建设中不可或缺的基础设施,赣州智能消防栓的核心价值与应用场景传统消防栓往往存在“看不见、摸不着、用不了”的痛点,在赣州这样地形复杂、老城区与新城区并存的区域,传统设施的管理难度极大,智能消防栓的出……

    2026-06-29
    0
  • 云服务器和物理机到底有啥区别?

    云服务器本质上是虚拟化资源池中的弹性实例,而传统物理服务器是独占的硬件实体,前者胜在弹性与运维便捷,后者强在物理隔离与性能稳定,具体选择取决于业务对成本、扩展性及安全合规的权衡,很多人初次接触服务器时,容易把“云服务器”和“传统物理服务器”混为一谈,觉得它们都是用来跑网站或存数据的盒子,这两者的底层逻辑完全不同……

    2026-06-29
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注