鸿蒙开发之表格布局(TableLayout)

一、描述

表格布局可以实现行列式摆放组件 , 也就是说可以把组件放在不同的单元格内进行布局呈现 。

它既可以通过xml代码实现也可以通过java代码实现 , 我们这里主要介绍xml实现方式。

二、表格布局重要的属性

我们知道html重点表格的框架的基本样式或结构也是通过table的基本属性实现的 , 然而在鸿蒙中也不例外。下面列出来鸿蒙表格布局的重要属性。

属性名称取值描述

alignment_type

对齐方式

align_edges表示布局里的各个组件按边界对齐
align_contents表示布局里的组件按边距对齐
column_count数值规定表格布局的列数
row_count数值规定表格布局的行数

orientation

排列方式

horizontal规定表格水平排列
vertical规定表格垂直排列

注意:排列方式所指对象是表格单元或者内容按照规定的排列顺序排列。如果排列方式是垂直 则先排列垂直方向在排列水平方向 否则 反之。

三、完整案例

 话不多说 先给个完整代码 :

  

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:orientation="vertical"
    ohos:row_count="3"
    ohos:column_count="2"
    xmlns:ohos="http://schemas.huawei.com/res/ohos">

    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="30vp"
        ohos:text="按钮"
        ohos:top_margin="10vp"
        ohos:left_margin="10vp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="black"
        />
    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="30vp"
        ohos:text="按钮"
        ohos:top_margin="10vp"
        ohos:left_margin="10vp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="black"
        />
    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="30vp"
        ohos:text="按钮"
        ohos:top_margin="10vp"
        ohos:left_margin="10vp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="black"
        />

    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="30vp"
        ohos:text="按钮"
        ohos:top_margin="10vp"
        ohos:left_margin="10vp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="black"
        />
    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="30vp"
        ohos:text="按钮"
        ohos:top_margin="10vp"
        ohos:left_margin="10vp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="black"
        />
    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="30vp"
        ohos:text="按钮"
        ohos:top_margin="10vp"
        ohos:left_margin="10vp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="black"
        />

</TableLayout>

效果图:

 

四、排列方式的区别:

如果排列方式是水平排列 , 即:orientation : horizontal

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:orientation="horizontal"
    ohos:row_count="3"
    ohos:column_count="2"
    xmlns:ohos="http://schemas.huawei.com/res/ohos">

    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="30vp"
        ohos:text="按钮"
        ohos:top_margin="10vp"
        ohos:left_margin="10vp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="black"
        />
    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="30vp"
        ohos:text="按钮"
        ohos:top_margin="10vp"
        ohos:left_margin="10vp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="black"
        />


</TableLayout>

效果:

 

如果排列方式是水平排列 , 即:orientation : vertical

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:orientation="vertical"
    ohos:row_count="3"
    ohos:column_count="2"
    xmlns:ohos="http://schemas.huawei.com/res/ohos">

    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="30vp"
        ohos:text="按钮"
        ohos:top_margin="10vp"
        ohos:left_margin="10vp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="black"
        />
    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_size="30vp"
        ohos:text="按钮"
        ohos:top_margin="10vp"
        ohos:left_margin="10vp"
        ohos:text_color="#FFFFFF"
        ohos:background_element="black"
        />


</TableLayout>

效果:

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值