首页 > 移动平台 > 详细

Android开发之自定义TabHost文字及背景(源代码分享)

时间:2014-05-11 20:02:43      阅读:481      评论:0      收藏:0      [点我收藏+]

    使用TabHost 可以在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该如何进行自定义修改优化呢

MainActivity的源代码

package com.dream.ledong;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

import com.example.client.R;

@SuppressWarnings("deprecation")
public class itemList extends TabActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.itemlist);
		final TabHost tabHost = getTabHost();
		Intent remoteIntent = new Intent(itemList.this, item1.class);
		TabHost.TabSpec remoteTabSpec = tabHost.newTabSpec("remote");
		remoteTabSpec.setIndicator("运动推荐");
		remoteTabSpec.setContent(remoteIntent);
		tabHost.addTab(remoteTabSpec);
		
		Intent localIntent = new Intent(itemList.this, item2.class);
		TabHost.TabSpec localTabSpec = tabHost.newTabSpec("local");
		localTabSpec.setIndicator("球友人气");
		localTabSpec.setContent(localIntent);
		tabHost.addTab(localTabSpec);
		
		Intent localIntent2 = new Intent(itemList.this, item2.class);
		TabHost.TabSpec localTabSpec2 = tabHost.newTabSpec("a");
		localTabSpec2.setIndicator("竞技氛围");
		localTabSpec2.setContent(localIntent2);
		tabHost.addTab(localTabSpec2);
        
		updateTabStyle(tabHost);

		// 当某个Tab被选中时,则更新背景样式
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				updateTabStyle(tabHost);
			}
		});
	}

	private void updateTabStyle(final TabHost mTabHost) {
		TabWidget tabWidget = mTabHost.getTabWidget();
		tabWidget.setRightStripDrawable(R.drawable.list_item_divide_operate);
		tabWidget.setLeftStripDrawable(R.drawable.list_item_divide_operate);
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			RelativeLayout tabView = (RelativeLayout) mTabHost.getTabWidget()
					.getChildAt(i);
			TextView text = (TextView) tabWidget.getChildAt(i).findViewById(
					android.R.id.title);
			text.setTextSize(15);
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text
					.getLayoutParams();
			params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
			params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
			text.setLayoutParams(params); 
			text.setGravity(Gravity.CENTER);
			if (mTabHost.getCurrentTab() == i) {
				// 选中
				tabView.setBackgroundColor(Color.parseColor("#8DB6CD"));
				text.setTextColor(this.getResources().getColorStateList(
						android.R.color.black));
			} else {
				// 未选中
				tabView.setBackgroundColor(Color.parseColor("#ffffff"));
				text.setTextColor(this.getResources().getColorStateList(
						android.R.color.darker_gray));
			}
		}
	}

}


Android开发之自定义TabHost文字及背景(源代码分享),布布扣,bubuko.com

Android开发之自定义TabHost文字及背景(源代码分享)

原文:http://blog.csdn.net/gerogelin/article/details/25531547

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!