花粉乐分享平台宣传视频
> 华为资讯 > 华为资讯 > 鸿蒙编程 > 零基础学鸿蒙编程-UI控件_DirectionalLayout
零基础学鸿蒙编程-UI控件_DirectionalLayout
来源:蓝不蓝编程
2022-06-30 17:43:42
641
管理
什么是DirectionalLayout DirectionalLayout又称方向布局,是鸿蒙开发中几个常用的布局之一,使用频率较高,而且非常简单.布局内的控件依次排列,支持横向或纵向排列. 基础样例1. 纵向排列效果图代码 <?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"> <Text ohos:height="match_content" ohos:width="match_content" ohos:text="花生皮编程1"/> <Text ohos:height="match_content" ohos:width="match_content" ohos:text="花生皮编程2"/> <Text ohos:height="match_content" ohos:width="match_content" ohos:text="花生皮编程3"/></DirectionalLayout> 代码说明:
  1. 设置ohos:orientation为vertical,展示方向变成纵向
  2. DirectionalLayout里面包括了三个显示文本的Text.
2. 横向排列效果图代码 <?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"> <Text ohos:height="match_content" ohos:width="match_content" ohos:text="花生皮编程1"/> <Text ohos:height="match_content" ohos:width="match_content" ohos:text="花生皮编程2"/> <Text ohos:height="match_content" ohos:width="match_content" ohos:text="花生皮编程3"/></DirectionalLayout> 代码说明:
  1. 设置ohos:orientation为horizontal,展示方向变成横向.
3. 调整子控件摆放位置(gravity属性) 通过DirectionalLayout的ohos:alignment属性控制其子控件相对于自己的对齐方式. 3.1 水平居中效果图代码 <?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="horizontal_center"> <Text ohos:height="match_content" ohos:width="match_content" ohos:text="花生皮编程-水平居中"/></DirectionalLayout>3.2 垂直居中效果图 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y0eZrvJ6-1634824230834)(https://upload-images.jianshu.io/upload_images/6169789-11bb955a945217ce.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)] 代码 <?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="vertical_center"> <Text ohos:height="match_content" ohos:width="match_content" ohos:text="花生皮编程-垂直居中"/></DirectionalLayout>3.3 水平 垂直居中效果图代码 <?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"> <Text ohos:height="match_content" ohos:width="match_content" ohos:text="花生皮编程-居中"/></DirectionalLayout>4. 按比例分空间(layout_weight) DirectionalLayout里的子控件可以通过weight属性按比例分空间大小(横向或纵向).按照DirectionalLayout里所有直属子控件(不算子控件的子控件)设置的weight作为总和,各个控件按照自己的weight所占总和比例来分空闲空间(有些控件未设置weight,则按照固定值). 4.1 等分空间效果图代码 <?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"> <Text ohos:height="match_content" ohos:width="match_content" ohos:background_element="#f2eada" ohos:text="Text1" ohos:text_alignment="center" ohos:weight="1"/> <Text ohos:height="match_content" ohos:width="match_content" ohos:background_element="#9b95c9" ohos:text="Text2" ohos:text_alignment="center" ohos:weight="1"/></DirectionalLayout> 代码说明:
1.两个按钮都设置ohos:weight属性,且值相同,故平分空间. 4.2 按比例分 一个控件保持固定大小,一个占据剩余可用空间. 效果图代码 <?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"> <Text ohos:height="match_content" ohos:width="match_content" ohos:background_element="#f2eada" ohos:text="Text1" ohos:text_alignment="center"/> <Text ohos:height="match_content" ohos:width="match_content" ohos:background_element="#9b95c9" ohos:text="Text2" ohos:text_alignment="center" ohos:weight="1"/></DirectionalLayout> 代码说明:
  1. 第一个按钮不设置ohos:weight属性,保持本身大小.
  2. 第二个按钮设置ohos:weight属性,占据所有剩余可用空间.
完整源代码 https://gitee.com/hspbc/harmonyos_demos/tree/master/directionalLayoutDemo 常用属性说明
属性名 用途
ohos:width 设置控件宽度,可设置为:match_parent(和父控件一样),match_content(按照内容自动伸缩),设置固定值(如200vp)
ohos:height 设置控件高度,可设置为:match_parent(和父控件一样),match_content(按照内容自动伸缩),设置固定值(如200vp)
ohos:layout_alignment 在父控件内对齐方式,可选值:left:居左;start:居左;center:居中;right:居右;end:居右;top:居上;bottom:居下;horizontal_center:水平居中;vertical_center:垂直居中
ohos:background_element 设置背景,可以是色值(如#FF0000)或图片等
ohos:visibility 可选值: visible(显示), invisible(隐藏,但是仍占据UI空间),hide(隐藏,且不占UI空间)
ohos:orientation 子控件排列方向,可选值,horizontal:横向;vertical:纵向
weight DirectionalLayout所有直属子控件,通过该属性值按比例分剩余可用空间

花粉社群VIP加油站

0
点赞
赏礼
赏钱
0
收藏
免责声明:本文仅代表作者个人观点,与花粉乐分享无关。其原创性以及文中陈述文字和内容未经本网证实,对本文以及其中全部或者 部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
凡本网注明 “来源:XXX(非花粉乐分享)”的作品,均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对 其真实性负责。
如因作品内容、版权和其它问题需要同本网联系的,请在一周内进行,以便我们及时处理。
QQ:2443165046 邮箱:info@hflfx.com
关于作者
萤火虫不发光..(花粉指导)
文章
528
主题
0
关注
0
粉丝
0
点击领取今天的签到奖励!
签到排行
随手拍
54个圈友 0个话题
华为手机随手拍,记录生活点滴之美好
华为P30pro
51个圈友 0个话题
这里是华为P30pro手机交流圈,欢迎华为P30pro用户进群交流
体验官
60个圈友 2个话题
华为花粉体验官,体验官专属的交流群
登录后查看您创建的圈子
登录后查看您创建的圈子
所有圈子
杭州互联网违法和不良信息举报平台 网络110报警服务 浙ICP备17046585号
0
0
分享
请选择要切换的马甲:

个人中心

每日签到

我的消息

内容搜索