package com.example.tabhost;
import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
public class MainActivity extends TabActivity{
private TabHost mytabhost;
int [] tabs=new int[]{R.id.tab1,R.id.tab2,R.id.tab3};
TabWidget tw=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mytabhost=super.getTabHost();
tw=mytabhost.getTabWidget();
//设置整个标签栏内文字的方式
tw.setHorizontalGravity(Gravity.LEFT);
tw.setVerticalGravity(Gravity.BOTTOM);
//设置整个标签栏的高呀
tw.getLayoutParams().height=35;
//设置整个标签栏的背景
tw.setBackgroundColor(Color.BLUE);
LayoutInflater.from(this).inflate(R.layout.activity_main, this.getTabHost().getTabContentView(), true);
for (int i = 0; i < tabs.length; i++) {
//获得 内容
TabSpec tabspec=mytabhost.newTabSpec(i+"");
tabspec.setContent(tabs[i]);
tabspec.setIndicator("标签"+i);//也可以设置成view
mytabhost.addTab(tabspec);
}
//为每个标签设置背景色
//获得tw.getChildAt(i)应在mytabhost.addTab(tabspec);之后否则会报找不到异常
for (int i = 0; i < tw.getChildCount(); i++) {
tw.getChildAt(i).setBackgroundColor(Color.RED);
//获得标签对应的textview
TextView tv=(TextView) tw.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.YELLOW);
}
}
}
配置文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<include layout="@layout/tab1"/>
<include layout="@layout/tab2"/>
<include layout="@layout/tab3"/>
</RelativeLayout>
tab1.xml tab2 tab3类似
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:id="@+id/tab1"
android:orientation="vertical"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1"
/>
</LinearLayout>