Android Tabhost 使用方法

 

1.概念

盛放tab的容器。可以通过两种方式实现:

方法一:不继承tabActibity,通过setup()建立tabhost;通过setcontent()获取页面布局。但是布局文件的格式有规范。必须是tabhost 包含 tabwidget(id必须为tabs);tabhost还包含 framelayout(id必须为 tabcontent),framelayout里面放置你的布局,我的理解是由于帧布局,一个布局遮住另一个布局,所以可以实现切换。

方法二:继承tabActivity,通过tabActivity中的中gethost()方法获取tabhost;通过LayoutInflater.from(this).inflate()取得tab的布局,默认这整个页面的最大框架将是tabhost(所以会出现无法将tabhost里面的页头移到中间或结尾的情况)。

2.实现
方法一:
xml 文件:activity_group.xml
 
xml 文件:activity_group.xml
  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     android:layout_width="fill_parent"
  3     android:layout_height="fill_parent"
  4     android:background="#fff"
  5     android:orientation="vertical" >
  6 
  7     <FrameLayout
  8         android:layout_width="match_parent"
  9         android:layout_height="45dp"
 10         android:background="@drawable/repeat_bg"
 11         android:orientation="horizontal" >
 12 
 13         <Button
 14             android:id="@+id/button1"
 15             android:layout_width="42dp"
 16             android:layout_height="32dp"
 17             android:layout_gravity="right"
 18             android:layout_marginTop="5dp"
 19             android:background="@drawable/btn_refresh_selector"
 20             android:gravity="center" />
 21 
 22         <TextView
 23             android:id="@+id/textView1"
 24             android:layout_width="wrap_content"
 25             android:layout_height="wrap_content"
 26             android:layout_gravity="center"
 27             android:gravity="center"
 28             android:text="Tabhost例子"
 29             android:textColor="#fff"
 30             android:textSize="18dip" />
 31     </FrameLayout>
 32      <!-- 以上为标题 -->
 33      <!-- 以下为tabhost -->
 34     <LinearLayout
 35         android:layout_width="fill_parent"
 36         android:layout_height="fill_parent"
 37         android:layout_weight="1"
 38         android:orientation="vertical" >
 39 
 40         <TabHost
 41             android:id="@+id/tabhost"
 42             android:layout_width="fill_parent"
 43             android:layout_height="wrap_content" >
 44 
 45             <LinearLayout
 46                 android:layout_width="fill_parent"
 47                 android:layout_height="fill_parent"
 48                 android:orientation="vertical" >
 49 
 50                 <TabWidget
 51                     android:id="@android:id/tabs"
 52                     android:layout_width="fill_parent"
 53                     android:layout_height="wrap_content"
 54                     android:orientation="horizontal" />
 55 
 56                 <FrameLayout
 57                     android:id="@android:id/tabcontent"
 58                     android:layout_width="fill_parent"
 59                     android:layout_height="fill_parent" >
 60 
 61                     <LinearLayout
 62                         android:id="@+id/tab01"
 63                         android:layout_width="fill_parent"
 64                         android:layout_height="fill_parent"
 65                         android:orientation="vertical" >
 66 
 67                         <ListView
 68                             android:id="@+id/lv1"
 69                             android:layout_width="fill_parent"
 70                             android:layout_height="wrap_content" >
 71                         </ListView>
 72                     </LinearLayout>
 73 
 74                     <LinearLayout
 75                         android:id="@+id/tab02"
 76                         android:layout_width="fill_parent"
 77                         android:layout_height="fill_parent"
 78                         android:orientation="vertical" >
 79 
 80                         <TextView
 81                             android:layout_width="fill_parent"
 82                             android:layout_height="wrap_content"
 83                             android:text="萨僧-2011/07/11" />
 84 
 85                         <TextView
 86                             android:layout_width="fill_parent"
 87                             android:layout_height="wrap_content"
 88                             android:text="唐僧-2011/07/10" />
 89                     </LinearLayout>
 90 
 91                     <LinearLayout
 92                         android:id="@+id/tab03"
 93                         android:layout_width="fill_parent"
 94                         android:layout_height="fill_parent"
 95                         android:orientation="vertical" >
 96 
 97                         <TextView
 98                             android:layout_width="fill_parent"
 99                             android:layout_height="wrap_content"
100                             android:text="萨僧-2011/07/11" />
101 
102                         <TextView
103                             android:layout_width="fill_parent"
104                             android:layout_height="wrap_content"
105                             android:text="唐僧-2011/07/10" />
106                     </LinearLayout>
107                 </FrameLayout>
108             </LinearLayout>
109         </TabHost>
110     </LinearLayout>
111 
112     <LinearLayout
113         android:layout_width="match_parent"
114         android:layout_height="wrap_content"
115         android:layout_marginBottom="4dip"
116         android:layout_marginTop="10dip"
117         android:background="#E4E4E2"
118         android:orientation="vertical" >
119 
120         <LinearLayout
121             android:layout_width="match_parent"
122             android:layout_height="wrap_content"
123             android:background="@drawable/hr_bottom" >
124         </LinearLayout>
125 
126         <Button
127             android:layout_width="match_parent"
128             android:layout_height="40dip"
129             android:layout_marginLeft="30dip"
130             android:layout_marginRight="30dip"
131             android:background="@drawable/btn_finished"
132             android:gravity="center"
133             android:text="开始新的活动  >>"
134             android:textColor="#fff"
135             android:textSize="20dip" />
136     </LinearLayout>
137 
138 </LinearLayout>
java文件:GroupActivity
 1 public class GroupActivity extends Activity {
 2     @Override
 3     public void onCreate(Bundle savedInstanceState) {
 4         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
 5 
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.activity_group);         
 8         TabHost tabHost = (TabHost) findViewById(R.id.tabhost);            
 9         tabHost.setup();  
10         tabHost.addTab(tabHost.newTabSpec("tab01")
11                 .setIndicator(createTab("当前热议")).setContent(R.id.tab01));
12         tabHost.addTab(tabHost.newTabSpec("tab02")
13                 .setIndicator(createTab("我发起的")).setContent(R.id.tab02));
14         tabHost.addTab(tabHost.newTabSpec("tab03")
15                 .setIndicator(createTab("已删除的")).setContent(R.id.tab03));
16         // ListVIew适配器
17         SimpleAdapter adapter = new SimpleAdapter(this, getData(),
18                 R.layout.listv, new String[] { "number", "title", "time",
19                         "info" }, new int[] { R.id.number, R.id.title,
20                         R.id.time, R.id.info });
21         ListView lv = (ListView) findViewById(R.id.lv1);
22         lv.setAdapter(adapter);
23 
24     }

方法二:xml中可以没有tabwidget和framelayout,但是要有tabhost。例如:<tabhost><LinearLayout></LinearLayout>><LinearLayout></LinearLayout>><LinearLayout></LinearLayout></tabhost>

LinearLayout中贴自己的每个布局。此处我就不贴xml文件了

下面是java文件的关键代码:(类要继承tabactivity:public class GroupActivity extends TabActivity)

super.onCreate(savedInstanceState);
        TabHost tabHost = getTabHost();
        LayoutInflater.from(this).inflate(R.layout.activity_group,
                tabHost.getTabContentView(), true);
        tabHost.addTab(tabHost.newTabSpec("tab01")
                .setIndicator(createTab("当前热议")).setContent(R.id.tab01));

 

要看实例可以看这位大牛的~我的是在他的基础上加了自己的理解,若有错误还请指正
posted @ 2012-07-16 08:39  石沉溪涧  阅读(7356)  评论(0编辑  收藏  举报