当前位置:   article > 正文

HarmonyOS:使用TableLayout实现Android开发中的GridView_鸿蒙开发显示gridview

鸿蒙开发显示gridview

  Android开发中GridView是一种网格布局的控件,其效果如下:
在这里插入图片描述
  具体实现请参考:Android自定义较为精美的GridView

1 实现效果

  鸿蒙开发中没有类似于GridView的控件,只有一种网格布局方式TableLayout,因此我们可以利用TableLayout来实现类似的效果。效果如下:
在这里插入图片描述

2 实现步骤

2.1 布局文件

  通过观察上图我们可以知道,每一个小的按钮都是一张图下跟着一行文字,因此布局文件中我们采用DirectionalLayout,每一个垂直布局的DirectionalLayout包裹着一个Image和一个Text。布局文件代码如下:

<?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:alignment="center"
    ohos:background_element="#DDDDDD"
    ohos:orientation="vertical">

    <DirectionalLayout
        ohos:id="$+id:banner_d"
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:orientation="vertical">
        
        <com.youth.banner.Banner
            ohos:id="$+id:banner"
            ohos:height="195vp"
            ohos:width="match_parent">
        </com.youth.banner.Banner>

        <DirectionalLayout
            ohos:id="$+id:info"
            ohos:height="20vp"
            ohos:width="match_parent"
            ohos:top_margin="4vp"
            ohos:background_element="#F7F7F7"
            ohos:orientation="horizontal">

            <Text
                ohos:height="match_parent"
                ohos:width="match_parent"
                ohos:left_margin="10vp"
                ohos:text="个人教务"
                ohos:text_color="#000000"
                ohos:text_size="16fp">
            </Text>

        </DirectionalLayout>

        <TableLayout
            ohos:id="$+id:table1"
            ohos:height="200vp"
            ohos:top_margin="4vp"
            ohos:width="match_parent"
            ohos:column_count="4"
            ohos:row_count="2"
            ohos:padding="8vp"
            ohos:orientation="horizontal"
            ohos:background_element="#F7F7F7">

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_1"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:query_mark"/>

                <Text
                    ohos:id="$+id:tv_grid_1"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="成绩查询"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_2"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:query_timetable"/>

                <Text
                    ohos:id="$+id:tv_grid_2"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="课表查询"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_3"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:query_exam"/>

                <Text
                    ohos:id="$+id:tv_grid_3"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="考试安排"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_4"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:query_credit"/>

                <Text
                    ohos:id="$+id:tv_grid_4"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="GPA"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_5"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:query_cult"/>

                <Text
                    ohos:id="$+id:tv_grid_5"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="培养方案"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_6"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:grade_total_1"/>

                <Text
                    ohos:id="$+id:tv_grid_6"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="成绩总表"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_7"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:query_zc"/>

                <Text
                    ohos:id="$+id:tv_grid_7"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="综合测评"/>
            </DirectionalLayout>


        </TableLayout>

        <DirectionalLayout
            ohos:height="20vp"
            ohos:width="match_parent"
            ohos:top_margin="4vp"
            ohos:background_element="#F7F7F7"
            ohos:orientation="horizontal">

            <Text
                ohos:height="match_parent"
                ohos:width="match_parent"
                ohos:left_margin="10vp"
                ohos:text="数据可视化"
                ohos:text_color="#000000"
                ohos:text_size="16fp">
            </Text>

        </DirectionalLayout>

        <TableLayout
            ohos:height="200vp"
            ohos:top_margin="4vp"
            ohos:width="match_parent"
            ohos:orientation="horizontal"
            ohos:padding="8vp"
            ohos:column_count="4"
            ohos:row_count="2"
            ohos:background_element="#F7F7F7">

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_8"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:grade_rate"/>

                <Text
                    ohos:id="$+id:tv_grid_8"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="成绩占比"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_9"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:grade_pk"/>

                <Text
                    ohos:id="$+id:tv_grid_9"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="成绩比较"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_10"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:grade_tend"/>

                <Text
                    ohos:id="$+id:tv_grid_10"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="GPA走势"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_11"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:single_major_2"/>

                <Text
                    ohos:id="$+id:tv_grid_11"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="单科分析"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_12"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:major_sort"/>

                <Text
                    ohos:id="$+id:tv_grid_12"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="专业排名"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_13"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:grade_loss"/>

                <Text
                    ohos:id="$+id:tv_grid_13"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="挂科分析"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_14"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:query_single_sort"/>

                <Text
                    ohos:id="$+id:tv_grid_14"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="单科排名"/>
            </DirectionalLayout>

            <DirectionalLayout
                ohos:width="match_content"
                ohos:height="match_content"
                ohos:alignment="center"
                ohos:orientation="vertical"
                ohos:padding="0vp">

                <Image
                    ohos:id="$+id:iv_grid_15"
                    ohos:width="85vp"
                    ohos:height="68vp"
                    ohos:image_src="$media:radar"/>

                <Text
                    ohos:id="$+id:tv_grid_15"
                    ohos:width="match_content"
                    ohos:height="20vp"
                    ohos:top_margin="3vp"
                    ohos:layout_alignment="center"
                    ohos:text_size="14fp"
                    ohos:text="个人分析"/>
            </DirectionalLayout>

        </TableLayout>

    </DirectionalLayout>

