20.HarmonyOS App(JAVA)表格布局Layout使用方法

news2024/9/23 21:23:57

ability_main.xml,实现计算器键盘按钮

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:row_count="6"
    ohos:column_count="4"
   >
    <Button
        ohos:id="$+id:btn_table"
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "表格布局_计算器键盘"
        ohos:background_element="#FF374FF1"
        ohos:text_size="20fp"
        ohos:text_color="#FFFDFCFC"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "7"
        ohos:background_element="#FF7BA4CF"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "8"
        ohos:background_element="#FF7BA4CF"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "9"
        ohos:background_element="#FF7BA4CF"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "/"
        ohos:background_element="#FF7BA4CF"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />

    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "4"
        ohos:background_element="#FF59EC23"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "5"
        ohos:background_element="#FF59EC23"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "6"
        ohos:background_element="#FF59EC23"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "*"
        ohos:background_element="#FF59EC23"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "1"
        ohos:background_element="#FF7BA4CF"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "2"
        ohos:background_element="#FF7BA4CF"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "3"
        ohos:background_element="#FF7BA4CF"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "-"
        ohos:background_element="#FF7BA4CF"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "0"
        ohos:background_element="#FF59EC23"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "."
        ohos:background_element="#FF59EC23"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "+"
        ohos:background_element="#FF59EC23"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "="
        ohos:background_element="#FF59EC23"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />
    <Button
        ohos:id="$+id:btn_clear"
        ohos:height="35vp"
        ohos:width="65vp"
        ohos:text = "clear"
        ohos:background_element="#FF7BA4CF"
        ohos:text_size="20fp"
        ohos:margin="6vp"
        />

</TableLayout>

 MainAbilitySlice.java

点击按钮,toast消息提示,设置按钮控件跨列效果

package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TableLayout;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import static ohos.agp.components.TableLayout.*;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Button button_table = (Button) findComponentById(ResourceTable.Id_btn_table);
        Button button_clear = (Button) findComponentById(ResourceTable.Id_btn_clear);
        TableLayout.LayoutConfig config = new TableLayout.LayoutConfig(TableLayout.specification(0,1),TableLayout.specification(0,4));
        //TableLayout.specification(4,1),行规范
        //TableLayout.specification(0,4),列规范
        //设置宽度
        config.width = button_table.getWidth()*4 + button_table.getMarginLeft()*6;
        //设置高度
        config.height = button_table.getHeight();

        //设置外边框
        config.setMargins(button_table.getMarginLeft(),button_table.getMarginTop(),button_table.getMarginRight(),button_table.getMarginBottom());
        button_table.setLayoutConfig(config);


        TableLayout.LayoutConfig config2 = new TableLayout.LayoutConfig(TableLayout.specification(5,1),TableLayout.specification(0,4));
        //TableLayout.specification(4,1),行规范
        //TableLayout.specification(0,4),列规范
        //设置宽度
        config2.width = button_clear.getWidth()*4 + button_clear.getMarginLeft()*6;
        //设置高度
        config2.height = button_clear.getHeight();

        //设置外边框
        config2.setMargins(button_clear.getMarginLeft(),button_clear.getMarginTop(),button_clear.getMarginRight(),button_clear.getMarginBottom());
        button_clear.setLayoutConfig(config2);

        button_clear.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                new ToastDialog(getContext())
                        .setText("点击了清除按钮")
                        .setAlignment(LayoutAlignment.CENTER)
                        .show();
            }
        });


    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

 TableLayout的自有XML属性见下表:

表1 TableLayout的自有XML属性

属性名称

中文描述

取值

取值说明

使用案例

alignment_type

对齐方式

align_edges

表示TableLayout内的组件按边界对齐。

ohos:alignment_type="align_edges"

align_contents

表示TableLayout内的组件按边距对齐。

ohos:alignment_type="align_contents"

column_count

列数

integer类型

可以直接设置整型数值,也可以引用integer资源。

ohos:column_count="3"

ohos:column_count="$integer:count"

row_count

行数

integer类型

可以直接设置整型数值,也可以引用integer资源。

ohos:row_count="2"

ohos:row_count="$integer:count"

orientation

排列方向

horizontal

表示水平方向布局。

ohos:orientation="horizontal"

vertical

表示垂直方向布局。

ohos:orientation="vertical"

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1430683.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

STL——空间配置器

空间配置器是STL六大组件之一&#xff0c;它和其他五个组件相互配合&#xff0c;起着很关键的作用。 容器&#xff1a;各种数据结构、如vector、list、stack、deque、queue、set、map、unordered_map等等算法&#xff1a;各种算法&#xff0c;如sort、serach、copy、erase 提供…

AI新宠Arc浏览器真可以取代Chrome吗?

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

微信小程序实现吸顶、网格、瀑布流布局

微信小程序开发通常是在webview模式下编写&#xff0c;但是对小程序的渲染性能有一定的追求&#xff0c;就需要使用Skyline模式进行渲染&#xff0c;同时在这种模式下有也有一些特殊的组件&#xff0c;可以轻松的实现想要的效果&#xff0c;本文将介绍在Skyline模式下如何实现吸…

单项选择题标准化考试系统设计

工程链接放在这里 https://download.csdn.net/download/Samature/88805471使用 注意事项 登录 在这里插入图片描述 登录有初始账号&#xff1a;yzb 密码&#xff1a;123456 后续可以自己加 保存的用户信息位置和题目 problems是题目 users是用户名 可能遇到的bug CMake Er…

20240203在Ubuntu20.04.6下配置stable-diffusion-webui.git

20240203在Ubuntu20.04.6下配置stable-diffusion-webui.git 2024/2/3 11:55 【结论&#xff1a;在Ubuntu20.04.6下&#xff0c;生成512x512分辨率的图像&#xff0c;大概需要11秒钟&#xff01;】 前提条件&#xff0c;可以通过技术手段上外网&#xff01;^_首先你要有一张NVID…

