HarmonyOS之常用组件TabList与Tab的功能和使用

一、什么是 Tablist 与 Tab ?
  • Tablist 可以实现多个页签栏的切换,Tab 为某个页签。
  • 子页签通常放在内容区上方,展示不同的分类。
  • 页签名称应该简洁明了,清晰描述分类的内容。
二、支持的 XML 属性
属性名称中文描述取值取值说明使用案例
fixed_mode固定所有页签并同时显示boolean类型可以直接设置true/false,也可以引用boolean资源ohos:fixed_mode="true"
ohos:fixed_mode="$boolean:true_tag"
orientation页签排列方向horizontal表示水平排列ohos:orientation="horizontal"
vertical表示垂直排列ohos:orientation="vertical"
normal_text_color未选中的文本颜色color类型可以直接设置色值,也可以引用color资源ohos:normal_text_color="#FFFFFFFF"
ohos:normal_text_color="$color:black"
selected_text_color选中的文本颜色color类型可以直接设置色值,也可以引用color资源ohos:normal_text_color="#FFFFFFFF"
ohos:normal_text_color="$color:black"
selected_tab_indicator_color选中页签的颜色color类型可以直接设置色值,也可以引用color资源ohos:normal_text_color="#FFFFFFFF"
ohos:normal_text_color="$color:black"
selected_tab_indicator_height选中页签的高度float类型表示尺寸的float类型。可以是浮点数值,其默认单位为px;也可以是带px/vp/fp单位的浮点数值;也可以引用float资源ohos:tab_length="100"
ohos:tab_length="20vp"
ohos:tab_length="$float:size_value"
tab_indicator_type页签指示类型invisible表示选中的页签无指示标记ohos:tab_indicator_type="invisible"
bottom_line表示选中的页签通过底部下划线标记ohos:tab_indicator_type="bottom_line"
left_line表示选中的页签通过左侧分割线标记ohos:tab_indicator_type="left_line"
oval表示选中的页签通过椭圆背景标记ohos:tab_indicator_type="oval"
tab_length页签长度float类型表示尺寸的float类型。可以是浮点数值,其默认单位为px;也可以是带px/vp/fp单位的浮点数值;也可以引用float资源ohos:tab_length="100"
ohos:tab_length="20vp"
ohos:tab_length="$float:size_value"
tab_margin页签间距float类型表示尺寸的float类型。可以是浮点数值,其默认单位为px;也可以是带px/vp/fp单位的浮点数值;也可以引用float资源
text_alignment文本对齐方式left表示文本靠左对齐可以设置取值项如表中所列,也可以使用“|”进行多项组合。
ohos:text_alignment="center"
ohos:text_alignment="top|left"
top表示文本靠顶部对齐
right表示文本靠右对齐
bottom表示文本靠底部对齐
horizontal_center表示文本水平居中对齐
vertical_center表示文本垂直居中对齐
center表示文本居中对齐
start表示文本靠起始端对齐
end表示文本靠结尾端对齐
text_size文本大小float类型表示尺寸的float类型。可以是浮点数值,其默认单位为px;也可以是带px/vp/fp单位的浮点数值;也可以引用float资源ohos:text_size="100"
ohos:text_size="16fp"
ohos:text_size="$float:size_value"
三、TabList 的使用
① TabList 创建
  • xml 中创建 TabList
	<?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:background_element="black"
	    ohos:orientation="vertical">
	
	   <TabList
	       ohos:id="$+id:tab_list"
	       ohos:top_margin="40vp"
	       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" />
	
	</DirectionalLayout>
  • 设置默认状态和选中状态的字体颜色和 indicator 的颜色:
	<TabList
	    ...
	    ohos:normal_text_color="#999999"
	    ohos:selected_text_color="#FFFFFF"
	    ohos:selected_tab_indicator_color="#FFFFFF"
	    ohos:selected_tab_indicator_height="2vp"/>
  • 或者在代码中设置 Tab 的布局:
	tabList.setTabLength(140 * 3); // 设置Tab的宽度
	tabList.setTabMargin(24 * 3); // 设置两个Tab之间的间距
  • TabList 中添加 Tab:
	TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
	TabList.Tab tab = tabList.new Tab(getContext());
	tab.setText("Image");
	tabList.addTab(tab);
	... // 请自行创建其他Tab
  • 效果如下:
    在这里插入图片描述
