Android TabHost

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文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/hometabs" android:orientation="vertical"
	android:layout_width="fill_parent" android:layout_height="fill_parent">
	<TabHost android:id="@+id/tabhost" android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<LinearLayout android:orientation="vertical"
			android:layout_width="fill_parent" android:layout_height="fill_parent">

			<TabWidget android:id="@android:id/tabs"
				android:orientation="horizontal" android:layout_width="fill_parent"
				android:layout_height="wrap_content">
			</TabWidget>

			<FrameLayout android:id="@android:id/tabcontent"
				android:layout_width="wrap_content" android:layout_height="wrap_content">
				<TextView android:id="@+id/view1" android:layout_width="fill_parent"
					android:layout_height="fill_parent" android:text="Tab1" />
				<TextView android:id="@+id/view2" android:layout_width="fill_parent"
					android:layout_height="fill_parent" android:text="Tab2" />
				<TextView android:id="@+id/view3" android:layout_width="fill_parent"
					android:layout_height="fill_parent" android:text="Tab3" />
			</FrameLayout>

		</LinearLayout>
	</TabHost>
</LinearLayout>

  

posted @ 2012-03-27 20:00  cornellbox  阅读(370)  评论(0编辑  收藏  举报