自學(xué)鴻蒙應(yīng)用開發(fā)(17)- TabList和Tab
本文介紹在鴻蒙應(yīng)用中TabList和TabList.Tab組件的基本用法。
準(zhǔn)備TabList頁面布局
在layout目錄下創(chuàng)建TabList布局,將其命名為ability_tablist.xml。
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><TabListohos: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"/><DirectionalLayoutohos:id="$+id:tab_container"ohos:height="match_parent"ohos:width="match_parent">DirectionalLayout>DirectionalLayout>
布局代碼中第7行~第22行的用于生成TabList組件,定義了TabList的基本屬性;第23行~第27行用于生成Tab頁面的容器,目前還沒有具體內(nèi)容,稍后有具體代碼生成。
準(zhǔn)備Image頁面
Image頁面主要包含一個Image文件和簡單的文字表示:
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Componentohos:height="0vp"ohos:weight="3"ohos:width="match_parent"/><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="center"ohos:orientation="vertical"><Imageohos:id="$+id:image"ohos:width="match_content"ohos:height="match_content"ohos:layout_alignment="center"ohos:image_src="$media:DevEco"/><Componentohos:height="20vp"ohos:width="match_parent"/><Textohos: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><Componentohos:height="0vp"ohos:weight="5"ohos:width="match_parent"/>DirectionalLayout>
準(zhǔn)備Video頁面
Video頁面包含一個動畫,稍微復(fù)雜一些。首先是頁面本身::
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Componentohos:height="0vp"ohos:weight="3"ohos:width="match_parent"/><DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_content"ohos:width="match_content"ohos:layout_alignment="center"ohos:orientation="vertical"><Componentohos:id="$+id:video_area"ohos:height="300vp"ohos:width="300vp"/><Componentohos:height="20vp"ohos:width="match_parent"/><Textohos: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><Componentohos:height="0vp"ohos:weight="5"ohos:width="match_parent"/>DirectionalLayout>
這個文件和Image頁面區(qū)別不大,只是在用Component組件代替了Image組件。而動畫的實際內(nèi)容則是由graphic目錄中的animation_element.xml文件決定:
<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;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); // 設(shè)置Tab的寬度tabList.setTabMargin(26); // 設(shè)置兩個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() {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);}}public void onUnselected(TabList.Tab tab) {if(tab.getText().equals("Video")) {frameAnimationElement.start();}ComponentContainer container = (ComponentContainer) findComponentById(ResourceTable.Id_tab_container);container.removeAllComponents();}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);}}});//最開始選選擇tab1tabList.selectTab(tab1);}public void onActive() {super.onActive();}public void onForeground(Intent intent) {super.onForeground(intent);}}
代碼第22行~第27行分別生成了Image和Video兩個Tab頁。
第29行~第69行是為TabList的各種事件提供響應(yīng)。需要響應(yīng)的處理主要有Tab頁選擇,Tab頁取消選擇和Tab重新選擇。代碼中根據(jù)當(dāng)前選中的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
動畫開發(fā)指導(dǎo)
https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ui-java-animation-0000000000580278
新書介紹
《實戰(zhàn)Python設(shè)計模式》是作者最近出版的新書,拜托多多關(guān)注!

本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計模式逐個進(jìn)行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設(shè)計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進(jìn)行說明,讓讀者明白在編寫代碼時如何判斷使用設(shè)計模式的利弊,并合理運用設(shè)計模式。
對設(shè)計模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計和開發(fā)的參考;使用Python 語言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>

