Android中TabActivity用法大全

对于android中的页面,我们如何实现多标签,向浏览器里的多标签一样节省空间,减少开发量。大概 有三种方法可实现这种效果---使用Gellary,使用bar,使用TabActivity。下面主要介绍一下TabActivity的使用方法,可能 有不到位的地方,还望大家多多指教:

1: 我们首先建立一个用于显示页面的布局文件:main.xml如下

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"   
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent"     
    android:layout_height="fill_parent" > 
<LinearLayout     
    android:orientation="vertical" 
    android:gravity="bottom" 
    android:layout_width="fill_parent"     
    android:layout_height="fill_parent" >
 
<FrameLayout     
    android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"
    > 
      
    <RelativeLayout 
    android:id="@+id/view1" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
        <TextView    
            android:id="@+id/text" 
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"   
            android:text="Hello to Johnny.Griffin!" 
            android:layout_centerInParent="true" 
            android:textStyle="bold|italic" /> 
        <ImageView    
            android:layout_width="fill_parent"   
            android:layout_height="fill_parent"   
            android:src="http://blog.163.com/dmg_123456/blog/@drawable/icon" 
            android:layout_toLeftOf="@id/text" /> 
    </RelativeLayout> 
      
    <TextView 
        android:id="@+id/view2" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text="创新源于模仿!" /> 
          
    <TextView 
        android:id="@+id/view3" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:text="欢迎进入 droid 世界!" /> 
          
   
</FrameLayout> 
      <TabWidget     
    android:id="@android:id/tabs"   
    android:layout_width="fill_parent"     
    android:layout_height="60dip" />  
</LinearLayout>     
</TabHost>  
  ----注意:上面的TabWidget的id属性设置为tabs,而且FrameLayout的属性id设置为tabcontent。这事系统默认的。不用深究。

2:写主Activity,继承自TabActivity

package com.jftt.tab;

import com.jftt.common.CONST;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.Toast;

public class TabPActivity extends TabActivity {
 public static final String Tab1 = "Tab1";  
 public static final String Tab2 = "Tab2";  
 public static final String Tab3 = "Tab3";  
 public static final String Tab4 = "Tab4";  
 public static final String Tab5 = "Tab5";  
    public static final String TAG="TabActivity";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);

 TabHost tHost= this.getTabHost();

 tHost.addTab(tHost.newTabSpec(Tab1).setIndicator("Tab 1", getResources().getDrawable(R.drawable.ccleaner)).setContent(R.id.view1));
       tHost.addTab(tHost.newTabSpec(Tab2).setIndicator("Tab 2", getResources().getDrawable(R.drawable.cad)).setContent(R.id.view2));
               

      tHost.addTab(tHost.newTabSpec(Tab3).setIndicator("Tab 3",
      getResources().getDrawable(R.drawable.cmd)).setContent(R.id.view3));
  tHost.addTab(tHost.newTabSpec(Tab4).setIndicator("Tab 4").setContent(new Intent(this,SecondActivity.class)));
        tHost.setOnTabChangedListener(new TabHost.OnTabChangeListener (){   
            @Override  
            public void onTabChanged(String tabId) {   
              // Toast.makeText(getApplicationContext(), "The "+tabId+" is selected!", Toast.LENGTH_SHORT).show();  
             }   
                
        }); 
    }
}

扫描二维码关注公众号,回复: 9645563 查看本文章

我们只需使用简单的  TabHost 的方法就可以实现加载不同页面到不同的tab里面的效果。    不见效果不足以看到希望  来看看效果图吧


 
 
 
这就是我们实现的效果了。
 
大家可以看到我们的页面主要是在TabHost 的setContent()方法中设置的。那么如果我们所有的页面都写到了一个main布局文件中是不是很繁琐了,很好,能发现这个问题说明我们有一定的探索精神了,继续讨论。
 
   如何实现不同的tab的页面位于不同的布局文件中的效果呢。
 
  看看setContent()方法(android中的源码):
 

 /**
         * Specify the id of the view that should be used as the content
         * of the tab.
         */
        public TabSpec setContent(int viewId) {
            mContentStrategy = new ViewIdContentStrategy(viewId);
            return this;
        }

        /**
         * Specify a {@link android.widget.TabHost.TabContentFactory} to use to
         * create the content of the tab.
         */
        public TabSpec setContent(TabContentFactory contentFactory) {
            mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);
            return this;
        }

        /**
         * Specify an intent to use to launch an activity as the tab content.
         */
        public TabSpec setContent(Intent intent) {
            mContentStrategy = new IntentContentStrategy(mTag, intent);
            return this;
        }

