Android開(kāi)發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法
本文實(shí)例分析了Android開(kāi)發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法。分享給大家供大家參考,具體如下:
前言:
雖然現(xiàn)在谷歌已經(jīng)不推薦使用TabHost,但是初學(xué)者還是很有必要接觸下這一成金的經(jīng)典的,本文將介紹纖細(xì)介紹這一空間的使用,以及大家可能遇到的問(wèn)題。注:文末給出完整實(shí)現(xiàn)代碼
三個(gè)問(wèn)題:
1. 無(wú)法顯示TabHost
2. 添加圖片 + 文字 無(wú)法同時(shí)
3. 說(shuō)在最后:點(diǎn)擊事件
4. 底部導(dǎo)航無(wú)法實(shí)現(xiàn)
現(xiàn)在
從問(wèn)題出發(fā):
問(wèn)題一:無(wú)法顯示 TabHost
很多人調(diào)用TabHost的方法是:
setContentView(R.layout.activity_main); tabHost = getTabHost();
然后發(fā)現(xiàn)啥也沒(méi)有,一臉蒙圈。。。 在這里建議大家采用遮掩的調(diào)用方法:
LayoutInflater.from(this).inflate(R.layout.activity_main,
tabHost.getTabContentView(), true);
成功后的頁(yè)面:

注:UI 略丑請(qǐng)忽視
問(wèn)題二:圖片、文字無(wú)法同時(shí)添加
好了,很多人辛辛苦苦把界面搞出來(lái)了,可能想搞個(gè)底部菜單 加個(gè)圖片,結(jié)果涼涼 半天搞不出來(lái) ,這里介紹一個(gè)方法 ,由于TabHost本身圖片、文字沖突 ,無(wú)法添加,這是我們就得把目光遷移到自定義view上:本段參考自:http://www.dbjr.com.cn/article/157914.htm
首先在/layout下建立自定義view名為:tab_indicator.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_weight="1"
android:orientation="vertical"
android:background="#45c0c0c0"
android:padding="5dp">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
/>
</RelativeLayout>
接著,緊隨其后在/drawable下添加:tab_info.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/find"
android:state_selected="true" />
<item android:drawable="@drawable/find1" />
</selector>
這些都搞定之后,就可以在活動(dòng)中調(diào)用了:
首先在活動(dòng)中先建立AddTab()方法:
private void AddTab(String label, int drawableId) {
Intent intent = new Intent(this, TextActivity.class);
TabHost.TabSpec spec = tabHost.newTabSpec(label);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(label);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
終于我們。。。:

成功了?。?!
問(wèn)題三:添加監(jiān)聽(tīng)事件
這個(gè)無(wú)腦 只要 id 匹配就行了,直接上代碼:
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
@Override
// tabId是newTabSpec參數(shù)設(shè)置的tab頁(yè)名,并不是layout里面的標(biāo)識(shí)符id
public void onTabChanged(String tabId) {
if (tabId.equals("tab1")) { //第一個(gè)標(biāo)簽
Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁(yè)一", Toast.LENGTH_SHORT).show();
}else if (tabId.equals("tab2")) { //第二個(gè)標(biāo)簽
Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁(yè)二", Toast.LENGTH_SHORT).show();
}else if (tabId.equals("tab3")) { //第三個(gè)標(biāo)簽
Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁(yè)三", Toast.LENGTH_SHORT).show();
}
}
});
暫時(shí)能記起來(lái)的 疑難就這些了 如果還有請(qǐng)給我留言 我盡力解答。。
附上布局與實(shí)現(xiàn):
布局:
<?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"
android:scrollbarSize="100dp">
<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">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--定義第一個(gè)標(biāo)簽頁(yè)特內(nèi)容-->
<LinearLayout
android:id="@+id/tab01"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text11"
android:textSize="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text12"
android:textSize="20dp"/>
</LinearLayout>
<!--定義第二個(gè)標(biāo)簽頁(yè)的內(nèi)容-->
<LinearLayout
android:id="@+id/tab02"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text11"
android:textSize="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text12"
android:textSize="20dp"/>
</LinearLayout>
<!--定義第三個(gè)標(biāo)簽頁(yè)的內(nèi)容-->
<LinearLayout
android:id="@+id/tab03"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text11"
android:textSize="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text12"
android:textSize="20dp"/>
</LinearLayout>
</FrameLayout>
</TabWidget>
</LinearLayout>
</TabHost>
實(shí)現(xiàn):
public class MainActivity extends TabActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_main,
tabHost.getTabContentView(), true);
AddTab("tab1", R.drawable.tab_info);
AddTab("tab2", R.drawable.tab_info);
AddTab("tab3", R.drawable.tab_info);
//
//標(biāo)簽切換事件處理,setOnTabChangedListener
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
@Override
// tabId是newTabSpec參數(shù)設(shè)置的tab頁(yè)名,并不是layout里面的標(biāo)識(shí)符id
public void onTabChanged(String tabId) {
if (tabId.equals("tab1")) { //第一個(gè)標(biāo)簽
Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁(yè)一", Toast.LENGTH_SHORT).show();
}else if (tabId.equals("tab2")) { //第二個(gè)標(biāo)簽
Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁(yè)二", Toast.LENGTH_SHORT).show();
}else if (tabId.equals("tab3")) { //第三個(gè)標(biāo)簽
Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁(yè)三", Toast.LENGTH_SHORT).show();
}
}
});
}
private void AddTab(String label, int drawableId) {
Intent intent = new Intent(this, TextActivity.class);
TabHost.TabSpec spec = tabHost.newTabSpec(label);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setText(label);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
ps:新建的layout和/drawable里的xml文件在問(wèn)題給過(guò),這里就不反復(fù)給了。
問(wèn)題四:底部導(dǎo)航效果無(wú)法實(shí)現(xiàn)
底部導(dǎo)航的參見(jiàn)方法是把TabWidget放在FrameLayout后面,但是嘖嘖。。。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top">
中間內(nèi)容前面給出 這里省略
</FrameLayout>
</LinearLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
</TabWidget>
</TabHost>
</RelativeLayout>
你會(huì)發(fā)現(xiàn)并沒(méi)有什么 卵用 ?。。I??!,so:
百度了半天找不到問(wèn)題所在,然后。。。修改下MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//原來(lái)
// tabHost = getTabHost();
// LayoutInflater.from(this).inflate(R.layout.activity_main,
// tabHost.getTabContentView(), true);
//修改后
setContentView(R.layout.activity_main);
tabHost = getTabHost();
tabHost.setup(this.getLocalActivityManager());
AddTab("tab1", R.drawable.tab_info);
AddTab("tab2", R.drawable.tab_info);
AddTab("tab3", R.drawable.tab_info);
//標(biāo)簽切換事件處理,setOnTabChangedListener
iniClick();
}
注:此處我已經(jīng)將點(diǎn)擊事件封裝到方法中
最后:全劇終
哦,還沒(méi)有且等我放下最后的圖。。

