Tabhost的使用-android开发

关键代码分析:

<?xml version="1.0" encoding="utf-8"?>

<!--创建tabhost容器-->
<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" //为容器id,不可缺少,不可更改

>


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="5dp">


    <!--TabWidget存放选项卡容器-->
   <TabWidget
       android:id="@android:id/tabs"  //为选项卡id,不可缺少,不可更改
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>


     <!--FrameLayout存放选项卡对应内容的容器-->
    <FrameLayout
       android:id="@android:id/tabcontent" //为选项卡对应内容的ID,不可缺少,不可更改
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:padding="5dp"
        />
    
</LinearLayout>
</TabHost>

----------------------------------------------------------------------

         //获取资源对象
        Resources res=getResources();
        //获取host对象
        TabHost tabhost=getTabHost();
        //声明可以复用的tabspec
         TabHost.TabSpec spec;
        //声明可以复用的intent
         Intent intent;
                
         intent=new Intent().setClass(this, tab1.class);//创建启动的intent
         spec=tabhost.newTabSpec("tab1");//实例化选项卡tabspec,tab1为选项卡的标记
         spec.setIndicator("Tab1",res.getDrawable(R.drawable.tab11));//设置选项卡的显示名字与图片
         spec.setContent(intent);//绑定选项卡对应的intent
         tabhost.addTab(spec);//将选项卡添加到tabhost

         tabhost.setCurrentTab(1);//设置默认选项卡   

案例结果:

选项卡代码:(可以自定义样式文件对tab1内容进行丰富,本次只用代码添加内容)

 1 package caicai.tabhost;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.TextView;
 6 
 7 public class tab1 extends Activity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         // TODO Auto-generated method stub
12         super.onCreate(savedInstanceState);
13         TextView textview=new TextView(this);
14         textview.setText("这是tab1上的内容");
15         setContentView(textview);
16     }    
17 }
tab1
 1 package caicai.tabhost;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.TextView;
 6 
 7 public class tab2 extends Activity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         // TODO Auto-generated method stub
12         super.onCreate(savedInstanceState);
13         TextView textview=new TextView(this);
14         textview.setText("这是tab2上的内容");
15         setContentView(textview);
16     }
17 
18     
19     
20 }
tab2
 1 package caicai.tabhost;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.TextView;
 6 
 7 public class tab3 extends Activity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         // TODO Auto-generated method stub
12         super.onCreate(savedInstanceState);
13         TextView textview=new TextView(this);
14         textview.setText("这是tab3上的内容");
15         setContentView(textview);
16     }
17 
18     
19     
20 }
tab3

主类代码:

 1 package caicai.tabhost;
 2 
 3 import java.util.ResourceBundle;
 4 
 5 import android.app.Activity;
 6 import android.app.TabActivity;
 7 import android.content.Intent;
 8 import android.content.res.Resources;
 9 import android.os.Bundle;
10 import android.widget.TabHost;
11 
12 public class TabhostActivity extends TabActivity {
13     /** Called when the activity is first created. */
14     @Override
15     public void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.main);
18         //获取资源对象
19         Resources res=getResources();
20         //获取host对象
21         TabHost tabhost=getTabHost();
22         //声明可以复用的tabspec
23          TabHost.TabSpec spec;
24         //声明可以复用的intent
25          Intent intent;
26                 
27          intent=new Intent().setClass(this, tab1.class);//创建启动的intent
28          spec=tabhost.newTabSpec("tab1");//实例化选项卡tabspec,tab1为选项卡的标记
29          spec.setIndicator("Tab1",res.getDrawable(R.drawable.tab11));//设置选项卡的显示名字与图片
30          spec.setContent(intent);//绑定选项卡对应的intent
31          tabhost.addTab(spec);//将选项卡添加到tabhost
32          
33          intent=new Intent().setClass(this, tab2.class);
34          spec=tabhost.newTabSpec("tab2");
35          spec.setIndicator("Tab2",res.getDrawable(R.drawable.tab11));
36          spec.setContent(intent);
37          tabhost.addTab(spec);
38          
39          intent=new Intent().setClass(this, tab3.class);
40          spec=tabhost.newTabSpec("tab3");
41          spec.setIndicator("Tab3",res.getDrawable(R.drawable.tab11));
42          spec.setContent(intent);
43          tabhost.addTab(spec);
44          
45          tabhost.setCurrentTab(1);//设置默认选项卡       
46     }
47 }
TabhostActivity

主界面布局代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:id="@android:id/tabhost">
 6 <LinearLayout 
 7     android:layout_width="fill_parent"
 8     android:layout_height="fill_parent"
 9     android:orientation="vertical" 
10     android:padding="5dp">
11    
12     <FrameLayout 
13        android:id="@android:id/tabcontent"
14        android:layout_width="fill_parent"
15        android:layout_height="wrap_content"
16        android:layout_weight="1"
17        android:padding="5dp"
18         />
19     
20      <TabWidget 
21        android:id="@android:id/tabs"
22        android:layout_width="fill_parent"
23        android:layout_height="wrap_content"/>
24 </LinearLayout>
25 </TabHost>
main

配置文件代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="caicai.tabhost"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".TabhostActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".tab1"/>
            <activity android:name=".tab2"/>
                <activity android:name=".tab3"/>
    </application>

</manifest>
AndroidManifest.xml

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

如果想让选项卡位置为顶部只要更改主界面布局代码即可

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:id="@android:id/tabhost">
 6 <LinearLayout 
 7     android:layout_width="fill_parent"
 8     android:layout_height="fill_parent"
 9     android:orientation="vertical" 
10     android:padding="5dp">
11     
12    <TabWidget 
13        android:id="@android:id/tabs"
14        android:layout_width="fill_parent"
15        android:layout_height="wrap_content"/>
16    
17     <FrameLayout 
18        android:id="@android:id/tabcontent"
19        android:layout_width="fill_parent"
20        android:layout_height="wrap_content"
21        android:padding="5dp"
22         />
23     
24 </LinearLayout>
25 </TabHost>
main

结果如图:

原文地址:https://www.cnblogs.com/clarence/p/3296790.html