DevEco布局(笔记)

关于对于组件的放置 

一、线性布局

权重

在线性布局中,是按照百分比来的。每一个组件都有一个权重,将所有权重加起来,计算出每一个组件所占百分比。随后,计算父布局的剩余空间,通过百分比来分配其各个组件所占空间。

  • 当布局顺序为从上至下(垂直布局 ),只有高度按照百分比布局

  • 当布局顺序为从左到右(水平布局),只有宽度按照百分比布局

父布局可用空间=父布局长度或宽度 - 所有固定的长度或宽度

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">
    
    <Text
        ohos:height="0vp"
        ohos:width="100vp"
        ohos:background_element="#FF0000"
        ohos:weight="1"
        />
    <Text
        ohos:height="0vp"
        ohos:width="100vp"
        ohos:background_element="#00FF00"
        ohos:weight="2"
        />
    <Text
        ohos:height="0vp"
        ohos:width="100vp"
        ohos:background_element="#0000FF"
        ohos:weight="1"
        />
    <Text
        ohos:height="100vp"
        ohos:width="100vp"
        ohos:background_element="#000000"
        />
</DirectionalLayout>

电话按钮的布局实例

ability_main.xml

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">
    <Text
        ohos:height="50vp"
        ohos:width="200vp"
        ohos:text_size="20fp"
        ohos:top_margin="30vp"
        ohos:layout_alignment="horizontal_center"
        ohos:background_element="$graphic:text_empty"/>
    <DirectionalLayout
          ohos:height="match_content"
          ohos:width="match_parent"
          ohos:alignment="horizontal_center"
          ohos:orientation="horizontal"
          ohos:top_margin="10vp">
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="1"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="2"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="3"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
        </DirectionalLayout>
        <DirectionalLayout
            ohos:height="match_content"
            ohos:width="match_parent"
            ohos:alignment="horizontal_center"
            ohos:orientation="horizontal"
            ohos:top_margin="10vp">
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="4"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="5"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="6"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
        </DirectionalLayout>
        <DirectionalLayout
              ohos:height="match_content"
              ohos:width="match_parent"
              ohos:alignment="horizontal_center"
              ohos:orientation="horizontal"
              ohos:top_margin="10vp">
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="7"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="8"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="9"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
        </DirectionalLayout>
        <DirectionalLayout
              ohos:height="match_content"
              ohos:width="match_parent"
              ohos:alignment="horizontal_center"
              ohos:orientation="horizontal"
              ohos:top_margin="10vp">
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="#"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="0"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
          <Button
              ohos:height="70vp"
              ohos:width="70vp"
              ohos:text="*"
              ohos:text_size="35fp"
              ohos:text_color="black"
              ohos:text_alignment="center"
              ohos:background_element="$graphic:button_state"/>
      </DirectionalLayout>
    <Button
        ohos:height="50vp"
        ohos:width="200vp"
        ohos:text="CALL"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="$graphic:text_empty"
        ohos:text_alignment="center"
        ohos:top_margin="50vp"
        ohos:layout_alignment="horizontal_center"
        />
</DirectionalLayout>

 text_empty.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    ohos:shape="rectangle"
    xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <stroke
        ohos:color="#000000"
        ohos:width="3vp"/>
    <corners
        ohos:radius="15vp"/>
</shape>

button_state.xml

<?xml version="1.0" encoding="utf-8"?>
<state-container
    xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <item ohos:state="component_state_pressed" ohos:element="$graphic:button_press"/>
    <item ohos:state="component_state_empty" ohos:element="$graphic:button_empty"/>
</state-container>

 button_press.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    ohos:shape="oval"
    xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <stroke
        ohos:color="#000000"
        ohos:width="3vp"/>
    <solid ohos:color="#00FF00"/>
</shape>

 button_empty.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    ohos:shape="oval"
    xmlns:ohos="http://schemas.huawei.com/res/ohos">
    <stroke
        ohos:color="#000000"
        ohos:width="3vp"/>
</shape>

二、相对布局

  • 基准位置
  • 任意组件是相对其他组件而言

基准位置

一般而言以左上角为基准位置

  • ohos:alignment 用于设置基准位置
  • alignment=“center”:基准位置在正中央
  • alignment=“horizontal_center”:基准位置在最上方的中央
  • alignment="vertical_center":基准位置在最左侧的中央
  • alignment="top"/alignment="left"/不设置:基准位置在左上角
  • alignment="right":基准位置在右上角
  • alignment="bottom":基准位置在左下角
  • alignment="bottom|right":基准位置在右下角

放置的话一般用right_of和below,放置在上一个组件的相对位置。

<?xml version="1.0" encoding="utf-8"?>
<DependentLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="top"
    >
    <Text
        ohos:id="$+id:text1"
        ohos:height="100vp"
        ohos:width="100vp"
        ohos:text="text1"
        ohos:text_size="20fp"
        ohos:background_element="#21A8FD"/>
    <Text
        ohos:height="100vp"
        ohos:width="100vp"
        ohos:text="text2"
        ohos:text_size="20fp"
        ohos:background_element="#FF1234"
        ohos:right_of="$id:text1"/>
</DependentLayout>

三、表格布局

  • row_count行数
  • column_count列数
  • orientation摆放顺序
  • alignment_type对齐方式
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical"
    ohos:row_count="3"
    ohos:column_count="3"
    >
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="1"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#21A8FD"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="2"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#0000FF"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="3"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#00FF00"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="4"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#FF0000"/>
</TableLayout>

对齐方式

默认方式,ohos:alignment_type="align_contents"

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="horizontal"
    ohos:row_count="3"
    ohos:column_count="3"
    ohos:alignment_type="align_contents"
    >
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="1"
        ohos:text_size="35fp"
        ohos:left_margin="50vp"
        ohos:text_color="#000000"
        ohos:background_element="#21A8FD"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="2"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#0000FF"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="3"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#00FF00"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="4"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#FF0000"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="5"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#0FFFF0"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="6"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#FF0FF0"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="1"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#21A8FD"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="2"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#0000FF"/>
    <Text
        ohos:height="50vp"
        ohos:width="50vp"
        ohos:text="3"
        ohos:text_size="35fp"
        ohos:text_color="#000000"
        ohos:background_element="#00FF00"/>
</TableLayout>

 修改ohos:alignment_type="align_edges"

其他布局

  • 层叠布局
  • 绝对布局 
  • 自适应盒子布局
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值