</DirectionalLayout>
  • 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
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432

  其中,TableLayout的属性信息如下所示:

<TableLayout
    ohos:id="$+id:table1"
    ohos:height="200vp"
    ohos:top_margin="4vp"
    ohos:width="match_parent"
    ohos:column_count="4"
    ohos:row_count="2"
    ohos:padding="8vp"
    ohos:orientation="horizontal"
    ohos:background_element="#F7F7F7">
</TableLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

  column_count和row_count分别表示列数和行数,本例中是2行4列。padding="8vp"表示布局内两个小的格子间的间隔为8vp。orientation是一个极其重要的属性,orientation="horizontal"表示横向布局,也就是TableLaout中包裹的东西会优先布满第一行,然后是第二行。

2.2 Ability文件

package com.example.ncepu.Student.Query;

import com.example.ncepu.ResourceTable;
import com.example.ncepu.Student.Query.slice.QueryFragmentSlice;
import com.youth.banner.loader.ImageLoader;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class QueryFragment extends Ability {

    private Banner banner;
    private List<Integer> list;
    private List<String> titles;
    private TableLayout layout1, layout2;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(QueryFragmentSlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_query_fragment);
        initViews();
        try {
            initBanner();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initViews() {
        layout1 = (TableLayout) findComponentById(ResourceTable.Id_table1);
        layout1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                switch (component.getId()) {
                    case ResourceTable.Id_iv_grid_1:
                        break;
                }
            }
        });
    }

    private void initBanner() throws NotExistException, WrongTypeException, IOException {
        list = new ArrayList<>();
        titles = new ArrayList<>();
        banner = (Banner) findComponentById(ResourceTable.Id_banner);
//        list.add(ResourceTable.Media_ncepu_1);
        list.add(ResourceTable.Media_ncepu_2);
        list.add(ResourceTable.Media_ncepu_3);
        list.add(ResourceTable.Media_ncepu_4);
        list.add(ResourceTable.Media_ncepu_5);
        list.add(ResourceTable.Media_ncepu_6);
        for(int i = 1; i <= 5; i++) {
            titles.add(String.valueOf(i));
        }
        //设置参数
        banner.setImages(list)
                .setBannerTitles(titles)
                .setDelayTime(3000)
                .setBannerStyle(5)
                .setTitleTextSize(60);
        banner.start();
    }
}
  • 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
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/315231?site=
推荐阅读
相关标签
  

闽ICP备14008679号