HarmonyOS UI开发 DirectionalLayout(定向布局) 的使用
DirectionalLayout 是什么
DirectionalLayout 是定向布局,控件水平或垂直排列(类似Android 的线性布局不过还是有区别的)
DirectionalLayout的自有XML属性
属性名称 |
中文描述 |
取值 |
取值说明 |
使用案例 |
---|---|---|---|---|
alignment |
对齐方式 |
left |
表示左对齐。 |
可以设置取值项如表中所列,也可以使用“|”进行多项组合。 ohos:alignment="top|left" ohos:alignment="left" |
top |
表示顶部对齐。 |
|||
right |
表示右对齐。 |
|||
bottom |
表示底部对齐。 |
|||
horizontal_center |
表示水平居中对齐。 |
|||
vertical_center |
表示垂直居中对齐。 |
|||
center |
表示居中对齐。 |
|||
start |
表示靠起始端对齐。 |
|||
end |
表示靠结束端对齐。 |
|||
orientation |
子布局排列方向 |
horizontal |
表示水平方向布局。 |
ohos:orientation="horizontal" |
vertical |
表示垂直方向布局。 |
ohos:orientation="vertical" |
||
total_weight |
所有子视图的权重之和 |
float类型 |
可以直接设置浮点数值,也可以引用float浮点数资源。 |
ohos:total_weight="2.5" ohos:total_weight="$float:total_weight" |
DirectionalLayout 自有属性 alignment
1 底部对齐
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="bottom"ohos:orientation="vertical"><Buttonohos:id="$+id:button1"ohos:height="60vp"ohos:width="100vp"ohos:background_element="#00d8a0"ohos:text="Java"ohos:text_size="20fp"/></DirectionalLayout>
效果图
2 右边对齐
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="right"ohos:orientation="vertical"><Buttonohos:id="$+id:button1"ohos:height="60vp"ohos:width="100vp"ohos:background_element="#00d8a0"ohos:text="Java"ohos:text_size="20fp"/></DirectionalLayout>
效果图
3 居中
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="center"ohos:orientation="vertical"><Buttonohos:id="$+id:button1"ohos:height="60vp"ohos:width="100vp"ohos:background_element="#00d8a0"ohos:text="Java"ohos:text_size="20fp"/></DirectionalLayout>
效果图
4 右下角
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment="bottom|right"ohos:orientation="vertical"><Buttonohos:id="$+id:button1"ohos:height="60vp"ohos:width="100vp"ohos:background_element="#00d8a0"ohos:text="Java"ohos:text_size="20fp"/></DirectionalLayout>
这里就先了解这几个
DirectionalLayout 自有属性 orientation
orientation设置布局内组件的排列方式的属性是 ,如果没有设置默认为垂直排列
控制垂直排列的属性为 ohos:orientation="vertical"
控制水平排列的属性为ohos:orientation="horizontal"
orientation设置垂直排列
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Buttonohos:id="$+id:button1"ohos:height="60vp"ohos:width="120vp"ohos:top_margin="10vp"ohos:background_element="#00d8a0"ohos:text_size="20fp"ohos:text="Java"/><Buttonohos:id="$+id:button2"ohos:height="60vp"ohos:width="120vp"ohos:background_element="#00d8a0"ohos:text="Android"ohos:text_size="20fp"ohos:top_margin="10vp"/><Buttonohos:id="$+id:button3"ohos:height="60vp"ohos:width="120vp"ohos:background_element="#00d8a0"ohos:text="HarmonyOS"ohos:text_size="20fp"ohos:top_margin="10vp"/></DirectionalLayout>
效果如下
orientation 水平排列
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="horizontal"><Buttonohos:id="$+id:button1"ohos:height="60vp"ohos:width="100vp"ohos:top_margin="10vp"ohos:left_margin="10vp"ohos:background_element="#00d8a0"ohos:text_size="20fp"ohos:text="Java"/><Buttonohos:id="$+id:button2"ohos:height="60vp"ohos:width="100vp"ohos:left_margin="10vp"ohos:background_element="#00d8a0"ohos:text="Android"ohos:text_size="20fp"ohos:top_margin="10vp"/><Buttonohos:id="$+id:button3"ohos:height="60vp"ohos:width="100vp"ohos:left_margin="10vp"ohos:background_element="#00d8a0"ohos:text="HarmonyOS"ohos:text_size="20fp"ohos:top_margin="10vp"/></DirectionalLayout>
效果如下
DirectionalLayout 自有属性 total_weight
这个属性有意思就是子内容权重数之和,感觉可以不设置,这里设置了就限制了子内容自己要设置的数值了,假如在DirectionalLayout 设置为2 子内容刚好有2个内容的话可以设置数值为1,如果设置1 子内容2个子内容不许设置0.5 如果设置子内容设置1的话就显示1个内容了,个人目前感觉没有特别的需求不要设置它了,因为不设置total_weight也不影响使用权重,
下面截图说下自己是实验了的
下面没有设置total_weight正常的情况
底部使用的是1 ,如果total_weight 设置为3 效果合理,如果我设置为2,或者1 那就行了如下
total_weight 会把子内容权重的值限制了(这里解释下就是子内容weight的值有时候我想没一个设置2,或者0.5之类,还需要算号里面权重的的和在写到外层去,现在是不写也能实现的想要的功能)
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所包含组件可支持的XML属性:layout_alignment
这个属性需要特别注意呢
当orientation 设置水平的时候左右无效果的看下图
所以使用的时候需要注意orientation 设置的属性,下面的设置垂直的时候可以使用左右
同样的道理当orientation 设置垂直的上和下的属性就不能使用了
个人试了多次总结情况大致如下:
当 ohos:orientation="vertical" 可以使用layout_alignment可以使用的属性有left,right,center,horizontal_center(水平居中),其他情况无效
当ohos:orientation="orientation" 可以使用layout_alignment可以使用的属性有top,botton,center,vertical_center(垂直居中),其他情况无效
DirectionalLayout所包含组件可支持的XML属性:weight(权重)
水平方向的权重
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="horizontal"><Buttonohos:id="$+id:button1"ohos:height="60fp"ohos:width="0"ohos:weight="1"ohos:left_margin="10vp"ohos:right_margin="10vp"ohos:background_element="#00d8a0"ohos:text="Java"ohos:text_size="20fp"/><Buttonohos:id="$+id:button2"ohos:height="60vp"ohos:width="0"ohos:weight="1"ohos:left_margin="10vp"ohos:right_margin="10vp"ohos:background_element="#00d8a0"ohos:text="Android"ohos:text_size="20fp"/><Buttonohos:id="$+id:button3"ohos:height="60vp"ohos:width="0"ohos:weight="1"ohos:left_margin="10vp"ohos:right_margin="10vp"ohos:background_element="#00d8a0"ohos:text="HarmonyOS"ohos:text_size="20fp"/></DirectionalLayout>
效果图
垂直方向的权重
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="200vp"ohos:width="match_parent"ohos:orientation="vertical"><Buttonohos:id="$+id:button1"ohos:height="0"ohos:width="120fp"ohos:weight="1"ohos:top_margin="10vp"ohos:bottom_margin="10vp"ohos:background_element="#00d8a0"ohos:text="Java"ohos:text_size="20fp"/><Buttonohos:id="$+id:button2"ohos:height="0"ohos:width="120fp"ohos:weight="1"ohos:top_margin="10vp"ohos:bottom_margin="10vp"ohos:background_element="#00d8a0"ohos:text="Android"ohos:text_size="20fp"/><Buttonohos:id="$+id:button3"ohos:height="0"ohos:width="120fp"ohos:weight="1"ohos:top_margin="10vp"ohos:bottom_margin="10vp"ohos:background_element="#00d8a0"ohos:text="HarmonyOS"ohos:text_size="20fp"/></DirectionalLayout>
效果图
,
官方参考连接地址
其他的布局
HarmonyOS UI开发 DependentLayout(依赖布局) 的使用
HarmonyOS UI开发 TableLayout(表格布局) 的使用
HarmonyOS UI开发 AdaptiveBoxLayout(自适应盒子布局) 的使用
HarmonyOS UI开发 PositionLayout(位置布局) 的使用
HarmonyOS UI开发 TableLayout(表格布局) 的使用
HarmonyOS UI开发 DirectionalLayout(定向布局) 的使用相关推荐
- HarmonyOS UI开发 TableLayout(表格布局) 的使用
TableLayout 是什么 TableLayout 是表格布局,就是使用表格的方式划分子组件, 个人感觉应该也不是常用的布局,常用的还是选择DirectionalLayout和Dependent ...
- HarmonyOS UI开发 PositionLayout(位置布局) 的使用
PositionLayout 是什么 PositionLayout 是位置布局,在PositionLayout中,子组件通过指定准确的x/y坐标值在屏幕上显示.(0, 0)为左上角:当向下或向右移动时 ...
- HarmonyOS UI开发 DependentLayout(依赖布局) 的使用
DependentLayout是什么 DependentLayout是依赖布局,每个组件可以指定相对于其他同级元素的位置,或者指定相对于父组件的位置.(类似Android的相对布局) Dependen ...
- HarmonyOS UI开发 AdaptiveBoxLayout(自适应盒子布局) 的使用
AdaptiveBoxLayout 是什么 AdaptiveBoxLayout 是自适应盒子布局,该布局提供了在不同屏幕尺寸设备上的自适应布局能力,主要用于相同级别的多个组件需要在不同屏幕尺寸设备上自 ...
- HarmonyOS UI开发 StackLayout(堆栈布局) 的使用
StackLayout是什么 StackLayout 是堆栈布局,直接在屏幕上开辟出一块空白的区域,添加到这个布局中的视图都是以层叠的方式显示,而它会把这些视图默认放到这块区域的左上角,第一个添加到布 ...
- HarmonyOS UI 开发 vp ,fp , px 以及写具体数字的 理解
HarmonyOS 开发UI 距离和字体使用什么单位 距离使用 vp (virtual pixel) 字体大小使用 fp (font pixel) 详细了解 vp ,fp , px 虚拟像素单位:vp ...
- HarmonyOS UI开发 match_parent与match_content
match_parent: 表示组件大小将扩展为父组件允许的最大值,它将占据父组件方向上的剩余大小 match_content: 表示组件大小与它的内容占据的大小范围相适应. 两者详细说明: 下面看下 ...
- Android 开发 -- 开发第一个安卓程序、Android UI开发(布局的创建:相对布局和线性布局、控件单位:px pt dp sp、常用控件 、常见对话框、ListView)
文章目录 1. 开发第一个Hello World程序 1.1 开发程序 1.2 认识程序中的文件 1.3 Android程序结构 1.4 安卓程序打包 2. Android UI开发 2.1 布局的创 ...
- 【Android -- UI开发】一份 UI 开发学习指南
思维导图 推荐资料:官方文档 六大布局 网上有人比喻的很好:布局好比是建筑里的框架,组件按照布局的要求依次排列,就组成了用于看见的漂亮界面了. 请看文章:[Android – UI 开发]六大布局 U ...
最新文章
- shell判断数字大小_shell! shell! shell!
- [原] Excel(VBA)中数据的非科学记数法显示
- linux nice线程,linux nice 线程
- el-table中每列设置同样的宽度导致表格宽度没法实现100%布局
- Tianlesoftware Oracle 学习 手册 第一版
- Nifi清空Queue操作
- 电商促销惊喜海报设计模板,会讲故事的素材
- python之七行代码制作GIF动画
- [转]一个故事讲清楚NIO
- 【知云】第十期:如何保障云上游戏安全?云盾+高防IP筑起云上安全高墙
- 深度学习群体行为识别python包_入门Python深度学习,学会这个方法事半功倍
- 实验一:JDK下载与安装、 Eclipse下载与使用
- python安装不了whl文件_python怎样安装whl文件
- 前端学习笔记-JS数据类型
- Navicat连接Oracle
- MIMO雷达波形设计
- 要有遥不可及的梦想,也要有脚踏实地的本事
- VG验证码识别框架2.2 免费识别验证码
- Windows 11 新功能 Microsoft Teams
- 关于火车运煤的一些想法