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

    使用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文字及背景(源码分享)

上一篇:iOS 运行时添加属性和方法


下一篇:@+id/和android:id有什么区别?