Android組件TabHost實現(xiàn)頁面中多個選項卡切換效果
TabHost組件可以在界面中存放多個選項卡, 很多軟件都使用了改組件進行設計。
一、基礎知識
TabWidget : 該組件就是TabHost標簽頁中上部 或者 下部的按鈕, 可以點擊按鈕切換選項卡;
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)效果:
其他:
當點擊tabwidget的時候,若想注冊事件監(jiān)聽器,可以使用:
1.調用
tabHost.setOnTabChangedListener(new TabChangeListener(){ public void onTabChanged(String id) { } });
這個傳入的id,就是tabwidget的indicator,這里是"tab1","tab2";
2.調用
tabWidget.getChildAt(0).setOnClickListener(new OnClickListener(){ });
以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Android實現(xiàn)Activity水平和垂直滾動條的方法
這篇文章主要介紹了Android實現(xiàn)Activity水平和垂直滾動條的方法,涉及Activity的ScrollView設置相關技巧,需要的朋友可以參考下2016-07-07Android搜索框(SearchView)的功能和用法詳解
這篇文章主要為大家詳細介紹了Android搜索框SearchView的功能和用法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05利用Android中的TextView實現(xiàn)逐字顯示動畫
在安卓程序啟動的時候,想逐字顯示一段話,每個字都有一個從透明到不透明的漸變動畫。那如何顯示這個效果,下面一起來看看。2016-08-08Android NotificationManager簡單使用詳解
這篇文章主要為大家詳細介紹了Android NotificationManager的簡單使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11