Android 中的TabHost控件的使用

介绍 

有时,我们想在一个window中显示多个视图,这时就需要用到Tab容器。在Android里它叫TabHost。

使用TabHost有两种方式:

  1. 在相同的activity中使用TabHost导航多个视图
  2. 使用TabHost导航多个Activity(通过intents)
Tab应用的结构
TabHost的Activity的结构如下:


1 layout 文件 main.xml 代码:
<?xml version="1.0" encoding="utf-8"?> 
      
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:id="@android:id/tabhost" >  
    <TabWidget  
	    android:layout_width="fill_parent"  
	    android:layout_height="wrap_content"  
	    android:id="@android:id/tabs"  />  
    <FrameLayout  
	     android:layout_width="fill_parent"  
	    android:layout_height="fill_parent"  
	    android:id="@android:id/tabcontent"  >  
	    <LinearLayout  
		    android:layout_width="fill_parent"  
		    android:layout_height="wrap_content"  
		    android:id="@+id/tab1"  
		    android:orientation="vertical"  
		    android:paddingTop="60px"   >  

		    <TextView
		        android:id="@+id/txt1"
		        android:layout_width="fill_parent"
		        android:layout_height="wrap_content"
		        android:layout_marginTop="10dp"
		        android:text="This is tab1" />
 
        </LinearLayout>  
	       
	    <LinearLayout
	        android:id="@+id/tab2"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:orientation="vertical"
	        android:paddingTop="60px" >

	        <TextView
	            android:id="@+id/txt2"
	            android:layout_width="fill_parent"
	            android:layout_height="wrap_content"
	            android:layout_marginTop="10dp"
	            android:text="This is tab 2" />
  
        </LinearLayout>  
	       
	    <LinearLayout
	        android:id="@+id/tab3"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:orientation="vertical"
	        android:paddingTop="60px" >

			<TextView
			    android:id="@+id/txt3"
			    android:layout_width="fill_parent"
			    android:layout_height="wrap_content"
			    android:layout_marginTop="10dp"
			    android:text="This is tab 3" />
	     
	     </LinearLayout>  
    </FrameLayout>  
      
</TabHost>  

2Activity代码

import android.app.TabActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
 
public class MainActivity1 extends TabActivity 
{

	@Override
    public void onCreate(Bundle savedInstanceState) {  
	    super.onCreate(savedInstanceState);  
	    setContentView(R.layout.main);  
	    TabHost tabHost=(TabHost)findViewById(android.R.id.tabhost);  
	    tabHost.setup();  	// 如果使用TabAcitivity 作为你的Activity 的基类,
	    						// 你不用调用Tabost.Setup方法
	      
	    TabSpec spec1=tabHost.newTabSpec("Tab 1");  
	    spec1.setContent(R.id.tab1);  
	    spec1.setIndicator("Tab 1");  
	
	      
	    TabSpec spec2=tabHost.newTabSpec("Tab 2");  
	    spec2.setIndicator("Tab 2"); 
	    spec2.setContent(R.id.tab2);
 
	    TabSpec spec3=tabHost.newTabSpec("Tab 3");  
	    spec3.setIndicator("Tab 3");  
	    spec3.setContent(R.id.tab3); 
	    
	    tabHost.addTab(spec1);  
	    tabHost.addTab(spec2);  
	    tabHost.addTab(spec3);  
     }  
}

效果:



  1. 这里通过TabSpecs类创建Tab
  2. 使用setIndicator方法设置tab的文字
  3. 使用setContent设置tab的内容
  4. 如果你使用TabActivity作为你的Activity的基类,你不用调用TabHost.Setup()方法。
同时还可以指定indicator为一个view:
</pre><pre name="code" class="html">TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
TextView txt=new TextView(this);
txt.setText("Tab 1");
txt.setBackgroundColor(Color.RED);
spec1.setIndicator(txt);

设置tab的内容
上面的例子展示了使用tab显示不同的layout资源。如果我们需要通过tab导航到不同的Activity,该怎么办?
这种情况,我们需要有一个activity作为应用的根activity。这个Activity包含TabHost,通过intents导航不同的activity。
注意:根Activity必须继承TabActivity。代码如下:
Layout:

<?xml version="1.0" encoding="utf-8"?>
    <TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabhost"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <TabWidget
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabs"
    />
     <FrameLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabcontent"
     >
     </FrameLayout>
    </TabHost>

Activity:

public class TabDemo extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost=getTabHost();
        // no need to call TabHost.Setup()        
        
        //First Tab
        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.sun));
        Intent in1=new Intent(this, Act1.class);
        spec1.setContent(in1);
        
        TabSpec spec2=tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Tab 2",getResources().getDrawable(R.drawable.chart));
        Intent in2=new Intent(this,Act2.class);
        spec2.setContent(in2);

        tabHost.addTab(spec2);
        tabHost.addTab(spec3);
    }
}

以上的会发生运行错误,可能是在我写的时候,android版本更新了。所以旧代码运行起来有问题。有知道原因的朋友可以留言。


在运行时添加Tab
在运行时我们可以通过调用TabSepc.setContent(TabContentFactory)方法添加Tab。

import android.app.TabActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
 
public class MainTabActivity extends TabActivity 
{

	@Override
    public void onCreate(Bundle savedInstanceState) {  
	    super.onCreate(savedInstanceState);  
	    setContentView(R.layout.main);  
	    TabHost tabHost=(TabHost)findViewById(android.R.id.tabhost);  
	    tabHost.setup();  	// 如果使用TabAcitivity 作为你的Activity 的基类,
	    						// 你不用调用Tabost.Setup方法
	      
//	    TabSpec spec1=tabHost.newTabSpec("Tab 1");  
//	    spec1.setContent(R.id.tab1);  
//	    spec1.setIndicator("Tab 1");  
//	    
	    TabSpec spec1=tabHost.newTabSpec("Tab 1");
	    spec1.setContent(R.id.tab1);
	    TextView txt=new TextView(this);
	    txt.setText("Tab 1 in a View L");
	    txt.setBackgroundColor(Color.RED);
	    spec1.setIndicator(txt);
	      
	    TabSpec spec2=tabHost.newTabSpec("Tab 2");  
	    spec2.setIndicator("Tab 2"); 
	    // method 1 , setContent(int) 
	    spec2.setContent(R.id.tab2);
	    
	    // method 2 , setContent(Intent)

	    // method 3 , setContent(TabHost.TabContentFactory)
//	    spec2.setContent(new TabHost.TabContentFactory() {
//	    	public View createTabContent(String tag) {  
//	    	   return (new AnalogClock(MainTabActivity.this));  
//	    	}  
//	     });
	    
	    TabSpec spec3=tabHost.newTabSpec("Tab 3");  
	    spec3.setIndicator("Tab 3");  
	    spec3.setContent(R.id.tab3); 
	    
	    tabHost.addTab(spec1);  
	    tabHost.addTab(spec2);  
	    tabHost.addTab(spec3);  
     }  
}


运行效果





原地址:http://blog.csdn.net/xinem/article/details/7083523
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值