【Android】自定义 Tabhost

效果图:


MainActivity.java

package tianshuai.homePage;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainActivity extends TabActivity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		//取得当前的TabHost
		TabHost tabs = getTabHost();
		
		//载入主布局文件
		LayoutInflater.from(this).inflate(R.layout.main, tabs.getTabContentView(), true);
		
		//给Tab1添加自定义样式
		RelativeLayout tabStyle1 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null);
		TextView text1 = (TextView)tabStyle1.findViewById(R.id.tab_label);
		text1.setText("1");
		
		//给Tab2添加自定义样式
		RelativeLayout tabStyle2 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null);
		TextView text2 = (TextView)tabStyle2.findViewById(R.id.tab_label);
		text2.setText("2");
		
		//创建新Tab, 使用tab1的样式
		TabSpec tab1 = tabs.newTabSpec("tab1");
		tab1.setIndicator(tabStyle1);
		tab1.setContent(R.id.Tab1);
		tabs.addTab(tab1);
		
		//创建新Tab, 使用tab2的样式
		TabSpec tab2 = tabs.newTabSpec("tab2");
		tab2.setIndicator(tabStyle2);
		tab2.setContent(R.id.Tab2);
		tabs.addTab(tab2);
	}
	
	
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"  
    >
   
   <LinearLayout
   	android:id="@+id/Tab1"
    	android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical"
    >
                
   	<TextView 
		android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:text="这是tab1"
    	/>
    </LinearLayout>
    
       <LinearLayout
    	android:id="@+id/Tab2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical"
     >
                
    	<TextView 
    		android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:text="这是tab2"
    	/>
    </LinearLayout>
</FrameLayout>

table_style.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    
    >  
 
    <TextView android:id="@+id/tab_label"  
        android:layout_width="fill_parent"
        android:layout_height="30px"
        android:gravity="center"
        android:textColor="#000000"
        android:textStyle="bold"
        android:background="@drawable/tab"
        /> 
</RelativeLayout>
tab.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android = "http://schemas.android.com/apk/res/android">   
        
         <item   
        	android:state_selected = "true"   
         	android:drawable = "@drawable/red"   
         />
         <item   
         	android:state_selected = "false"   
         	android:drawable = "@drawable/bule"   
         />    
</selector> 




posted @ 2012-03-29 13:56  MXi4oyu  阅读(159)  评论(0编辑  收藏  举报