[转载]Android的tabhost取消默认的白底色 - - ITeye技术网站

[转载]tabhost取消默认的白底色 – – ITeye技术网站.

http://www.cnblogs.com/zhaolong_chn/archive/2011/12/18/2291872.html
http://www.cnblogs.com/wader2011/archive/2011/10/13/2209668.html
http://www.cnblogs.com/zhukoo/archive/2011/09/30/2196878.html

package com.hg.beyond.demo.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

import com.hg.beyond.demo.R;

public class PhoneManagerHomeActivity extends TabActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.phone_manager_home);
		setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		launchTabHost();
	}

	private void launchTabHost() {

		final TabHost tabHost = this.getTabHost();
		
		
		//注册监听器调用方法改变底色
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				updateTabBackground(tabHost);

			}
		});
		View callView = LayoutInflater.from(this).inflate(
				R.layout.phone_manager_tab_item, null);

		View msgView = LayoutInflater.from(this).inflate(
				R.layout.phone_manager_tab_item, null);

		View contactView = LayoutInflater.from(this).inflate(
				R.layout.phone_manager_tab_item, null);

		View favoritView = LayoutInflater.from(this).inflate(
				R.layout.phone_manager_tab_item, null);

		TextView callLable = (TextView) callView
				.findViewById(R.id.tab_item_lable);
		ImageView callImage = (ImageView) callView
				.findViewById(R.id.tab_item_icon);
		callLable.setText(getString(R.string.tab_call_txt));
		callImage.setImageResource(R.drawable.call_tab_item_selector);

		TextView contactLable = (TextView) contactView
				.findViewById(R.id.tab_item_lable);
		ImageView contactImage = (ImageView) contactView
				.findViewById(R.id.tab_item_icon);
		contactLable.setText(getString(R.string.tab_contact_txt));
		contactImage.setImageResource(R.drawable.contact_tab_item_selector);

		TextView msgLable = (TextView) msgView
				.findViewById(R.id.tab_item_lable);
		ImageView msgImage = (ImageView) msgView
				.findViewById(R.id.tab_item_icon);
		msgLable.setText(getString(R.string.tab_msg_txt));
		msgImage.setImageResource(R.drawable.msg_tab_item_selector);

		TextView favoritLable = (TextView) favoritView
				.findViewById(R.id.tab_item_lable);
		ImageView favoritImage = (ImageView) favoritView
				.findViewById(R.id.tab_item_icon);
		favoritLable.setText(getString(R.string.tab_fav_txt));
		favoritImage.setImageResource(R.drawable.fav_tab_item_selector);

		tabHost.addTab(tabHost.newTabSpec("call").setIndicator(callView)
				.setContent(new Intent(this, CallActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("msg").setIndicator(msgView)
				.setContent(new Intent(this, MsgActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("contact").setIndicator(contactView)
				.setContent(new Intent(this, ContactActivity.class)));

		tabHost.addTab(tabHost.newTabSpec("favorit").setIndicator(favoritView)
				.setContent(new Intent(this, FavoritActivity.class)));
		tabHost.setCurrentTab(0);
	}

	/**
	 * 更新Tab标签的底色图
	 * 
	 * @param tabHost
	 */
	private void updateTabBackground(final TabHost tabHost) {
		for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
			View vvv = tabHost.getTabWidget().getChildAt(i);
			if (tabHost.getCurrentTab() == i) {
				
				// 选中后的背景
				vvv.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.cm_tabwidget_select_bg));
			} else {
				// 非选择的背景
			 vvv.setBackgroundDrawable(null);
			}
		}
	}

}
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