Android_UI_实现TabHost的两种方法

来源:互联网 发布:人脸识别算法 编辑:程序博客网 时间:2024/05/08 11:39

实现TabHost有两种方式

一、

1.不继承TabActivity

2.在布局文件中定义TabHost


1.图文解意:在布局文件tabhostdemo1.xml里怎么定义tabhost,大家看下面的图片(部分代码)即可,文章最后都会贴出完整代码



【注意】这里需要强调的是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是   @android:id/tabcontent。因为在android的api里这两个id都是定死的,必须用它定义好的,否则报错哦。


2.代码展示

1)TabHostdemo1Activity.java:

[java] view plaincopy
  1. public class Tabhostdemo1Activity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.tabhostdemo1);  
  7.           
  8.         //获取tabhost  
  9.         TabHost tabHost = (TabHost) this.findViewById(R.id.tabhost);       
  10.         tabHost.setup();//实例化了tabWidget和tabContent  
  11.           
  12.         //第一个tab  
  13.         TabSpec tabSpec01 = tabHost.newTabSpec("tab1"); //选项卡其实就是一个tabspec,获取一个新的TabHost.TabSpec,并关联到当前tabhost         
  14.         tabSpec01.setIndicator("first tab"this.getResources().getDrawable(R.drawable.ic_launcher));//往选项卡里添加东西  
  15.         tabSpec01.setContent(R.id.tab1_txt);      
  16.           
  17.         //第二个tab  
  18.         TabSpec tabSpec02 = tabHost.newTabSpec("tab2");        
  19.         tabSpec02.setIndicator("second tab"this.getResources().getDrawable(R.drawable.ic_launcher));         
  20.         tabSpec02.setContent(R.id.tab2_txt);   
  21.           
  22.         //第三个tab  
  23.         TabSpec tabSpec03 = tabHost.newTabSpec("tab3");        
  24.         tabSpec03.setIndicator("third tab"this.getResources().getDrawable(R.drawable.ic_launcher));         
  25.         tabSpec03.setContent(R.id.tab3_txt);  
  26.           
  27.         tabHost.addTab(tabSpec01);  
  28.         tabHost.addTab(tabSpec02);   
  29.         tabHost.addTab(tabSpec03);         
  30.     }  
  31. <span style="font-size:12px;">}  
  32. </span>  

步骤说明:

1>这个例子中我们从布局文件中获取到自定义的tabhost ,即TabHost   tabHost  =   (TabHost) this.findViewById(R.id.tabHost); 使用setup()初始化,整个类继承的是Activity。

    如果使用系统默认的tabhost,即(TabHost) this.findViewById(android.R.id.tabhost),红色部分就是系统自带的样式,直接用getTabhost()初始化,整个类继承TabActivity。

    使用tabHost.setup();===>因为tabWidget和tabContent是在setUp()方法里初始化的,如果没有这句会报空指针异常。


2>TabSpec:理解tabspec,它就相当于一个tab选项卡,我们要给选项卡设置标签、添加图片和文字就用setIndicator(...)和setContent(...)。需要几个选项卡就创建几个tabspec。而tabhost就是一个盛装选项卡的容器,所以选项卡设置好后要把他们一 一添加到容器内,即tabHost.addTab(tabspec);

    TabSpec  tabSpec =tabHost.newTabSpec("tab");   TabSpec的构造函数是私有的,那TabHost必定要提供一个方法来创建TabSpec对象,这个方法就是newTabSpec(String tag)。

   ( "A tab(选项卡) has a tab indicatorcontent, and a tag  that is used  to keep track of it",TabHost.TabSpec就是管理这三个东西的——设置选项卡的标签和内容)


3>tabspec.setIndicator(...)——设置tab(选项卡)的标签label它有三种形式如下,我们的例子用的是第三种

        <1>setIndicator(CharSequence label)----指定一个label作为tab的指示器-----eg:setIndicator("first tab");这个first tab就是显示在选项卡上面的文字

        <2>setIndicatior(View view)----指定一个view作为tab的指示器

        <3>setIndicator(CharSequence label,Drawable icon)----指定一个label和icon作为tab的指示器-----eg:setIndicatior("tab",this.getResource().getDrawable(R.drawable.×××));


