【Android】通过Java代码替换TabHost中的drawableTop资源

在博客 http://blog.csdn.net/jueblog/article/details/11837445 中的Tab选项卡中,

点击相应的Tab选项,图标没有发生改变。


这些资源图片也没有尽用,若要使点击选项后,图标发生改变,可以在Java代码中实现。

改变drawableTop的Java代码:

tab_icon_weixin.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_weixin_pressed,0, 0);
							

从而,我们可以对Tab代码进行优化:

		main_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int id) {
						if (id == tab_icon_weixin.getId()) {
							tab_icon_weixin.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_weixin_pressed,0, 0);
							tab_icon_address.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_address_normal,0, 0);
							tab_icon_find.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_find_frd_normal,0, 0);
							tab_icon_myself.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_settings_normal,0, 0);
							mTabHost.setCurrentTab(0);
						} else if (id == tab_icon_address.getId()) {
							tab_icon_weixin.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_weixin_normal,0, 0);
							tab_icon_address.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_address_pressed,0, 0);
							tab_icon_find.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_find_frd_normal,0, 0);
							tab_icon_myself.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_settings_normal,0, 0);
							mTabHost.setCurrentTab(1);
						} else if (id == tab_icon_find.getId()) {
							tab_icon_weixin.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_weixin_normal,0, 0);
							tab_icon_address.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_address_normal,0, 0);
							tab_icon_find.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_find_frd_pressed,0, 0);
							tab_icon_myself.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_settings_normal,0, 0);
							mTabHost.setCurrentTab(2);
						} else if (id == tab_icon_myself.getId()) {
							tab_icon_weixin.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_weixin_normal,0, 0);
							tab_icon_address.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_address_normal,0, 0);
							tab_icon_find.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_find_frd_normal,0, 0);
							tab_icon_myself.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_settings_pressed,0, 0);
							mTabHost.setCurrentTab(3);
						}
					}
				});
从而可以得到想要的切换效果。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值