tabhost

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>



 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值