4>tabspec.setContent(...)——设置tab的内容content,它有三种形式,我们的例子用的是第一种

        <1>setContent(int viewId)----是一个view 的id,这个view是用来创建tab内容的------eg:setContent(R.id.tab01_text);

        <2>setContent(Intent intent)----指定一个intent,用来启动一个activity,来创建tab的content。想了解具体用法,请滚动到最后 (\0_</)!!!

        <3>setContent(TabHost.TabContentFactory contentFactory)-----指定一个contentfactory来创建tab内容


5>最后不要忘了把你所创建的选项卡tab添加到tabhost容器里==>tabHost.addTab(tabspec01);


2)tabhostdemo1.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TabHost   
  8.         android:id="@+id/tabhost"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent">  
  11.           
  12.         <LinearLayout   
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="fill_parent"  
  15.             android:orientation="vertical">  
  16.           
  17.         <TabWidget   
  18.             android:id="@android:id/tabs"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"/>  
  21.            
  22.         <FrameLayout   
  23.             android:id="@android:id/tabcontent"  
  24.             android:layout_width="fill_parent"  
  25.             android:layout_height="fill_parent"  
  26.             android:background="#505088">  
  27.                     
  28.             <TextView   
  29.                 android:id="@+id/tab1_txt"  
  30.                 android:layout_width="fill_parent"  
  31.                 android:layout_height="fill_parent"  
  32.                 android:text="the content of tab1"  
  33.                 />  
  34.               
  35.             <TextView   
  36.                 android:id="@+id/tab2_txt"  
  37.                 android:layout_width="fill_parent"  
  38.                 android:layout_height="fill_parent"  
  39.                 android:text="the content of tab2"/>  
  40.               
  41.             <TextView   
  42.                 android:id="@+id/tab3_txt"  
  43.                 android:layout_width="fill_parent"  
  44.                 android:layout_height="fill_parent"  
  45.                 android:text="the content of tab3"/>  
  46.         </FrameLayout>  
  47.           
  48.         </LinearLayout>  
  49.           
  50.     </TabHost>  
  51.   
  52. </LinearLayout>  

二、

1.继承TabActivity

2.用getTabhost()方法获取TabHost

3.各tab内容在布局文件中定义


1.代码展示

1)TabHostdemo2Activity.java:

[java] view plaincopy
  1. public class TabHostdemo2Activity extends TabActivity{  
  2.       
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.           
  6.         super.onCreate(savedInstanceState);  
  7.         //我們繼承TabActivity,tabActivity裏面已經有一個TabHost對象,我們就直接通過getTabHost()獲取  
  8.         TabHost tabHost  = this.getTabHost();  
  9.         //把自己的佈局文件添加到tabhost裏面,tabhost相當於一個viewroot  
  10.         LayoutInflater.from(this).inflate(R.layout.tabhostdemo2, tabHost.getTabContentView(), true);  
  11.   
  12.         TabSpec tabSpec;  
  13.         //第一个tab  
  14.         tabSpec = tabHost.newTabSpec("tab1").setIndicator("first tab"this.getResources().getDrawable(R.drawable.ic_launcher))  
  15.                 .setContent(R.id.tab1_txt);      
  16.         tabHost.addTab(tabSpec);  
  17.           
  18.         //第二个tab  
  19.         tabSpec = tabHost.newTabSpec("tab2").setIndicator("second tab"this.getResources().getDrawable(R.drawable.ic_launcher))  
  20.                 .setContent(R.id.tab2_txt);   
  21.         tabHost.addTab(tabSpec);  
  22.           
  23.         //第三个tab  
  24.         tabSpec = tabHost.newTabSpec("tab3").setIndicator("third tab"this.getResources().getDrawable(R.drawable.ic_launcher))  
  25.                 .setContent(R.id.tab3_txt);  
  26.         tabHost.addTab(tabSpec);  
  27.   
  28.         //設置第一次打開時默認顯示的tab,參數與tabHost.newTabSpec("tab1")的參數相同  
  29.         tabHost.setCurrentTabByTag("tab2");  
  30.         //設置第一次打開時默認顯示的tab,參數是其添加到標籤中的順序,tab的位置使從0開始的。  
  31.         //tabHost.setCurrentTab(1);        
  32.     }  
  33. }  
