当前位置:   article > 正文

HarmonyOS之常用布局DirectionalLayout的使用_ohos.promptaction 居中显示

ohos.promptaction 居中显示
一、DirectionalLayout
  • DirectionalLayout 是 Java UI 中的一种重要组件布局,用于将一组组件(Component)按照水平或者垂直方向排布,能够方便地对齐布局内的组件。该布局和其他布局的组合,可以实现更加丰富的布局方式。
  • DirectionalLayout 示意如下:

在这里插入图片描述

二、支持的 XML 属性
属性名称中文描述取值取值说明使用案例
alignment对齐方式left表示左对齐可以设置取值项如表中所列,也可以使用“|”进行多项组合。
ohos:alignment="top|left"
ohos:alignment="left"
top表示顶部对齐
right表示右对齐
bottom表示底部对齐
horizontal_center表示水平居中对齐
vertical_center表示垂直居中对齐
center表示居中对齐
start表示靠起始端对齐
end表示靠结束端对齐
total_weight所有子视图的权重之和float类型可以直接设置浮点数值,也可以引用float浮点数资源ohos:total_weight="2.5"
ohos:total_weight="$float:total_weight"
orientation子布局排列方向horizontal表示水平方向布局ohos:orientation="horizontal"
vertical表示垂直方向布局ohos:orientation="vertical"
  • DirectionalLayout 所包含组件可支持的 XML 属性见下表:
属性名称中文描述取值取值说明使用案例
layout_alignment对齐方式left表示左对齐可以设置取值项如表中所列,也可以使用“|”进行多项组合。
ohos:layout_alignment="top"
ohos:layout_alignment="top|left"
top表示顶部对齐
right表示右对齐
bottom表示底部对齐
horizontal_center表示水平居中对齐
vertical_center表示垂直居中对齐
center表示居中对齐
weight比重float类型可以直接设置浮点数值,也可以引用float浮点数资源ohos:weight="1"
ohos:weight="$float:weight"
三、排列方式
  • DirectionalLayout 的排列方向(orientation)分为水平(horizontal)或者垂直(vertical)方向。
  • 使用 orientation 设置布局内组件的排列方式,默认为垂直排列。
① 垂直排列
  • 垂直方向排列三个按钮,效果如下:

在这里插入图片描述

  • 示例代码如下:
	<?xml version="1.0" encoding="utf-8"?>
	<DirectionalLayout
	    xmlns:ohos="http://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"/>
	</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 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="#00FFFD"/>
	</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
② 水平排列
  • 水平方向排列三个按钮,效果如下:

在这里插入图片描述

  • 示例代码:
	<?xml version="1.0" encoding="utf-8"?>
	<DirectionalLayout
	    xmlns:ohos="http://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"/>
	</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 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="#00FFFD"/>
	</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • DirectionalLayout 不会自动换行,其子组件会按照设定的方向依次排列,若超过布局本身的大小,超出布局大小的部分将不会被显示,例如:
	<?xml version="1.0" encoding="utf-8"?>
	<DirectionalLayout
	    xmlns:ohos="http://schemas.huawei.com/res/ohos"
	    ohos:width="match_parent"
	    ohos:height="20vp"
	    ohos:orientation="horizontal">
	    <Button
	        ohos:width="166vp"
	        ohos:height="match_content"
	        ohos:left_margin="13vp"
	        ohos:background_element="$graphic:color_cyan_element"
	        ohos:text="Button 1"/>
	    <Button
	        ohos:width="166vp"
	        ohos:height="match_content"
	        ohos:left_margin="13vp"
	        ohos:background_element="$graphic:color_cyan_element"
	        ohos:text="Button 2"/>
	    <Button
	        ohos:width="166vp"
	        ohos:height="match_content"
	        ohos:left_margin="13vp"
	        ohos:background_element="$graphic:color_cyan_element"
	        ohos:text="Button 3"/>
	</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 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="#00FFFD"/>
	</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 此布局包含了三个按钮,但由于 DirectionalLayout 不会自动换行,超出布局大小的组件部分无法显示。界面显示如下:

在这里插入图片描述

四、对齐方式
  • DirectionalLayout 中的组件使用 layout_alignment 控制自身在布局中的对齐方式,当对齐方式与排列方式方向一致时,对齐方式不会生效,如设置了水平方向的排列方式,则左对齐、右对齐将不会生效。
  • 三种对齐方式的示例代码:
	<?xml version="1.0" encoding="utf-8"?>
	<DirectionalLayout
	    xmlns:ohos="http://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"/>
	</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 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="#00FFFD"/>
	</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 三种对齐方式效果示例:

在这里插入图片描述

五、权重
  • 权重(weight)就是按比例来分配组件占用父组件的大小,在水平布局下计算公式为:
    父布局可分配宽度=父布局宽度-所有子组件 width 之和;
    组件宽度=组件 weight /所有组件 weight 之和*父布局可分配宽度;
  • 实际使用过程中,建议使用 width=0 来按比例分配父布局的宽度,1:1:1效果如下:

在这里插入图片描述

  • 示例代码:
	<?xml version="1.0" encoding="utf-8"?>
	<DirectionalLayout
	    xmlns:ohos="http://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"/>
	    <Button
	        ohos:width="0vp"
	        ohos:height="20vp"
	        ohos:weight="1"
	        ohos:background_element="$graphic:color_cyan_element"
	        ohos:text="Button 3"/>
	</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 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="#00FFFD"/>
	</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 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="#878787"/>
	</shape>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
六、场景示例

在这里插入图片描述

  • 源码示例:
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:width="match_parent"
    ohos:height="match_parent"
    ohos:background_element="$graphic:color_light_gray_element">
    <DirectionalLayout
        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"/>
    </DirectionalLayout>
    <Component ohos:height="20vp"/>
    <DirectionalLayout
        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"/>
    </DirectionalLayout>
    <Component ohos:height="20vp"/>
    <DirectionalLayout
        ohos:width="match_parent"
        ohos:height="20vp"
        ohos:orientation="horizontal">
        <Button
            ohos:width="166vp"
            ohos:height="match_content"
            ohos:left_margin="13vp"
            ohos:background_element="$graphic:color_cyan_element"
            ohos:text="Button 1"/>
        <Button
            ohos:width="166vp"
            ohos:height="match_content"
            ohos:left_margin="13vp"
            ohos:background_element="$graphic:color_cyan_element"
            ohos:text="Button 2"/>
        <Button
            ohos:width="166vp"
            ohos:height="match_content"
            ohos:left_margin="13vp"
            ohos:background_element="$graphic:color_cyan_element"
            ohos:text="Button 3"/>
    </DirectionalLayout>
    <Component ohos:height="20vp"/>
    <DirectionalLayout
        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"/>
    </DirectionalLayout>
    <Component ohos:height="20vp"/>
    <DirectionalLayout
        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"/>
        <Button
            ohos:width="0vp"
            ohos:height="20vp"
            ohos:weight="1"
            ohos:background_element="$graphic:color_cyan_element"
            ohos:text="Button 3"/>
    </DirectionalLayout>
</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/228857
推荐阅读
相关标签
  

闽ICP备14008679号