tabhost 动态改变tab的背景图

[b][color=blue]在我的项目中 有5个tab 需要在选中某个TAB的时候 背景图片更换掉
我的Activity继承了
MyClass extends TabActivity implements TabHost.TabContentFactory
Tab主界面对应的layout文件就不用说了
网上有很多关于tabhost配置文件需要注意的 这里的话 我再啰嗦一下[/color]

[color=red]<FrameLayout android:id="@android:id/tabcontent" 一定要这样设置
<TabWidget android:id="@android:id/tabs"一定要这样设置[/color]

[color=blue]由于系统自带的样式太丑了 我就自定义了一个tab样式
每个TAB样式都有TAB显示文字和背景图片 请下载本文附件参阅tab_spec.xml

现在说到正题了 如果在点击的过程中 选中项背景变色呢
1,首先应该默认给TAB一个背景色
2,在点击过程中 根据TAB发生变化 再重新赋一次背景就OK了
见如下代码[/color]

[color=red]//获取要显示的TAB页图片名称
private int[] drawableArray = Constant.drawableArray ;

//当该TAB选中时,更换背景图片
private int[] drawableSelectedArray = Constant.drawableSelectedArray ;

// 添加一个Tab页
addTab(tabHost, str+num, str, drawableArray[num-1], intent);
//设置初始化加载页面默认被选中项
tabHost.setCurrentTab(0) ;
tabWidget.getChildAt(0).findViewById(R.id.img_ico).setBackgroundResource(drawableSelectedArray[0]) ;


private void addTab(TabHost tabHost, String Tag, String name, int drawableId, Intent intent) {
// 填充
View tabView = LayoutInflater.from(this).inflate(R.layout.tab_spec,
null);
//填充图片(这里是设置默认的背景图片)
ImageView image = (ImageView)tabView.findViewById(R.id.img_ico);
image.setBackgroundResource(drawableId) ;

//填充文本
TextView textView = (TextView) tabView.findViewById(R.id.txt_name);
textView.setText(name);
tabHost.addTab(tabHost.newTabSpec(Tag).setIndicator(tabView)
.setContent(intent));
}

/** 监听切换tab */
private TabHost.OnTabChangeListener tabChange = new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
View view = CrossedWorkMainActivity.this.getTabHost().getCurrentView();

int index = Integer.valueOf(tabId.substring(tabId.length()-1, tabId.length()))-1 ;//获取当前索引
tabIndex = index ;

//获得当前所有的TabWidget
TabWidget widget = CrossedWorkMainActivity.this.getTabHost().getTabWidget() ;
int num = widget.getChildCount() ;
for(int i=0;i<num;i++){
ImageView imageView = (ImageView)widget.getChildAt(i).findViewById(R.id.img_ico) ;
if(tabIndex==i){
//如果某个tab被选中,则更换背景图片
imageView.setBackgroundResource(drawableSelectedArray[i]) ;
}else{
//未选中的,则使用默认背景图
imageView.setBackgroundResource(drawableArray[i]) ;
}
}
}
};[/color][/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值