步骤说明:

1>这种方法和第一种方法的区别只有两个地方 :

    <1>用getTabhost()方法获取TabHost因为我们继承了TabActivity,TabActivity里面已经有一个TabHost对象,我们直接通过getTabHost()方法获取。

    <2>代码中我们不是用:setContentView(R.layout.tabhostdemo2);而是下面这句

           LayoutInflater.from(this).inflate(R.layout.tabhostdemo2, tabHost.getTabContentView(), true);

           inflate方法原型:public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)

           第一个参数是布局文件,第二个参数是根路径,第三个参数是是否添加到根路径,他们的关系是,如果attachToRoot为true那么我就把parser(这个内部是要解析的,我们不用考   虑)布局文件添加到根路径root下。getContentView()返回的是一个tabContent,类型为FrameLayout,所以我们是把布局文件添加到FrameLayout这个根目录下。


2>代码格式稍微变换了一下,但和第一种方法没差,这个例子我们只需要创建一个tabspec,但是创建完就要将其添加到tabHost中。


3>最后两句是设置第一次打开时默认显示哪一个选项卡tab,有两种方法,一个是根据参数相同,参数与tabHost.newTabSpec("tab1")中参数相同,一个是根据添加到TabHost容器里的顺序,注意的是,添加的顺序是从0开始的。


2)tabhostdemo2.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.   
  7.     <LinearLayout   
  8.         android:id="@+id/ly1"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent">  
  11.           
  12.         <TextView   
  13.             android:id="@+id/tab1_txt"  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="fill_parent"  
  16.             android:text="the content of tab1_txt"/>  
  17.     </LinearLayout>  
  18.    
  19.     <LinearLayout  
  20.         android:id="@+id/ly2"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="fill_parent">  
  23.           
  24.         <TextView   
  25.             android:id="@+id/tab2_txt"  
  26.             android:layout_width="fill_parent"  
  27.             android:layout_height="fill_parent"  
  28.             android:text="the content of tab2_txt"/>  
  29.     </LinearLayout>  
  30.       
  31.     <LinearLayout  
  32.         android:id="@+id/ly3"  
  33.         android:layout_width="fill_parent"  
  34.         android:layout_height="fill_parent">  
  35.           
  36.         <TextView   
  37.             android:id="@+id/tab3_txt"  
  38.             android:layout_width="fill_parent"  
  39.             android:layout_height="fill_parent"  
  40.             android:text="the content of tab03_txt"/>  
  41.     </LinearLayout>  
  42.   
  43. </FrameLayout>  

注意根元素是FrameLayout

====================================================================================================================================

【Extera:setContent(Intent intent)怎么用---简单示例】

 ----指定一个intent,用来启动一个activity,来创建tab的content

直接贴代码了:

TabLayout02Activity.java

[java] view plaincopy
  1. public class TabLayout02Activity extends TabActivity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.           
  8.         TabHost tabHost = getTabHost();//得到一个盛装tab的容器  
  9.           
  10.         Intent intent;  
  11.         intent = new Intent().setClass(this, Tab01Activity.class);  
  12.           
  13.         tabHost.addTab(tabHost.newTabSpec("tab").setIndicator("first tab", getResources().getDrawable(R.drawable.icon)).setContent(intent));  
  14.          
  15.         intent = new Intent().setClass(this, Tab02Activity.class);  
  16.         tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("second tab", getResources().getDrawable(R.drawable.icon)).setContent(intent));  
  17.           
  18.         intent = new Intent().setClass(this, Tab03Activity.class);  
  19.         tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("third tab", getResources().getDrawable(R.drawable.icon)).setContent(intent));  
  20.           
  21.     }  
  22. }  

提前建好三个Activity分别是Tab01Activity,Tab02Activity,Tab03Activity。




再来看Tab01Activity的代码:

Tab01Activity.java

[java] view plaincopy
  1. public class Tab01Activity extends Activity{  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         // TODO Auto-generated method stub  
  5.         super.onCreate(savedInstanceState);  
  6.         TextView textview = new TextView(this);//创建一个新的文本组件  
  7.         textview.setText("tag1");  
  8.         setContentView(textview);  
  9.     }  
  10. }  

0 0
原创粉丝点击