HarmonyOS之常用布局StackLayout的使用

举报
Serendipity·y 发表于 2022/02/16 22:57:26 2022/02/16
【摘要】 一、StackLayout 简介 StackLayout 直接在屏幕上开辟出一块空白的区域,添加到这个布局中的视图都是以层叠的方式显示,而它会把这些视图默认放到这块区域的左上角,第一个添加到布局中的视图...

一、StackLayout 简介

  • StackLayout 直接在屏幕上开辟出一块空白的区域,添加到这个布局中的视图都是以层叠的方式显示,而它会把这些视图默认放到这块区域的左上角,第一个添加到布局中的视图显示在最底层,最后一个被放在最顶层。上一层的视图会覆盖下一层的视图。
  • StackLayout 示意如下:

在这里插入图片描述

二、支持的 XML 属性

  • StackLayout 无自有的 XML 属性,共有 XML 属性继承自 Component。详情请参考我之前的博客:HarmonyOS之组件通用的XML属性总览
  • StackLayout 所包含组件可支持的 XML 属性见下表:
属性名称 中文描述 取值 取值说明 使用案例
layout_alignment 对齐方式 left 表示左对齐 可以设置取值项如表中所列,也可以使用“|”进行多项组合。 ohos:layout_alignment="top" ohos:layout_alignment="top|left"
top 表示顶部对齐
right 表示右对齐
bottom 表示底部对齐
horizontal_center 表示水平居中对齐
vertical_center 表示垂直居中对齐
center 表示居中对齐

三、StackLayout 使用

  • 创建 StackLayout:
	<?xml version="1.0" encoding="utf-8"?>
	<StackLayout
	    xmlns:ohos="http://schemas.huawei.com/res/ohos"
	    ohos:height="match_parent"
	    ohos:width="match_parent">
	</StackLayout>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 使用默认布局添加组件,StackLayout 中组件的布局默认在区域的左上角,并且以后创建的组件会在上层。xml 布局如下:
	<?xml version="1.0" encoding="utf-8"?>
	<StackLayout
	    xmlns:ohos="http://schemas.huawei.com/res/ohos"
	    ohos:id="$+id:stack_layout"
	    ohos:height="match_parent"
	    ohos:width="match_parent">
	
	    <Text
	        ohos:id="$+id:text_blue"
	        ohos:text_alignment="bottom|horizontal_center"
	        ohos:text_size="24fp"
	        ohos:text="Layer 1"
	        ohos:height="400vp"
	        ohos:width="400vp"
	        ohos:background_element="#3F56EA" />
	
	    <Text
	        ohos:id="$+id:text_light_purple"
	        ohos:text_alignment="bottom|horizontal_center"
	        ohos:text_size="24fp"
	        ohos:text="Layer 2"
	        ohos:height="300vp"
	        ohos:width="300vp"
	        ohos:background_element="#00AAEE" />
	
	    <Text
	        ohos:id="$+id:text_orange"
	        ohos:text_alignment="center"
	        ohos:text_size="24fp"
	        ohos:text="Layer 3"
	        ohos:height="80vp"
	        ohos:width="80vp"
	        ohos:background_element="#00BFC9" />
	        
	</StackLayout>

  
 
  • 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
  • 多个视图排列效果如下所示:

在这里插入图片描述

  • 使用相对位置添加组件,使用 layout_alignment 属性可以指定组件在 StackLayout 中的相对位置,如下表示 Button 组件位于 StackLayout 的右面:

在这里插入图片描述

  • xml 布局:
	<?xml version="1.0" encoding="utf-8"?>
	<StackLayout
	    xmlns:ohos="http://schemas.huawei.com/res/ohos"
	    ohos:id="$+id:stack_layout"
	    ohos:height="match_parent"
	    ohos:width="match_parent">
	
	   <Button
	        ohos:id="$+id:button"
	        ohos:height="40vp"
	        ohos:width="80vp"
	        ohos:layout_alignment="right"
	        ohos:background_element="#3399FF"/>
	
	</StackLayout>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

四、场景展示

  • 点击将子视图从底层移到顶层显示:

在这里插入图片描述

  • Java 示例代码:
	ComponentContainer stackLayout = (ComponentContainer) findComponentById(ResourceTable.Id_stack_layout);
	
	Text textFirst = (Text) findComponentById(ResourceTable.Id_text_blue);
	
	textFirst.setClickedListener(new Component.ClickedListener() {
	    @Override
	    public void onClick(Component component) {
	        stackLayout.moveChildToFront(component);
	    }
	});

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

文章来源: blog.csdn.net,作者:Serendipity·y,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/Forever_wj/article/details/118253868

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。