android空间学习之tabhost

学习完布局,接下来就是一些常用的控件了,这里介绍下tabhost

先上布局xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <TabHost
 3 android:id="@android:id/tabhost"
 4 xmlns:android="http://schemas.android.com/apk/res/android"
 5 android:layout_width="fill_parent" android:layout_height="fill_parent">
 6 <TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent">
 7 </TabWidget>
 8 <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">
 9 </FrameLayout>
10 </TabHost>
上面的配置文件就是tabhost的基础布局
TabHost,TabWidget,FrameLayout三个标签对应的id必须是tabhost,tabs,tabcontent,选项卡的内容布局必须是framelayout布局

否则会报空指针异常提示相应的id缺少
比如:Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'

布局后的xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <TabHost
 3 android:id="@android:id/tabhost"
 4 xmlns:android="http://schemas.android.com/apk/res/android"
 5 android:layout_width="fill_parent" android:layout_height="fill_parent">
 6 <LinearLayout android:id="@+id/linearlayout1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
 7 <TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent">
 8 </TabWidget>
 9 <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">
10 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tabcontent1" android:text="第一个选项卡内容"></TextView>
11           <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tabcontent2" android:text="第二个选项卡内容"></TextView>
12           <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tabcontent3" android:text="第三个选项卡内容"></TextView>
13 </FrameLayout>
14 </LinearLayout>
15 </TabHost>

java代码

 1 public class TabWidgetActivity extends TabActivity {
 2     private TabHost mTabHost;
 3 
 4     @Override
 5     protected void onCreate(Bundle savedInstanceState) {
 6         // TODO Auto-generated method stub
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.tab_widget);
 9 
10         //获取TabHoast对象
11         mTabHost=getTabHost();
12         /* 为TabHost添加标签 */
13         //新建一个newTabSpec(newTabSpec)
14         //设置其标签和图标(setIndicator)
15         //设置内容(setContent)
16         Intent intent=new Intent();
17         intent.setClass(this, Test.class);
18         mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB1",getResources().getDrawable(R.drawable.img1)).setContent(R.id.tabcontent1));
19         //mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB2",getResources().getDrawable(R.drawable.img2)).setContent(R.id.tabcontent2));
20         mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB2",getResources().getDrawable(R.drawable.img2)).setContent(intent));
21         mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB3",getResources().getDrawable(R.drawable.img3)).setContent(R.id.tabcontent3));
22         
23         //设置TabHost的背景颜色
24         mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
25         
26         //显示第一个tab
27         mTabHost.setCurrentTab(0);
28 
29     }
30 }

这里解释下

newTabSpec(String tab) 返回一个TagSpec对象(即一个选项卡对象)
addTab(TabHost.TabSpec tabSpec)添加选项卡

setIndicator(CharSequence label, Drawable icon)设置标签和图片资源
setContent(int viewId)添加选项卡面板内容(也可以是一个intent 跳转到另外一个activity)

添加一个tabwidget的过程如下
1      TabSpec tabObj = mTabHost.newTabSpec("选项卡对象");
2         tabObj.setContent(intent);//添加内容视图
3         Resources resources = getResources();//创建资源对象
4         Drawable drawable = resources.getDrawable(R.drawable.icon);//添加资源内容
5         tabObj.setIndicator("标签名", drawable);//往选项卡添加资源对象

注意的是类必须继承TabActivity 



posted @ 2012-10-26 10:47  draem0507  阅读(601)  评论(0编辑  收藏  举报
View Code