之前一直认为TabHost不够灵活而使用ActivityGroup。看到了一个利用反射来控制TabHost的例子
先通过
tabHost.newTabSpec(CagConstant.TAB_TAG_CHANGE_INFO);
tabHost.newTabSpec(CagConstant.TAB_TAG_ROUTE_INFO);
tabHost.newTabSpec(CagConstant.TAB_TAG_STATION_INFO);
来获得TabSpec单个按钮的对象,之后setContent(ViewId)来设置选中状态效果。接下来通过反射
tabWidget.getClass().getDeclaredField("mBottomLeftStrip");
tabWidget.getClass().getDeclaredField("mBottomRightStrip");
(2.2之后改为mLeftStrip,mRightStrip)
得到TabSpec左右两侧按钮的Field对象。然后解除访问限制setAccessible(true);设置未被选中时的背景。点击事件按钮背景的变化是通过for循环来判断的,个人感觉还有可以改进的地方。
package com.CustomTabhost;
import java.lang.reflect.Field;
import com.tab.R;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
public class CustomTabhost extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int width = 45;
int height = 30;
final TabHost tabs = getTabHost();
final TabWidget tabWidget = tabs.getTabWidget();
//-----------------------------若是继承自Activity OR ActivityGroup-----------------------
// TabHost tabHost=(TabHost)findViewById(R.id.tabhost);
// tabs.addTab(tabs.newTabSpec("first tab").setIndicator("信息").setContent(new Intent(CustomTabhost.this, Load.class)));
// 注意此处我们用的是setContent(new Intent(CustomTabhost.this, Load.class))
// 那么tabHost.setup(this.getLocalActivityManager());必须继承ActivityGroup
// 若是用的是类似:mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB a").setContent(R.id.LinearLayout01));
// 则应该用tabHost.setup(); 可以继承自Activity OR ActivityGroup
// TabWidget tabWidget = tabs.getTabWidget();//此句话应该在setup方法后面,不然会为null,若是继承自TabActivity则不用考虑因为TabActivity已经帮我们封装实现了
//---------------------------------------------------------------------------------------
Field mBottomLeftStrip;
Field mBottomRightStrip;
// LayoutInflater.from(this).inflate(R. layout.loading,
// tabs.getTabContentView(), true);
tabs.addTab(tabs.newTabSpec("first tab").setIndicator("信息").setContent(
new Intent(CustomTabhost.this, Load.class)));
tabs.addTab(tabs.newTabSpec("second tab").setIndicator("收藏")
.setContent(new Intent(CustomTabhost.this, Load.class)));
tabs.addTab(tabs.newTabSpec("second tab").setIndicator("设置")
.setContent(new Intent(CustomTabhost.this, Load.class)));
tabs.setCurrentTab(1);
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));
/**
* 此方法是为了去掉系统默认的色白的底角
*
* 在 TabWidget中mBottomLeftStrip、mBottomRightStrip
* 都是私有变量,但是我们可以通过反射来获取
*
* 由于还不知道Android 2.2的接口是怎么样的,现在先加个判断好一些
*/
if (Integer.valueOf(Build.VERSION.SDK) <= 7) {
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 {
try {
mBottomLeftStrip = tabWidget.getClass().getDeclaredField(
"mLeftStrip");
mBottomRightStrip = tabWidget.getClass().getDeclaredField(
"mRightStrip");
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) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
View vvv = tabWidget.getChildAt(i);
if (tabs.getCurrentTab() == i) {
vvv.setBackgroundDrawable(getResources().getDrawable(
R.drawable.navbgfocus));
} else {
vvv.setBackgroundDrawable(getResources().getDrawable(
R.drawable.menubg));
}
}
/**
* 当点击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.menufocus));
} else {
vvv.setBackgroundDrawable(getResources().getDrawable(
R.drawable.menubg));
}
}
}
});
}
}