鸿蒙应用开发 | 选项卡(TabList / Tab)的功能和用法

简介
Tablist可以实现多个页签栏的切换,Tab为某个页签。子页签通常放在内容区上方,展示不同的分类。页签名称应该简洁明了,清晰描述分类的内容。

一般应用场景 都是作为应用首页的 二级菜单的多个分类:

用到的属性
Tablist的共有XML属性继承自:ScrollView

图片

图片

图片

图片

官网属性一大堆,看到都头大,其实使用起来就几个属性。


常用接口:

实战

1,创建项目,添加默认Tablist

<TabList
    ohos:id="$+id:tab_list"
    ohos:text_size="20fp"
    ohos:height="50vp"
    ohos:width="match_parent"
    ohos:layout_alignment="center"
    ohos:orientation="horizontal"
    ohos:text_alignment="center"
/>

添加Tab

TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
TabList.Tab tab = tabList.new Tab(getContext());
tab.setText("推荐");
tabList.addTab(tab);
TabList.Tab tab2 = tabList.new Tab(getContext());
tab2.setText("视频");
tabList.addTab(tab2);
TabList.Tab tab3 = tabList.new Tab(getContext());
tab3.setText("新闻");
tabList.addTab(tab3);
TabList.Tab tab4 = tabList.new Tab(getContext());
tab4.setText("问答");
tabList.addTab(tab4);

新增选项,在两个之间插入一个。

// 本示例中在"推荐"和"视频"之间的页签中新增"资源"页签
  TabList.Tab tab5 = tabList.new Tab(getContext());
  tab5.setText("资源");
  tab5.setMinWidth(80);
  tab5.setPadding(12, 0, 12, 0);
  tabList.addTab(tab5, 1); // 1 表示位置,在第一个后新增一个

默认选中第几个:

// 默认选中 某一个tab
  tab2.select();
  tab2.getPosition(); // 获取tab的当前索引下标

设置点击某一个选中后的 操作:
 

// 设置监听
  tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
      @Override
      public void onSelected(TabList.Tab tab) {
          // 当某个Tab从未选中状态变为选中状态时的回调
          System.out.println("名称:"+tab.getText()+"- 下标:"+tab.getPosition());
          text_title.setText(tab.getText());
      }
 
      @Override
      public void onUnselected(TabList.Tab tab) {
          // 当某个Tab从选中状态变为未选中状态时的回调
      }
 
      @Override
      public void onReselected(TabList.Tab tab) {
          // 当某个Tab已处于选中状态,再次被点击时的状态回调
      }
  });

设置显示样式

<TabList
    ohos:id="$+id:tab_list"
    ohos:text_size="20fp"
    ohos:height="50vp"
    ohos:width="match_parent"
    ohos:layout_alignment="center"
    ohos:orientation="horizontal"
    ohos:text_alignment="center"
 
    ohos:tab_margin="20vp" //tab间距
    ohos:tab_length="60vp" //tab长度
 
    ohos:normal_text_color="#000000" //未选中字体颜色
    ohos:selected_text_color="#CE0000" // 选中字体样式
 
    ohos:selected_tab_indicator_color="#CE0000" // 选中下面的划线样式
    ohos:selected_tab_indicator_height="2vp"/> //选中划线的高度

完整代码:

<?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:orientation="vertical">
 
    <TabList
        ohos:id="$+id:tab_list"
        ohos:text_size="20fp"
        ohos:height="50vp"
        ohos:width="match_parent"
        ohos:layout_alignment="center"
        ohos:orientation="horizontal"
        ohos:text_alignment="center"
 
        ohos:tab_margin="20vp"
        ohos:tab_length="60vp"
 
        ohos:normal_text_color="#000000"
        ohos:selected_text_color="#CE0000"
 
        ohos:selected_tab_indicator_color="#CE0000"
        ohos:selected_tab_indicator_height="2vp"/>
 
    <DirectionalLayout
        ohos:height="1vp"
        ohos:width="match_parent"
        ohos:top_margin="10vp"
        ohos:background_element="#333333"/>
 
    <Text
        ohos:id="$+id:text_title"
        ohos:text_size="20vp"
        ohos:text="内容"
        ohos:weight="1"
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:text_alignment="center"/>
 
</DirectionalLayout>
package com.example.tablist_tab.slice;
 
import com.example.tablist_tab.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.TabList;
import ohos.agp.components.Text;
 
public class MainAbilitySlice extends AbilitySlice {
    private Text text_title;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
 
        text_title = (Text) findComponentById(ResourceTable.Id_text_title);
 
        TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        TabList.Tab tab = tabList.new Tab(getContext());
        tab.setText("推荐");
        tabList.addTab(tab);
        TabList.Tab tab2 = tabList.new Tab(getContext());
        tab2.setText("视频");
        tabList.addTab(tab2);
        TabList.Tab tab3 = tabList.new Tab(getContext());
        tab3.setText("新闻");
        tabList.addTab(tab3);
        TabList.Tab tab4 = tabList.new Tab(getContext());
        tab4.setText("问答");
        tabList.addTab(tab4);
 
 
        // 新增选项
        // 本示例中在"推荐"和"视频"之间的页签中新增"资源"页签
        TabList.Tab tab5 = tabList.new Tab(getContext());
        tab5.setText("资源");
        tab5.setMinWidth(80);
        tab5.setPadding(12, 0, 12, 0);
        tabList.addTab(tab5, 1); // 1 表示位置,在第一个后新增一个
 
        // 默认选中 某一个tab
        tab2.select();
        tab2.getPosition(); // 获取tab的当前索引下标
 
        // 设置监听
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                // 当某个Tab从未选中状态变为选中状态时的回调
                System.out.println("名称:"+tab.getText()+"- 下标:"+tab.getPosition());
                text_title.setText(tab.getText());
            }
 
            @Override
            public void onUnselected(TabList.Tab tab) {
                // 当某个Tab从选中状态变为未选中状态时的回调
            }
 
            @Override
            public void onReselected(TabList.Tab tab) {
                // 当某个Tab已处于选中状态,再次被点击时的状态回调
            }
        });
    }
 
    @Override
    public void onActive() {
        super.onActive();
    }
 
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.youkuaiyun.com/jianpengxuexikaifa/article/details/119395325

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值