android TabHost选项卡示例

1. 继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布局文件中定义即可。

tabactivity.xml:



    
    
        
        
        
    
    
    
        
        
        
    
    
     
        
        
        
    



2. 不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的ID必须是@android:id/tabs,FrameLayout的ID必须是@android:id/tabcontent,TabHost的ID可自定义。

tabxml.xml:



    
    
    
        
        
            
            
            
            
                
                
                
                
            
        
    



MyTabActivity.java 文件:

package com.example.baseexample;

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

public class MyTabActivity extends TabActivity {
	private TabHost myTabHost ;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		myTabHost = this.getTabHost();
		LayoutInflater.from(this).inflate(R.layout.tabactivity, myTabHost.getTabContentView(),true);
		myTabHost.addTab(myTabHost.newTabSpec("选项卡1").setIndicator("选项卡1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.firstTab));
		myTabHost.addTab(myTabHost.newTabSpec("选项卡2").setIndicator("选项卡2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.secondTab));
		myTabHost.addTab(myTabHost.newTabSpec("选项卡3").setIndicator("选项卡3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.thirdTab));
	}
}

TabXmlActivity.java文件:
 
  
package com.example.baseexample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;

public class TabXmlActivity extends Activity {
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabxml);
		TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
		tabHost.setup();
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view1));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view2));
		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view3));
	}
}


 
  

你可能感兴趣的:(android)