很显然,setContent()进行了三次重写。我们使用的是第一种,这种虽然简单,但是技术含量低。不适合扩展。如果是使用intent的 话(第三种方法)相信大家都会写了。不过这种方法虽然使得我们可以把不同的tab内容分开来写了,但是我们岂不是要写很多activity了。呵呵,能发 现这个问题,说明我们具有更加可贵的开发钻研精神。好吧,我们看看第二个方法的使用吧。我们不必把tab的各个页面写进一个布局文件了。具体如下:
   1:首先我们写一个实现接口TabContentFactory,并重写它的createTabContent()方法:
   
 
    

package com.jftt.tab;

import com.jftt.common.CONST;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;

public class CustomLayout implements TabContentFactory {
  private  Activity activity;  
     private  LayoutInflater inflaterHelper;  
     private  LinearLayout layout;  
 
   
           
   
           
          public CustomLayout (Activity a) {  
             this.activity = a;  
               
             inflaterHelper = a.getLayoutInflater();  
         }  
           
         /** {@inheritDoc} *///tag 标记各个标签  
         public View createTabContent(String tag) {  
                 return addCustomView(tag);  
         }  
           
         public View addCustomView(String id){  
               
             layout = new LinearLayout(activity);  
             layout.setOrientation(LinearLayout.VERTICAL);  
               
               
             if(id.equals(CONST.TAB1)){  
                 ImageView iv = new ImageView(activity);  
                 iv.setImageResource(R.drawable.cad);  
                 layout.addView(iv,  
                         new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));  
             }  
             else if(id.equals(CONST.TAB2)){  
                 EditText edit = new EditText(activity);  
                 layout.addView(edit,  
                         new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));  
                   
                 Button btn = new Button(activity);  
                 btn.setText("OK");  
                 btn.setWidth(100);  
                 layout.addView(btn,  
                         new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));  
                   
                 RadioGroup rGroup = new RadioGroup(activity);  
                 rGroup.setOrientation(LinearLayout.HORIZONTAL);  
                 RadioButton radio1 = new RadioButton(activity);  
                 radio1.setText("Radio A");  
                 rGroup.addView(radio1);  
                 RadioButton radio2 = new RadioButton(activity);  
                 radio2.setText("Radio B");  
                 rGroup.addView(radio2);  
                   
                 layout.addView(rGroup,  
                         new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));  
             }  
             else if(id.equals(CONST.TAB3)){  
                   
                 LinearLayout.LayoutParams param3 =  
                     new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);  
                   
                 layout.addView(inflaterHelper.inflate(R.layout.page_tab, null),param3);  
             }  
             else if(id.equals(CONST.TAB4)){  
                 TextView tv = new TextView(activity);  
                 tv.setText("HelloTags!");  
                 tv.setGravity(Gravity.CENTER);  
                 layout.addView(tv);  
             }  
  
             return layout;  
           

  
 }

}

可以看到,我们需要传递一个activity或者是一个Context引用,这样我们就可以将返回的页面加载到这个Context或者 Activity中去。看到了这句 话:layout.addView(inflaterHelper.inflate(R.layout.page_tab, null),param3);   大家因该明白,我们的不同的布局文件怎么加载到一个activity中的吧。呵呵,其实很简单哦。

然后我们在主的activity中这样做:

package com.jftt.tab;

import com.jftt.common.CONST;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.Toast;

public class TabPActivity extends TabActivity {
 public static final String Tab1 = "Tab1";  
 public static final String Tab2 = "Tab2";  
 public static final String Tab3 = "Tab3";  
 public static final String Tab4 = "Tab4";  
 public static final String Tab5 = "Tab5";  
    public static final String TAG="TabActivity";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);

 TabHost tHost= this.getTabHost();

 (R.id.view2));
        CustomLayout ct = new CustomLayout(this);  
        tHost.addTab(tHost.newTabSpec(CONST.TAB1).setIndicator(Tab1).setContent(ct)); 
        tHost.addTab(tHost.newTabSpec(CONST.TAB2).setIndicator(Tab2).setContent(ct)); 
        tHost.addTab(tHost.newTabSpec(CONST.TAB3).setIndicator("Tab3").setContent(ct)); 
        tHost.addTab(tHost.newTabSpec(CONST.TAB4).setIndicator("Tab4").setContent(ct)); 
       

 
    }
}

看官不要惊讶,主的activity如此简单,就是这么简单,让我们看了一目了然。CONST是我们的一个静态类,其中定义了很多静态常量。用于在程序之间互相访问(这是一个很好的编程习惯,建议采纳)。

效果不必掩饰,实践出真知。试试看吧。如果有更好的实现方法,希望大家与我留言。。。每一年,每一天,我们都在进步

原文

发布了4 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/studycwq/article/details/6655240