② 设置 fixedMode
  • 默认为 false,该模式下 TabList 的总宽度是各 Tab 宽度的总和,若固定了 TabList 的宽度,当超出可视区域,则可以通过滑动 TabList 来显示。
  • 如果设置为 true,TabList 的总宽度将与可视区域相同,各个 Tab 的宽度也会根据 TabList 的宽度而平均分配,该模式适用于 Tab 较少的情况。
	tabList.setFixedMode(true);
  • fixedMode 设置 false/true 前后的对比效果:

在这里插入图片描述

③ 在某个位置新增 Tab
	// 本示例中在"图片"和"视频"之间的页签中新增"新闻"页签
	TabList.Tab tab = tabList.new Tab(getContext());
	tab.setText("News");
	tab.setMinWidth(64);
	tab.setPadding(12, 0, 12, 0);
	tabList.addTab(tab, 1); // 1表示位置
  • 在某一位置新增 tab 的效果:

在这里插入图片描述

④ 响应焦点变化
	tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
	    @Override
	    public void onSelected(TabList.Tab tab) {
	        // 当某个Tab从未选中状态变为选中状态时的回调
	        ...
	    }
	
	    @Override
	    public void onUnselected(TabList.Tab tab) {
	        // 当某个Tab从选中状态变为未选中状态时的回调
	        ...
	    }
	
	    @Override
	    public void onReselected(TabList.Tab tab) {
	        // 当某个Tab已处于选中状态,再次被点击时的状态回调
	        ...
	    }
	});
⑤ TabList 常用接口
方法说明
getSelectedTab返回选中的Tab
getSelectedTabIndex返回选中的Tab的位置索引
getTabCount获取Tab的个数
getTabAt获取某个Tab
removeTab移除某个位置的tab
setOrientation设置横或竖方向
四、Tab 的使用
  • 设置 Tab 属性:
	tab.setMinWidth(64);
	tab.setPadding(12, 0, 12, 0);
  • 选中某个 Tab:
	tab.select();
  • 获取 Tab 在 TabList 中的位置索引:
	tab.getPosition();
uni-tab-bar 是一个底部选项卡组件,可以让用户在不同的页面之间进行切换。使用方法是在页面的 template 中引入该组件,然后在 script 中配置选项卡的样式和每个选项卡对应的页面路径。用户点击选项卡时,会自动跳转到对应的页面。以下是一个示例代码: ```html <template> <view> <uni-tab-bar :tab-list="tabList" :current="current" @change="onChange"></uni-tab-bar> </view> </template> <script> export default { data() { return { current: 0, tabList: [ { iconPath: "/static/home.png", selectedIconPath: "/static/home-active.png", text: "首页", pagePath: "/pages/home/home" }, { iconPath: "/static/user.png", selectedIconPath: "/static/user-active.png", text: "个人中心", pagePath: "/pages/user/user" } ] } }, methods: { onChange(event) { this.current = event.currentTarget.dataset.index uni.switchTab({ url: this.tabList[this.current].pagePath }) } } } </script> ``` uni-list 是一个列表组件,可以用来展示一组数据。使用方法是在页面的 template 中引入该组件,然后在 script 中配置列表的数据来源和每个列表项的样式。以下是一个示例代码: ```html <template> <view> <uni-list> <uni-list-item v-for="(item, index) in list" :key="index" :title="item.title" :note="item.note"></uni-list-item> </uni-list> </view> </template> <script> export default { data() { return { list: [ { title: "标题1", note: "描述1" }, { title: "标题2", note: "描述2" } ] } } } </script> ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

╰つ栺尖篴夢ゞ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值