之前一直认为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. */@Overridepublic 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 blocke.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() {@Overridepublic void onTabChanged(String tabId) {// TODO Auto-generated method stubfor (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));}}}});}}

更多相关文章

  1. Android动画播放的常用方式
  2. 安卓课程十八 RadioButton单选按钮的使用
  3. Android(安卓)标题栏上加一个返回按钮 超简单的返回
  4. Pair 了解一下
  5. android设置背景
  6. 〖Android〗我的ADT Eclipse定制
  7. [置顶] Android(安卓)Gallery橱窗效果
  8. Android(安卓)webview 常用配置
  9. Android(安卓)databinding RecycleView item 子控件点击事件

随机推荐

  1. Android BootLoader及两种刷机模式fastbo
  2. Android系统启动过程---uboot,kernel,and
  3. Android之LocationManager类的方法详解
  4. Android(安卓)应用开发支持不同的语言国
  5. Android APK应用安装位置选择与对Widget
  6. 一种在android中实现MVP模式的新思路
  7. android中c/c++程序的调试(eclipse)
  8. android开发怎样让悬浮Activity只是隐藏
  9. Native C of Android
  10. Android OpenCV获取相机并拍(Android Stu