Android編程實現(xiàn)自定義Tab選項卡功能示例
本文實例講述了Android編程實現(xiàn)自定義Tab選項卡功能。分享給大家供大家參考,具體如下:
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import android.widget.TabHost.OnTabChangeListener;
import android.os.Build;
import android.view.View;
import java.lang.reflect.Field;
import android.view.LayoutInflater;
public class testTabActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int width =45;
int height =48;
final TabHost tabs = getTabHost();
final TabWidget tabWidget = tabs.getTabWidget();
Field mBottomLeftStrip;
Field mBottomRightStrip;
LayoutInflater.from(this).inflate(R.layout.tab_views, tabs.getTabContentView(), true);
tabs.addTab(tabs.newTabSpec("first tab")
.setIndicator("信息",getResources().getDrawable(R.drawable.m))
.setContent(new Intent(testTabActivity.this,OneActivty.class))
);
tabs.addTab(tabs.newTabSpec("second tab")
.setIndicator("收藏",getResources().getDrawable(R.drawable.n))
.setContent(R.id.content));
tabs.addTab(tabs.newTabSpec("second tab")
.setIndicator("設置",getResources().getDrawable(R.drawable.s))
.setContent(R.id.content));
for (int i =0; i < tabWidget.getChildCount(); i++) {
/**
* 設置高度、寬度,不過寬度由于設置為fill_parent,在此對它沒效果
*/
tabWidget.getChildAt(i).getLayoutParams().height = height;
tabWidget.getChildAt(i).getLayoutParams().width = width;
/**
* 設置tab中標題文字的顏色,不然默認為黑色
*/
final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
/**
* 此方法是為了去掉系統(tǒng)默認的色白的底角
*
* 在 TabWidget中mBottomLeftStrip、mBottomRightStrip
* 都是私有變量,但是我們可以通過反射來獲取
*
* 由于還不知道Android 2.2的接口是怎么樣的,現(xiàn)在先加個判斷好一些
*/
if (Float.valueOf(Build.VERSION.RELEASE) <= 2.1) {
try {
mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip");
mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip");
if(!mBottomLeftStrip.isAccessible()) {
mBottomLeftStrip.setAccessible(true);
}
if(!mBottomRightStrip.isAccessible()){
mBottomRightStrip.setAccessible(true);
}
mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.no));
mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.no));
} catch (Exception e) {
e.printStackTrace();
}
} else {
/**
* 不做任何處理
*/
}
View vvv = tabWidget.getChildAt(i);
if(tabs.getCurrentTab()==i){
vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button));
}
else {
vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar));
}
}
/**
* 當點擊tab選項卡的時候,更改當前的背景
*/
tabs.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i =0; i < tabWidget.getChildCount(); i++) {
View vvv = tabWidget.getChildAt(i);
if(tabs.getCurrentTab()==i){
vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button));
}
else {
vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar));
}
}
}});
}
}
更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android編程之a(chǎn)ctivity操作技巧總結》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結》、《Android操作json格式數(shù)據(jù)技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android視圖View技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
viewPager+fragment刷新緩存fragment的方法
這篇文章主要介紹了viewPager+fragment刷新緩存fragment的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
Android App開發(fā)中HTTP擴展包OkHttp的入門使用指南
OkHttp包為安卓開發(fā)中基于HTTP協(xié)議的網(wǎng)絡編程提供了很大便利,這里我們就來看一下Android App開發(fā)中HTTP擴展包OkHttp的入門使用指南:2016-07-07
使用Android studio創(chuàng)建的AIDL編譯時找不到自定義類的解決辦法
這篇文章主要介紹了使用Android studio創(chuàng)建的AIDL編譯時找不到自定義類的解決辦法的相關資料,需要的朋友可以參考下2016-02-02
Android對EditTex的圖片實現(xiàn)監(jiān)聽
這篇文章主要為大家詳細介紹了Android如何對EditTex的圖片實現(xiàn)監(jiān)聽,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Android SQLite數(shù)據(jù)庫連接實現(xiàn)登錄功能
這篇文章主要為大家詳細介紹了Android SQLite數(shù)據(jù)庫連接實現(xiàn)登錄功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10

