Activity:
package com.tabtest;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.util.Log;import android.widget.TabHost;import android.widget.TabWidget;import android.widget.TextView;public class TabTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TabHost tabHost = (TabHost) findViewById(R.id.tabhost); tabHost.setup(); tabHost.addTab(tabHost .newTabSpec("tab1") .setIndicator("tab1", getResources().getDrawable(R.drawable.icon)) .setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2", null) .setContent(R.id.view2)); tabHost.addTab(tabHost .newTabSpec("tab3") .setIndicator("tab3", getResources().getDrawable(R.drawable.icon)) .setContent(R.id.view3)); /*设置TabWidget*/ TabWidget tabWidget = tabHost.getTabWidget(); for (int i = 0; i < tabWidget.getChildCount(); i++) { Log.d("log",String.valueOf(i)); tabWidget.setBackgroundColor(Color.LTGRAY); tabWidget.getChildAt(i).getLayoutParams().height = 30;// 设置tab的高度 TextView tv = (TextView) tabWidget.getChildAt(i).findViewById( android.R.id.title); tv.setTextColor(Color.rgb(255, 0, 0));// 设置tab内字体的颜色 } }}
XML文件: