TabHost的使用

1.TabHost的xml文件写法:

 android:id="@android:id/tabhost"    

android:id="@android:id/tabs"

android:id="@android:id/tabcontent"都是固有的写法

<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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="5dp" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>

</TabHost>  

 

mainActivity:

 

 1 package mars.mp3player;
 2 
 3 import android.app.TabActivity;
 4 import android.content.Intent;
 5 import android.content.res.Resources;
 6 import android.os.Bundle;
 7 import android.widget.TabHost;
 8 
 9 public class MainActivity extends TabActivity {
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.main);
14 
15         // 得到TabHost对象,正对TabActivity的操作通常都有这个对象完成
16         TabHost tabHost = getTabHost();
17         // 生成一个TabSpec对象,这个对象代表了一个页
18         TabHost.TabSpec remoteSpec = tabHost.newTabSpec("Remote");
19         Resources res = getResources();
20         // 设置该页的indicator
21         remoteSpec.setIndicator("Remote",
22                 res.getDrawable(android.R.drawable.stat_sys_download));
23         // 设置该页的内容
24         // 生成一个Intent对象,该对象指向一个Activity
25         Intent remoteIntent = new Intent();
26         remoteIntent.setClass(this, Mp3ListActivity.class);
27         remoteSpec.setContent(remoteIntent);
28         // 将设置好的TabSpec对象添加到TabHost当中
29         tabHost.addTab(remoteSpec);
30 
31         TabHost.TabSpec localSpec = tabHost.newTabSpec("Local");
32         localSpec.setIndicator("Local",
33                 res.getDrawable(android.R.drawable.stat_sys_upload));
34         Intent localIntent = new Intent();
35         localIntent.setClass(this, LocalMp3ListActivity.class);
36 
37         localSpec.setContent(localIntent);
38         tabHost.addTab(localSpec);
39     }
40 }

 

 

在TabHost中要设置setIndicator()和setContent()两个,先由TabHost.TabSpec remoteSpec = tabHost.newTabSpec("Remote");生产一页的对象

 

 

posted on 2012-10-12 17:48  liyajun2012  阅读(177)  评论(0编辑  收藏  举报

导航