Android TabHost組件使用方法詳解
最近研究了一下Contacts源碼,仿照上面自己寫了一個TabHostTest程序,現(xiàn)整理如下:
main.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"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" /> </LinearLayout> </TabHost>
inner.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"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </TabHost>
Main.java (主Activity類):
package com.android.test;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.CallLog.Calls;
import android.provider.Contacts.Intents.UI;
import android.view.Window;
import android.widget.TabHost;
public class Main extends TabActivity implements TabHost.OnTabChangeListener {
private static final int TAB_INDEX_DIALER = 0;
private static final int TAB_INDEX_CALL_LOG = 1;
private static final int TAB_INDEX_CONTACTS = 2;
private static final int TAB_INDEX_FAVORITES = 3;
private TabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mTabHost = getTabHost();
mTabHost.setOnTabChangedListener(this);
// Setup the tabs
setupDialerTab();
setupCallLogTab();
setupContactsTab();
setupFavoritesTab();
setCurrentTab(intent);
}
public void onTabChanged(String tabId) {
Activity activity = getLocalActivityManager().getActivity(tabId);
if (activity != null) {
activity.onWindowFocusChanged(true);
}
}
private void setupCallLogTab() {
// Force the class since overriding tab entries doesn't work
Intent intent = new Intent("com.android.phone.action.RECENT_CALLS");
intent.setClass(this, Inner.class);
mTabHost.addTab(mTabHost.newTabSpec("call_log")
.setIndicator("通話記錄",
getResources().getDrawable(R.drawable.ic_tab_unselected_recent))
.setContent(intent));
}
private void setupDialerTab() {
Intent intent = new Intent("com.android.phone.action.TOUCH_DIALER");
intent.setClass(this, Inner.class);
mTabHost.addTab(mTabHost.newTabSpec("dialer")
.setIndicator("撥號",
getResources().getDrawable(R.drawable.ic_tab_unselected_dialer))
.setContent(intent));
}
private void setupContactsTab() {
Intent intent = new Intent(UI.LIST_DEFAULT);
intent.setClass(this, Main.class);
mTabHost.addTab(mTabHost.newTabSpec("contacts")
.setIndicator("通訊錄",
getResources().getDrawable(R.drawable.ic_tab_unselected_contacts))
.setContent(intent));
}
private void setupFavoritesTab() {
Intent intent = new Intent(UI.LIST_STREQUENT_ACTION);
intent.setClass(this, Inner.class);
mTabHost.addTab(mTabHost.newTabSpec("favorites")
.setIndicator("收藏",
getResources().getDrawable(R.drawable.ic_tab_unselected_starred))
.setContent(intent));
}
/**
* Sets the current tab based on the intent's request type
*
* @param intent Intent that contains information about which tab should be selected
*/
private void setCurrentTab(Intent intent) {
// Dismiss menu provided by any children activities
Activity activity = getLocalActivityManager().
getActivity(mTabHost.getCurrentTabTag());
if (activity != null) {
activity.closeOptionsMenu();
}
// Tell the children activities that they should ignore any possible saved
// state and instead reload their state from the parent's intent
intent.putExtra("", true);
// Choose the tab based on the inbound intent
String componentName = intent.getComponent().getClassName();
if (getClass().getName().equals(componentName)) {
if (false) {
//in a call, show the dialer tab(which allows going back to the call)
mTabHost.setCurrentTab(TAB_INDEX_DIALER);
} else if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
// launched from history (long-press home) --> nothing to change
} else if (true) {
// The dialer was explicitly requested
mTabHost.setCurrentTab(TAB_INDEX_DIALER);
}
}
}
}
Inner.java類:
package com.android.test;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
public class Inner extends TabActivity implements TabHost.OnTabChangeListener {
private static final int TAB_INDEX_ALL = 0;
private static final int TAB_INDEX_MISSED = 1;
private static final int TAB_INDEX_OUTGOING = 2;
private static final int TAB_INDEX_RECEIVED = 3;
private TabHost mTabHost;
private TabWidget mTabWidget;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.inner);
mTabHost = getTabHost();
mTabHost.setOnTabChangedListener(this);
setupTabs();
mTabWidget = mTabHost.getTabWidget();
mTabWidget.setStripEnabled(false);
for (int i = 0; i < mTabWidget.getChildCount(); i++) {
TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(
android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(
android.R.color.white));
tv.setPadding(0, 0, 0,(int) tv.getTextSize());
tv.setText("Tab" + i);
mTabWidget.getChildAt(i).getLayoutParams().height =(int ) (3* tv.getTextSize());
mTabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
}
}
public void onTabChanged(String tabId) {
}
private void setupTabs() {
mTabHost.addTab(mTabHost.newTabSpec("all").setIndicator(
getString(R.string.inner)).setContent(
new Intent(this, Other.class)));
mTabHost.addTab(mTabHost.newTabSpec("Missed").setIndicator(
getString(R.string.inner)).setContent(
new Intent(this, Other.class)));
mTabHost.addTab(mTabHost.newTabSpec("Outgoing").setIndicator(
getString(R.string.inner)).setContent(
new Intent(this, Other.class)));
mTabHost.addTab(mTabHost.newTabSpec("Received").setIndicator(
getString(R.string.inner)).setContent(
new Intent(this, Other.class)));
}
}
效果圖如下:

以上就是本文的全部內(nèi)容,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android組件TabHost實現(xiàn)頁面中多個選項卡切換效果
- android TabHost(選項卡)的使用方法
- android 選項卡(TabHost)如何放置在屏幕的底部
- Android組件必學之TabHost使用方法詳解
- Android控件之TabHost用法實例分析
- Android 使用FragmentTabhost代替Tabhost
- 詳解Android應用中使用TabHost組件進行布局的基本方法
- Android編程實現(xiàn)設置TabHost當中字體的方法
- 詳解Android TabHost的多種實現(xiàn)方法 附源碼下載
- Android Tabhost使用方法詳解
- Android TabHost選項卡標簽圖標始終不出現(xiàn)的解決方法
相關(guān)文章
Android連接MySQL數(shù)據(jù)庫并進行增刪改查操作示例講解
這篇文章主要介紹了Android 連接MySQL數(shù)據(jù)庫并進行增刪改查操作示例講解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
android listview優(yōu)化幾種寫法詳細介紹
這篇文章只是總結(jié)下getView里面優(yōu)化視圖的幾種寫法,需要的朋友可以參考下2012-11-11
android使用ViewPager實現(xiàn)圖片自動切換
這篇文章主要為大家詳細介紹了android使用ViewPager實現(xiàn)圖片自動切換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02
Flutter 控制屏幕旋轉(zhuǎn)的實現(xiàn)
這篇文章主要介紹了Flutter 控制屏幕旋轉(zhuǎn)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
Android判斷用戶2G/3G/4G移動數(shù)據(jù)網(wǎng)絡
這篇文章主要介紹了Android判斷用戶2G/3G/4G移動數(shù)據(jù)網(wǎng)絡的方法,感興趣的小伙伴們可以參考一下2015-12-12

