Android入门第十一篇之TabHost,TabWidget .

这回要介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要的时候再使用。Android的Tab控件使用起来有点奇怪,必须包含和按照以下的顺序:

TabHost控件->TabWidget(必须命名为tabs)->FrameLayout(必须命名为tabcontent)。

接下来贴出本例运行的截图:

main.xml的源码:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TabHostandroid:layout_width="fill_parent"
  3. android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/TabHost1">
  4. <TabWidgetandroid:id="@android:id/tabs"
  5. android:layout_height="wrap_content"android:layout_width="fill_parent">
  6. </TabWidget>
  7. <FrameLayoutandroid:id="@android:id/tabcontent"
  8. android:paddingTop="65px"android:layout_width="fill_parent"android:layout_height="fill_parent">
  9. <LinearLayoutandroid:layout_height="wrap_content"android:id="@+id/Tab1"android:orientation="vertical"android:layout_width="fill_parent">
  10. <EditTextandroid:layout_height="wrap_content"android:id="@+id/edtTab1"android:layout_width="fill_parent"></EditText>
  11. <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btnTab1"android:text="Tab1"></Button>
  12. </LinearLayout>
  13. <LinearLayoutandroid:layout_height="wrap_content"android:id="@+id/Tab2"android:layout_width="fill_parent"android:orientation="horizontal">
  14. <EditTextandroid:layout_height="wrap_content"android:id="@+id/edtTab2"android:layout_width="wrap_content"android:layout_weight="300"></EditText>
  15. <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btnTab2"android:text="Tab2"></Button></LinearLayout>
  16. </FrameLayout>
  17. </TabHost>
<?xml version="1.0" encoding="utf-8"?> <TabHost android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/TabHost1"> <TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:paddingTop="65px" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab1" android:orientation="vertical" android:layout_width="fill_parent"> <EditText android:layout_height="wrap_content" android:id="@+id/edtTab1" android:layout_width="fill_parent"></EditText> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab1" android:text="Tab1"></Button> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab2" android:layout_width="fill_parent" android:orientation="horizontal"> <EditText android:layout_height="wrap_content" android:id="@+id/edtTab2" android:layout_width="wrap_content" android:layout_weight="300"></EditText> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab2" android:text="Tab2"></Button></LinearLayout> </FrameLayout> </TabHost>

程序源码:

  1. packagecom.testTab;
  2. importandroid.app.TabActivity;
  3. importandroid.os.Bundle;
  4. importandroid.view.View;
  5. importandroid.widget.Button;
  6. importandroid.widget.EditText;
  7. importandroid.widget.TabHost;
  8. importandroid.widget.TabHost.TabSpec;
  9. publicclasstestTabextendsTabActivity{//基于TabActivity构建
  10. ButtonbtnTab1,btnTab2;
  11. EditTextedtTab1,edtTab2;
  12. /**Calledwhentheactivityisfirstcreated.*/
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. TabHosttabs=getTabHost();
  18. //设置Tab1
  19. TabSpectab1=tabs.newTabSpec("tab1");
  20. tab1.setIndicator("tab1");//设置tab1的名称
  21. tab1.setContent(R.id.Tab1);//关联控件
  22. tabs.addTab(tab1);//添加tab1
  23. btnTab1=(Button)this.findViewById(R.id.btnTab1);
  24. edtTab1=(EditText)this.findViewById(R.id.edtTab1);
  25. btnTab1.setOnClickListener(newClickEvent());
  26. //设置Tab2
  27. TabSpectab2=tabs.newTabSpec("tab2");
  28. tab2.setIndicator("tab2");
  29. tab2.setContent(R.id.Tab2);
  30. tabs.addTab(tab2);
  31. btnTab2=(Button)this.findViewById(R.id.btnTab2);
  32. edtTab2=(EditText)this.findViewById(R.id.edtTab2);
  33. btnTab2.setOnClickListener(newClickEvent());
  34. tabs.setCurrentTab(0);
  35. }
  36. classClickEventimplementsView.OnClickListener{
  37. @Override
  38. publicvoidonClick(Viewv){
  39. if(v==btnTab1)
  40. {
  41. edtTab1.setText("tab1");
  42. }
  43. elseif(v==btnTab2)
  44. {
  45. edtTab2.setText("tab2");
  46. }
  47. }
  48. }
  49. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值