关于android:自学鸿蒙应用开发17-TabList和Tab

36次阅读

共计 5292 个字符,预计需要花费 14 分钟才能阅读完成。

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

具体步骤,可点击查看视频
筹备 TabList 页面布局

在 layout 目录下创立 TabList 布局,将其命名为 ability_tablist.xml。

<?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: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"/>
    <DirectionalLayout
        ohos:id="$+id:tab_container"
        ohos:height="match_parent"
        ohos:width="match_parent">
    </DirectionalLayout>
</DirectionalLayout>
<?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">
    <Component
        ohos:height="0vp"
        ohos:weight="3"
        ohos:width="match_parent"
    />
    <DirectionalLayout
        xmlns:ohos="http://schemas.huawei.com/res/ohos"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="center"
        ohos:orientation="vertical">
        <Component
            ohos:id="$+id:video_area"
            ohos:height="300vp"
            ohos:width="300vp"
            />
        <Component
            ohos:height="20vp"
            ohos:width="match_parent"
            />
        <Text
            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"
        />
    </DirectionalLayout>
    <Component
        ohos:height="0vp"
        ohos:weight="5"
        ohos:width="match_parent"
        />
</DirectionalLayout>

这个文件和 Image 页面区别不大,只是在用 Component 组件代替了 Image 组件。而动画的理论内容则是由 graphic 目录中的 animation_element.xml 文件决定:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:ohos="http://schemas.huawei.com/res/ohos"
                ohos:oneshot="false">
    <item ohos:element="$media:video01" ohos:duration="100"/>
    <item ohos:element="$media:video02" ohos:duration="100"/>
    <item ohos:element="$media:video03" ohos:duration="100"/>
    <item ohos:element="$media:video04" ohos:duration="100"/>
    <item ohos:element="$media:video05" ohos:duration="100"/>
    <item ohos:element="$media:video06" ohos:duration="100"/>
    <item ohos:element="$media:video07" ohos:duration="100"/>
    <item ohos:element="$media:video08" ohos:duration="100"/>
    <item ohos:element="$media:video09" ohos:duration="100"/>
    <item ohos:element="$media:video10" ohos:duration="100"/>
    <item ohos:element="$media:video11" ohos:duration="100"/>
</animation-list>

生成 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 组件
https://developer.harmonyos.c…

TabList 类

https://developer.harmonyos.c…

Tab List.Tab 类

https://developer.harmonyos.c…

动画开发领导

https://developer.harmonyos.c…

原文链接:https://developer.huawei.com/…
原作者:面向对象思考

正文完
 0