自學鴻蒙應用開發(17)- TabList和Tab

華為開發者論壇發表於2021-05-07

本文介紹在鴻蒙應用中TabList和TabList.Tab元件的基本用法。


具體步驟,可點選


準備TabList頁面佈局

在layout目錄下建立TabList佈局,將其命名為ability_tablist.xml。


<?xml version="1.0" encoding="utf-8"?><DirectionalLayout    xmlns:ohos="


佈局程式碼中第7行~第22行的用於生成TabList元件,定義了TabList的基本屬性;第23行~第27行用於生成Tab頁面的容器,目前還沒有具體內容,稍後有具體程式碼生成。


<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:ohos=">


準備Image頁面

Image頁面主要包含一個Image檔案和簡單的文字表示:


<?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">        <Image            ohos:id="$+id:image"            ohos:width="match_content"            ohos:height="match_content"            ohos:layout_alignment="center"            ohos:image_src="$media:DevEco"        />        <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="Image Tab"            ohos:text_color="#007F00"            ohos:text_size="100"        />    </DirectionalLayout>    <Component        ohos:height="0vp"        ohos:weight="5"        ohos:width="match_parent"        /></DirectionalLayout>


準備Video頁面

Video頁面包含一個動畫,稍微複雜一些。首先是頁面本身:


<?xml version="1.0" encoding="utf-8"?><DirectionalLayout    xmlns:ohos="


這個檔案和Image頁面區別不大,只是在用Component元件代替了Image元件。而動畫的實際內容則是由graphic目錄中的animation_element.xml檔案決定:


<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:ohos=">


生成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.com/cn/docs/documentation/doc-guides/ui-java-component-tablist-tab-0000001062229749


TabList類

https://developer.harmonyos.com/cn/docs/documentation/doc-references/tablist-0000001054238721


Tab List.Tab類

https://developer.harmonyos.com/cn/docs/documentation/doc-references/tablist_tab-0000001054678691


動畫開發指導

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-animation-0000000000580278


原文連結:

https://developer.huawei.com/consumer/cn/forum/topic/0201551132050440304?fid=0101303901040230869

原作者:物件導向思考


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69970551/viewspace-2771154/,如需轉載,請註明出處,否則將追究法律責任。

相關文章