自学鸿蒙应用,自学鸿蒙应用开发(17)- TabList和Tab

本文详细介绍了如何在鸿蒙应用中使用TabList和TabList.Tab组件创建页面布局,包括设置Tab的宽度、间距和颜色等属性。同时,通过代码展示了如何生成Image和Video两个Tab页面,并响应Tab的选择、取消选择和重新选择事件,动态加载不同组件。文章还提及了动画元素在graphic目录中的animation_element.xml文件中定义,实现了Tab切换时的动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文介绍在鸿蒙应用中TabList和TabList.Tab组件的基本用法。

具体步骤,可点击查看视频

准备TabList页面布局

在layout目录下创建TabList布局,将其命名为ability_tablist.xml。<?xml version="1.0" encoding="utf-8"?>

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:orientation="vertical">

ohos:id="$+id:tab_list"

ohos:background_element="#FFFF7F"

ohos:top_margin="10vp"

ohos:tab_margin="24vp"

ohos:tab_length="140vp"

ohos:text_size="20fp"

ohos:height="36vp"

ohos:width="match_parent"

ohos:layout_alignment="center"

ohos:orientation="horizontal"

ohos:text_alignment="center"

ohos:normal_text_color="#999999"

ohos:selected_text_color="#000000"

ohos:selected_tab_indicator_color="#000000"

ohos:selected_tab_indicator_height="2vp"/>

ohos:id="$+id:tab_container"

ohos:height="match_parent"

ohos:width="match_parent">

<?xml version="1.0" encoding="utf-8"?>

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:orientation="vertical">

ohos:height="0vp"

ohos:weight="3"

ohos:width="match_parent"

/>

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:height="match_content"

ohos:width="match_content"

ohos:layout_alignment="center"

ohos:orientation="vertical">

ohos:id="$+id:video_area"

ohos:height="300vp"

ohos:width="300vp"

/>

ohos:height="20vp"

ohos:width="match_parent"

/>

ohos:id="$+id:text_helloworld"

ohos:height="match_content"

ohos:width="match_content"

ohos:layout_alignment="horizontal_center"

ohos:text="Video Tab"

ohos:text_color="#0000FF"

ohos:text_size="100"

/>

ohos:height="0vp"

ohos:weight="5"

ohos:width="match_parent"

/>

这个文件和Image页面区别不大,只是在用Component组件代替了Image组件。而动画的实际内容则是由graphic目录中的animation_element.xml文件决定:<?xml version="1.0" encoding="utf-8"?>

ohos:oneshot="false">

生成TabList画面

TabList的每个Tab页面需要由代码生成,具体参见下面的页面类:package com.example.helloharmony.slice;

import com.example.helloharmony.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;

import ohos.aafwk.content.Intent;

import ohos.agp.components.*;

import ohos.agp.components.element.FrameAnimationElement;

import java.io.Console;

public class TablistAbilitySlice extends AbilitySlice {

private Component imageContent;

private Component videoContent;

private FrameAnimationElement frameAnimationElement;

@Override

public void onStart(Intent intent) {

super.onStart(intent);

super.setUIContent(ResourceTable.Layout_ability_tablist);

TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);

tabList.setTabLength(200); // 设置Tab的宽度

tabList.setTabMargin(26); // 设置两个Tab之间的间距

TabList.Tab tab1 = tabList.new Tab(getContext());

tab1.setText("Image");

tabList.addTab(tab1);

TabList.Tab tab2 = tabList.new Tab(getContext());

tab2.setText("Video");

tabList.addTab(tab2);

AbilitySlice slice = this;

tabList.addTabSelectedListener(new TabList.TabSelectedListener() {

@Override

public void onSelected(TabList.Tab tab) {

ComponentContainer container = (ComponentContainer) findComponentById(ResourceTable.Id_tab_container);

if(tab.getText().equals("Image")) {

imageContent = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_iamge_tab, null, false);

container.addComponent(imageContent);

}

else

{

videoContent = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_video_tab, null, false);

frameAnimationElement = new FrameAnimationElement(slice.getContext(), ResourceTable.Graphic_animation_element);

Component videoArea = videoContent.findComponentById(ResourceTable.Id_video_area);

videoArea.setBackground(frameAnimationElement);

frameAnimationElement.start();

container.addComponent(videoContent);

}

}

@Override

public void onUnselected(TabList.Tab tab) {

if(tab.getText().equals("Video")) {

frameAnimationElement.start();

}

ComponentContainer container = (ComponentContainer) findComponentById(ResourceTable.Id_tab_container);

container.removeAllComponents();

}

@Override

public void onReselected(TabList.Tab tab) {

ComponentContainer container = (ComponentContainer) findComponentById(ResourceTable.Id_tab_container);

if(tab.getText().equals("Image")) {

container.addComponent(imageContent);

}

else

{

frameAnimationElement.start();

container.addComponent(videoContent);

}

}

});

//最开始选选择tab1

tabList.selectTab(tab1);

}

@Override

public void onActive() {

super.onActive();

}

@Override

public void onForeground(Intent intent) {

super.onForeground(intent);

}

}

代码第22行~第27行分别生成了Image和Video两个Tab页。

第29行~第69行是为TabList的各种事件提供响应。需要响应的处理主要有Tab页选择,Tab页取消选择和Tab重新选择。代码中根据当前选中的Tab页面生成或选择不同的组件。

参考文档

TabList类

Tab List.Tab类

动画开发指导

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值