嘖嘖,搞定
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡
- Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法
- Android組件TabHost實(shí)現(xiàn)頁(yè)面中多個(gè)選項(xiàng)卡切換效果
- android TabHost(選項(xiàng)卡)的使用方法
- android 選項(xiàng)卡(TabHost)如何放置在屏幕的底部
- Android TabLayout(選項(xiàng)卡布局)簡(jiǎn)單用法實(shí)例分析
- Android多個(gè)TAB選項(xiàng)卡切換效果
- Android實(shí)現(xiàn)底部導(dǎo)航欄功能(選項(xiàng)卡)
- Android仿微信底部實(shí)現(xiàn)Tab選項(xiàng)卡切換效果
- android選項(xiàng)卡TabHost功能用法詳解
相關(guān)文章
Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果
這篇文章主要介紹了Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果,實(shí)現(xiàn)思路是先分別實(shí)現(xiàn)閃屏、注冊(cè)界面、登錄界面的活動(dòng),再用Intent將相關(guān)的活動(dòng)連接起來(lái),實(shí)現(xiàn)不同活動(dòng)之間的跳轉(zhuǎn),對(duì)android 實(shí)現(xiàn)閃屏和界面切換感興趣的朋友一起看看吧2016-11-11
Android編程實(shí)現(xiàn)下載圖片及在手機(jī)中展示的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)下載圖片及在手機(jī)中展示的方法,涉及Android針對(duì)圖形文件的遠(yuǎn)程下載及遍歷顯示相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android自定義布局實(shí)現(xiàn)仿qq側(cè)滑部分代碼
這篇文章主要為大家詳細(xì)介紹了自定義布局實(shí)現(xiàn)仿qq側(cè)滑Android部分代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android判斷touch事件點(diǎn)是否在view范圍內(nèi)的方法
這篇文章主要介紹了Android判斷touch事件點(diǎn)是否在view范圍內(nèi)的方法,涉及Android事件響應(yīng)與view屬性操作的相關(guān)技巧,需要的朋友可以參考下2016-03-03
Android 去掉自定義dialog的白色邊框的簡(jiǎn)單方法
這篇文章介紹了Android 去掉自定義dialog的白色邊框,有需要的朋友可以參考一下2013-09-09
Android AsyncTask的缺陷和問(wèn)題總結(jié)
這篇文章主要介紹了Android AsyncTask的缺陷和問(wèn)題總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03