苹果CMS挖片网升级版视频主题模版源码

自适应视频站正版高级挖片网收录模板&#xff0c;模板不错&#xff0c;是挖片网的升级版。 源码下载&#xff1a;https://download.csdn.net/download/m0_66047725/88799583 更多资源下载&#xff1a;关注我。

软件测试--接口自动化

接口自动化测试框架 目录结构 7 部分&#xff08;5个目录、2个文件&#xff09;&#xff1a; api/: 存储接口对象层&#xff08;自己封装的 接口&#xff09; scripts/: 存储测试脚本层 &#xff08;unittest框架实现的 测试类、测试方法&#xff09; data/: 存储 .json 数据…

Python基础知识:Python序列以及序列的索引、切片、相乘和相加

索引 索引就是序列中的每个元素所在的位置&#xff0c;可以通过从左往右的正数索引&#xff0c;也可以通过从右往左的负数索引。 从左往右的正数索引&#xff1a;在python序列中&#xff0c;第一个元素的索引值为0&#xff0c;第二个元素的索引值为1&#xff0c;以此类推&…

python+PyQt5实现指示灯检查

UI: 源代码&#xff1a; # -*- coding: utf-8 -*-# Form implementation generated from reading ui file CheckImageWinFrm.ui # # Created by: PyQt5 UI code generator 5.15.2 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again…

ICA:独立成分分析

1.意义 两个假设&#xff1a;一个是假设源信号是相互统计独立的,另一个是假设己知源信号的统计分布特征。 另一个假设是信号的非高斯性,现实世界的许多信号,诸如绝大多数的语音信号和图像信号即是服从非高斯分布的这个假设的可应用性,带来了独立成分分析的重要特征,即实际信号的…

N-142基于springboot,vue停车场管理系统

开发工具&#xff1a;IDEA 服务器&#xff1a;Tomcat9.0&#xff0c; jdk1.8 项目构建&#xff1a;maven 数据库&#xff1a;mysql5.7 项目采用前后端分离 前端技术&#xff1a;vueelementUI 服务端技术&#xff1a;springbootmybatis-plus 本项目分为普通用户和管理员…

Flutter实现轮播图功能

一、在pubspec.yaml中添加&#xff1a; dependencies:# 轮播图card_swiper: ^3.0.1card_swiper: ^3.0.1&#xff0c;要获取最新版本&#xff1a;https://pub-web.flutter-io.cn/packages/card_swiper/versions&#xff0c;这个里面有文档可以看&#xff0c;如下图&#xff1a;…

创建一个Vue项目(含npm install卡住不动的解决)

目录 1 安装Node.js 2 使用命令提示符窗口创建Vue 2.1 打开命令提示符窗口 2.2 初始Vue项目 2.2.1 npm init vuelatest 2.2.2 npm install 3 运行Vue项目 3.1 命令提示符窗口 3.2 VSCode运行项目 1 安装Node.js 可以看我的这篇文章《Node.js的安装》 2 使用命令提示…

Pytorch学习02_TensorBoard使用01

更换编辑器 找到自己的Anaconda安装路径下envs\pytorch01中的oython.exe&#xff0c;pytorch01是笔者自己创建的pytorch环境名 选择好后&#xff0c;点击确定 点击 “应用”&#xff0c;再点击“确定” 在pytorch环境下安装tensorboard pip install pytorch 安装结束 writer.ad…

css1文本属性

一.颜色&#xff08;color&#xff09;&#xff08;一般用16进制&#xff09; 二.对齐&#xff08;text-align) 三.装饰&#xff08;text-decoration&#xff09; 四.缩进&#xff08;text-indent&#xff09;&#xff08;一般用2em&#xff09;&#xff08;有单位&#xff09;…

Android开发--实时监测系统+部署故障诊断算法

0.项目整体思路介绍&#xff1a; 搭建无人装备模拟实验平台&#xff0c;使用采集器对数据进行采集&#xff0c;通过网络通信Udp协议发送到安卓端&#xff0c;安卓端作界面显示&#xff0c;算法使用matlab仿真后&#xff0c;用C语言实现。将采集器采集到的数据经过处理后训练&a…

docker入门教程之将应用程序容器化

将应用程序容器化 在本指南的其余部分中&#xff0c;您将使用在 Node.js 上运行的简单待办事项列表管理器。如果您不熟悉 Node.js&#xff0c;请不要担心。本指南不需要任何 JavaScript 经验。 先决条件 您已安装最新版本的 Docker Desktop。您已经安装了 Git 客户端。您可以…

树莓派与Win11通信【一对一】(四)

树莓派与Win11通信【一对一】&#xff08;四&#xff09; 树莓派与Win11通信【一对一】&#xff08;四&#xff09;的代码优化版&#xff0c; 最近给代码添加了打开摄像头与否的验证&#xff0c;以及文件的保存&#xff0c;定时拍摄 1.Server端 import socket import time …

解决浏览器端 globalThis is not defined 报错

解决浏览器端 globalThis is not defined 报错 前言解决办法&#xff1a; 前言 在使用低版本火狐浏览器出现报错globalThis is not defined 解决办法&#xff1a; 在vue的index.html 中添加 this.globalThis || (this.globalThis this) <head><script>this.g…

vue3-自定义指令

自定义指令 vue 除了内置的制指令&#xff08;v-model v-show 等&#xff09;之外&#xff0c;还允许我们注册自定义的指令。 vue 复用代码的方式&#xff1a; 组件&#xff1a;主要是构建模块。 组合式函数&#xff1a;侧重有状态的逻辑。 自定义指令&#xff1a;主要是为…