鸿蒙OS布局

DirectionalLayout 是 Java UI 中的一种重要组件布局,用于将一组组件(Component)按照水平或者垂直方向排布,能够方便地对齐布局内的组件。

DirectionalLayout 的排列方向(orientation)分为水平(horizontal)或者垂直(vertical)方向。使用 orientation 设置布局内组件的排列方式,默认为垂直排列。

其实相当于android的linerLayout(线性布局)

垂直布局:

<?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:orientation="vertical">

    <Button
        ohos:id="$+id:one"
        ohos:height="40vp"
        ohos:width="match_parent"
        ohos:background_element="$graphic:background_button"
        ohos:text_size="40px"
        ohos:margin="10px"
        ohos:text="第一个"></Button>
    <Button
        ohos:id="$+id:two"
        ohos:height="40vp"
        ohos:text_size="40px"
        ohos:width="match_parent"
        ohos:margin="10px"
        ohos:background_element="$graphic:background_button"
        ohos:text="第二个"></Button>
    <Button
        ohos:id="$+id:three"
        ohos:height="40vp"
        ohos:text_size="40px"
        ohos:width="match_parent"
        ohos:margin="10px"
        ohos:background_element="$graphic:background_button"
        ohos:text="第三个"></Button>

</DirectionalLayout>

水平布局:

<?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:orientation="horizontal">

    <Button
        ohos:id="$+id:one"
        ohos:height="40vp"
        ohos:width="match_parent"
        ohos:background_element="$graphic:background_button"
        ohos:text_size="40px"
        ohos:weight="1"
        ohos:margin="10px"
        ohos:text="第一个"></Button>
    <Button
        ohos:id="$+id:two"
        ohos:height="40vp"
        ohos:text_size="40px"
        ohos:width="match_parent"
        ohos:margin="10px"
        ohos:weight="1"
        ohos:background_element="$graphic:background_button"
        ohos:text="第二个"></Button>
    <Button
        ohos:id="$+id:three"
        ohos:height="40vp"
        ohos:text_size="40px"
        ohos:width="match_parent"
        ohos:weight="1"
        ohos:margin="10px"
        ohos:background_element="$graphic:background_button"
        ohos:text="第三个"></Button>

</DirectionalLayout>

对齐方式:

参数作用可搭配排列方式
left左对齐垂直排列
top顶部对齐水平排列
right右对齐垂直排列
bottom底部对齐水平排列
horizontal_center水平方向居中垂直排列
vertical_center垂直方向居中水平排列
center垂直与水平方向都居中水平/垂直排列

weight(权重):

权重(weight)就是按比例来分配组件占用父组件的大小,在水平布局下计算公式为:

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

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

weight=1:1:1

 

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

DependentLayout相当于android的RelativeLayout(相对布局)

位置布局描述
above处于同级组件的上侧。
below处于同级组件的下侧。
start_of处于同级组件的起始侧。
end_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处于父组件的中间。

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

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值