日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TabHost选项卡的实现(一):使用TabActivity实现

發布時間:2025/3/20 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TabHost选项卡的实现(一):使用TabActivity实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、 TabHost的基本開發流程

TabHost是一種非常實用的組件,可以很方便的在窗口上防止多個標簽頁,每個標簽頁相當于獲得了一個外部容器相同大小的組件擺放區域。

我們熟悉的手機電話系統“未接電話”、“已接電話”、“呼出電話”就可以使用TabHost實現。

1.1 與TabHost結合使用的組件

· TabWidget: 代表選項卡的標簽條。

· TabSpec: 代表選項卡的一個Tab頁面。

1.2 TabHost提供的創建選項卡方法

TabHost中常用方法;
·?void?addTab(TabHost.TabSpec tabSpec) 添加一個標簽頁
·?int getCurrentTab() 獲取當前的標簽頁id
·?String?getCurrentTabTag() 獲取當前的標簽頁的Tag
·?TabHost.TabSpec ?newTabSpec(String tag) 創建一個TabSpec
·?void?setCurrentTab(int index) 設置一個標簽頁的id

·?void?setCurrentTabByTag(String tag) 設置一個標簽頁的Tag


TabSpec中常用的方法:

·?String?getTag()?
·?TabHost.TabSpec setContent(int viewId) 通過指定布局文件,設置標簽頁的內容
·?TabHost.TabSpec setContent(Intent intent) 通過指定Intent所指定的Activity,設置標簽頁的內容
·?TabHost.TabSpec setIndicator(CharSequence label) 設置該標簽頁的標題
·?TabHost.TabSpec setIndicator(CharSequence label, Drawable icon)? 設置該標簽頁的標題和圖標

1.3 開發流程

· 在界面布局中定義TabHost組件,并為該組件定義該選項卡的內容

· Activity應該繼承TabActivity。

· 調用TabActivity的getTabHost()方法獲取TabHost對象。

· 通過TabHost對象的方法來創建、添加選項卡。


二、 實現TabHost布局的通話記錄界面

1) 首先,我們定義TabHost需要顯示的xml界面:

<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="wrap_content" /><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent" ><!-- 定義第一個標簽頁的內容 --><LinearLayoutandroid:id="@+id/tab01"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="殺生丸 - 2014-9-26"android:textSize="11pt" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="犬夜叉 - 2014-9-26"android:textSize="11pt" /></LinearLayout><!-- 定義第二個標簽頁的內容 --><LinearLayoutandroid:id="@+id/tab02"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="桔梗 - 2014年9月26日"android:textSize="11pt" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="彌勒 - 2014年9月26日"android:textSize="11pt" /></LinearLayout><!-- 定義第三個標簽頁的內容 --><LinearLayoutandroid:id="@+id/tab03"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:textSize="11pt" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="銀月 - 2014年9月26日"android:textSize="11pt" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="寒食 - 2014-9-26"android:textSize="11pt" /></LinearLayout></FrameLayout></LinearLayout> </TabHost>


對于上述布局文件,我們有三個地方的ID需要注意:

1. TabHost的ID應該為@android:id/tabhost

2. TabWidget的ID應該為@android:id/tabs

3. FrameLayout的ID應該為@android:id/tabcontent

這三個ID并不是程序員自定義的,而是Android系統提供的,必須按照如此方法設置。


2) Java代碼,將三個tab添加到容器中



public class MainActivity extends TabActivity {@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TabHost tabHost = getTabHost();TabSpec ts1 = tabHost.newTabSpec("tab1").setIndicator("已接來電").setContent(R.id.tab01);TabSpec ts2 = tabHost.newTabSpec("tab2").setIndicator("未接來電").setContent(R.id.tab02);TabSpec ts3 = tabHost.newTabSpec("tab3").setIndicator("呼出電話").setContent(R.id.tab03);tabHost.addTab(ts1);tabHost.addTab(ts2);tabHost.addTab(ts3);} }

效果圖:




說明; 如今,我們已經不再推薦使用TabActivity來創建標簽頁了,更加推薦使用Fragment代替TabActivity,請看下章講解如何去實現Fragment的TabHost。

轉載于:https://www.cnblogs.com/hehe520/p/6330014.html

總結

以上是生活随笔為你收集整理的TabHost选项卡的实现(一):使用TabActivity实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。