赞
踩
在PositionLayout中,子组件通过指定准确的x/y坐标值在屏幕上显示。(0, 0)为左上角;当向下或向右移动时,坐标值变大;允许组件之间互相重叠。
PositionLayout示意图
PositionLayout以坐标的形式控制组件的显示位置,允许组件相互重叠。
使用PositionLayout的布局效果
示例代码:
- <?xml version="1.0" encoding="utf-8"?>
- <PositionLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:id="$+id:position"
- ohos:height="match_parent"
- ohos:width="300vp"
- ohos:background_element="#3387CEFA">
-
- <Text
- ohos:id="$+id:position_text_1"
- ohos:height="50vp"
- ohos:width="200vp"
- ohos:background_element="#9987CEFA"
- ohos:position_x="50vp"
- ohos:position_y="8vp"
- ohos:text="Title"
- ohos:text_alignment="center"
- ohos:text_size="20fp"/>
-
- <Text
- ohos:id="$+id:position_text_2"
- ohos:height="200vp"
- ohos:width="200vp"
- ohos:background_element="#9987CEFA"
- ohos:position_x="8vp"
- ohos:position_y="64vp"
- ohos:text="Content"
- ohos:text_alignment="center"
- ohos:text_size="20fp"/>
-
- <Text
- ohos:id="$+id:position_text_3"
- ohos:height="200vp"
- ohos:width="200vp"
- ohos:background_element="#9987CEFA"
- ohos:position_x="92vp"
- ohos:position_y="188vp"
- ohos:text="Content"
- ohos:text_alignment="center"
- ohos:text_size="20fp"/>
- </PositionLayout>
复制
设置子组件的坐标时(position_x和position_y属性),除了上述示例中的XML方式,还可以在对应的AbilitySlice中通过setPosition(int x, int y)接口设置,Java示例代码如下:
- Text title = (Text)findComponentById(ResourceTable.Id_position_text_1);
- Text content1 = (Text)findComponentById(ResourceTable.Id_position_text_2);
- Text content2 = (Text)findComponentById(ResourceTable.Id_position_text_3);
-
- title.setPosition(vp2px(50), vp2px(8));
- content1.setPosition(vp2px(8), vp2px(64));
- content2.setPosition(vp2px(92), vp2px(188));
复制
单位转换的方法如下:
- private int vp2px(float vp){
- return AttrHelper.vp2px(vp,this);
- }
复制
Right组件右侧超出部分将不显示
示例代码:
- <?xml version="1.0" encoding="utf-8"?>
- <PositionLayout
- ...>
-
- ...
-
- <Text
- ohos:id="$+id:position_text_4"
- ohos:height="120vp"
- ohos:width="120vp"
- ohos:background_element="#9987CEFA"
- ohos:position_x="212vp"
- ohos:position_y="64vp"
- ohos:text="Right"
- ohos:text_alignment="center"
- ohos:text_size="20fp"/>
- </PositionLayout>
复制
AdaptiveBoxLayout是自适应盒子布局,该布局提供了在不同屏幕尺寸设备上的自适应布局能力,主要用于相同级别的多个组件需要在不同屏幕尺寸设备上自动调整列数的场景。
AdaptiveBoxLayout示意图
AdaptiveBoxLayout布局常用方法如下。
方法 | 功能 |
---|---|
addAdaptiveRule(int minWidth, int maxWidth, int columns) | 添加一个自适应盒子布局规则。 |
removeAdaptiveRule(int minWidth, int maxWidth, int columns) | 移除一个自适应盒子布局规则。 |
clearAdaptiveRules() | 移除所有自适应盒子布局规则。 |
在AdaptiveBoxLayout中添加和删除自适应盒子布局规则的效果对比如下。
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:orientation="vertical">
-
- <AdaptiveBoxLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="0vp"
- ohos:width="match_parent"
- ohos:weight="1"
- ohos:id="$+id:adaptive_box_layout">
-
- <Text
- ohos:height="40vp"
- ohos:width="80vp"
- ohos:background_element="#EC9DAA"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="NO 1"
- ohos:text_size="18fp" />
-
- <Text
- ohos:height="40vp"
- ohos:width="80vp"
- ohos:background_element="#EC9DAA"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="NO 2"
- ohos:text_size="18fp" />
-
- <Text
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:background_element="#EC9DAA"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:multiple_lines="true"
- ohos:text="AdaptiveBoxLayout, where a number of boxes with the same width but varied heights are laid out. The height of a row is determined by the highest box."
- ohos:text_size="18fp" />
-
- <Text
- ohos:height="40vp"
- ohos:width="80vp"
- ohos:background_element="#EC9DAA"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="NO 4"
- ohos:text_size="18fp" />
-
- <Text
- ohos:height="40vp"
- ohos:width="match_parent"
- ohos:background_element="#EC9DAA"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="Add"
- ohos:text_size="18fp" />
-
-
-
- <Text
- ohos:height="40vp"
- ohos:width="80vp"
- ohos:background_element="#EC9DAA"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="NO 5"
- ohos:text_size="18fp" />
-
- <Text
- ohos:height="160vp"
- ohos:width="80vp"
- ohos:background_element="#EC9DAA"
- ohos:margin="10vp"
- ohos:padding="10vp"
- ohos:text="NO 6"
- ohos:text_size="18fp" />
- </AdaptiveBoxLayout>
-
- <Button
- ohos:id="$+id:add_rule_btn"
- ohos:layout_alignment="horizontal_center"
- ohos:top_margin="10vp"
- ohos:padding="10vp"
- ohos:background_element="#A9CFF0"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:text_size="22fp"
- ohos:text="adaptiveBoxLayout.addAdaptiveRule(100, 2000, 3);"/>
-
- <Button
- ohos:id="$+id:remove_rule_btn"
- ohos:padding="10vp"
- ohos:top_margin="10vp"
- ohos:layout_alignment="horizontal_center"
- ohos:bottom_margin="10vp"
- ohos:background_element="#D5D5D5"
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:text_size="22fp"
- ohos:text="adaptiveBoxLayout.removeAdaptiveRule(100, 2000, 3);"/>
-
- </DirectionalLayout>
复制
Java关键代码:
- AdaptiveBoxLayout adaptiveBoxLayout = (AdaptiveBoxLayout)findComponentById(ResourceTable.Id_adaptive_box_layout);
-
- findComponentById(ResourceTable.Id_add_rule_btn).setClickedListener((component-> {
- // 添加规则
- adaptiveBoxLayout.addAdaptiveRule(100, 2000, 3);
- // 更新布局
- adaptiveBoxLayout.postLayout();
- }));
-
- findComponentById(ResourceTable.Id_remove_rule_btn).setClickedListener((component-> {
- // 移除规则
- adaptiveBoxLayout.removeAdaptiveRule(100, 2000, 3);
- // 更新布局
- adaptiveBoxLayout.postLayout();
- }));
为了能够让大家跟上互联网时代的技术迭代,赶上互联网开发人员寒冬期间一波红利,在这里跟大家分享一下我自己近期学习心得以及参考网上资料整理出的一份最新版的鸿蒙学习提升资料,有需要的小伙伴自行领取,限时开源,先到先得~~~~
需要以上视频学习资料小伙伴
这份手册涵盖了当前鸿蒙 (Harmony OS) 开发技术必掌握的核心知识点
HarmonyOS 概念:
如何快速入门?
开发基础知识:
基于ArkTS 开发:
获取以上文中提到的这份纯血版鸿蒙 (Harmony OS) 开发资料的小伙伴
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。