Android組件TabHost實現(xiàn)頁面中多個選項卡切換效果
TabHost組件可以在界面中存放多個選項卡, 很多軟件都使用了改組件進(jìn)行設(shè)計。
一、基礎(chǔ)知識
TabWidget : 該組件就是TabHost標(biāo)簽頁中上部 或者 下部的按鈕, 可以點擊按鈕切換選項卡;
TabSpec : 代表了選項卡界面, 添加一個TabSpec即可添加到TabHost中;
-- 創(chuàng)建選項卡 : newTabSpec(String tag), 創(chuàng)建一個選項卡;
-- 添加選項卡 : addTab(tabSpec);
二、實例講解
TabHost的基本使用,主要是layout的聲明要使用特定的id號,然后activity繼承TabActivity即可。
main.xml:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="aa" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="bb" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Main.java:
package com.app.main;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
public class Main extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = this.getTabHost();
TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator("tab1")
.setContent(R.id.tab1);
tabHost.addTab(tab1);
TabSpec tab2 = tabHost.newTabSpec("tab2").setIndicator("tab2")
.setContent(R.id.tab2);
tabHost.addTab(tab2);
}
}
實現(xiàn)效果:

其他:
當(dāng)點擊tabwidget的時候,若想注冊事件監(jiān)聽器,可以使用:
1.調(diào)用
tabHost.setOnTabChangedListener(new TabChangeListener(){
public void onTabChanged(String id)
{
}
});
這個傳入的id,就是tabwidget的indicator,這里是"tab1","tab2";
2.調(diào)用
tabWidget.getChildAt(0).setOnClickListener(new OnClickListener(){
});
以上就是本文的全部內(nèi)容,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android TabHost如何實現(xiàn)頂部選項卡
- Android開發(fā)之TabHost選項卡及相關(guān)疑難解決方法
- Android TabHost選項卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法
- android TabHost(選項卡)的使用方法
- android 選項卡(TabHost)如何放置在屏幕的底部
- Android TabLayout(選項卡布局)簡單用法實例分析
- Android多個TAB選項卡切換效果
- Android實現(xiàn)底部導(dǎo)航欄功能(選項卡)
- Android仿微信底部實現(xiàn)Tab選項卡切換效果
- android選項卡TabHost功能用法詳解
相關(guān)文章
詳解Android開發(fā)錄音和播放音頻的步驟(動態(tài)獲取權(quán)限)
這篇文章主要介紹了詳解Android開發(fā)錄音和播放音頻的步驟(動態(tài)獲取權(quán)限),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android實現(xiàn)取消GridView中Item選中時默認(rèn)的背景色
這篇文章主要介紹了Android實現(xiàn)取消GridView中Item選中時默認(rèn)的背景色,涉及Android GridView中Item屬性設(shè)置的相關(guān)技巧,需要的朋友可以參考下2016-02-02
android實現(xiàn)將位置信息寫入JPEG圖片文件
下面小編就為大家?guī)硪黄猘ndroid實現(xiàn)將位置信息寫入JPEG圖片文件。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
Android編程實現(xiàn)修改標(biāo)題欄位置使其居中的方法
這篇文章主要介紹了Android編程實現(xiàn)修改標(biāo)題欄位置使其居中的方法,涉及Android布局設(shè)置的簡單實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Android中Glide實現(xiàn)超簡單的圖片下載功能
本篇文章主要介紹了Android中Glide實現(xiàn)超簡單的圖片下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
如何利用Android Studio將moudle變成jar示例詳解
這篇文章主要給大家介紹了關(guān)于如何利用Android Studio將moudle變成jar的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08

