查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧!

首先先看一个小例子,接着讲授理原

  1. TabTest.java
  2. view plaincopy to clipboardprint?
  3. package org.hualang.tab;
  4. import android.app.Activity;
  5. import android.app.TabActivity;
  6. import android.graphics.Color;
  7. import android.os.Bundle;
  8. import android.widget.TabHost;
  9. import android.widget.Toast;
  10. import android.widget.TabHost.OnTabChangeListener;
  11. public class TabTest extends TabActivity {
  12. /** Called when the activity is first created. */
  13. TabHost tabhost;
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. //获得TabHost对象
  19. tabhost = getTabHost();
  20. //为TabHost添加签标
  21. //新建一个newTabSpec(newTabSpec)
  22. //设置其签标和标图(setIndicator)
  23. //设置容内(setContent)
  24. tabhost.addTab(tabhost.newTabSpec("tab1")
  25. .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))
  26. .setContent(R.id.text1));
  27. tabhost.addTab(tabhost.newTabSpec("tab2")
  28. .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))
  29. .setContent(R.id.text2));
  30. tabhost.addTab(tabhost.newTabSpec("tab3")
  31. .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))
  32. .setContent(R.id.text3));
  33. //设置TabHost的背景颜色
  34. //tabhost.setBackgroundColor(Color.argb(150,22,70,150));
  35. //设置TabHost的背景图片资源
  36. tabhost.setBackgroundResource(R.drawable.bg0);
  37. //设置前当示显哪个签标
  38. tabhost.setCurrentTab(0);
  39. //签标换切事件处理,setOnTabChangedListener
  40. tabhost.setOnTabChangedListener(new OnTabChangeListener()
  41. {
  42. public void onTabChanged(String tabId)
  43. {
  44. Toast toast=Toast.makeText(getApplicationContext(), "现在是"+tabId+"签标", Toast.LENGTH_SHORT);
  45. toast.show();
  46. }
  47. });
  48. }
  49. }

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent">
  10. <TabWidget
  11. android:id="@android:id/tabs"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content" />
  14. <FrameLayout
  15. android:id="@android:id/tabcontent"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent">
  18. <TextView
  19. android:id="@+id/text1"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. android:text="选项卡1" />
  23. <TextView
  24. android:id="@+id/text2"
  25. android:layout_width="fill_parent"
  26. android:layout_height="fill_parent"
  27. android:text="选项卡2" />
  28. <TextView
  29. android:id="@+id/text3"
  30. android:layout_width="fill_parent"
  31. android:layout_height="fill_parent"
  32. android:text="选项卡3" />
  33. </FrameLayout>
  34. </LinearLayout>
  35. </TabHost>


Android TabWidget的实现可以分为二种,一种是应用准标TabActivity实现,另外一种可以自定义方法实现,种这方法实现起来对相较比复杂,但对于要实现较比多元化的view是很好的,这里我们简略看下源码

一、通用做法

继承TabActivity,实现自己的TabActivity

[java] view plaincopy
  1. import android.app.Activity;  
  2. import android.app.TabActivity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.widget.TabHost;  
  6. import android.widget.TabHost.OnTabChangeListener;  
  7. public class TabWidgetDemo2 extends TabActivity implements OnTabChangeListener {  
  8.      private TabHost mTabHost;  
  9.        
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         // TODO Auto-generated method stub  
  13.         super.onCreate(savedInstanceState);  
  14.           
  15.         setContentView(R.layout.tabwidgetdemo2);    
  16.         mTabHost = getTabHost();  
  17.         mTabHost.setOnTabChangedListener(this);  
  18.         setupTab1();  
  19.         setupTab2();  
  20.         mTabHost.setCurrentTab(1);  
  21.     }  
  22.     private void setupTab2() {  
  23.         // TODO Auto-generated method stub  
  24.         Intent intent = new Intent();  
  25.         intent.setAction(Intent.ACTION_MAIN);  
  26.         intent.setClass(this, TabWidget2.class);  
  27.         mTabHost.addTab(mTabHost.newTabSpec("TabWidget2")  
  28.                 .setIndicator("TabWidget2",getResources().getDrawable(R.drawable.icon))  
  29.                 .setContent(intent));  
  30.     }  
  31.     private void setupTab1() {  
  32.         // TODO Auto-generated method stub  
  33.         Intent intent = new Intent();  
  34.         intent.setAction(Intent.ACTION_MAIN);  
  35.         intent.setClass(this, TabWidget1.class);  
  36.         mTabHost.addTab(mTabHost.newTabSpec("TabWidget1")  
  37.                 .setIndicator("TabWidget1",getResources().getDrawable(R.drawable.icon))  
  38.                 .setContent(intent));  
  39.     }  
  40.     public void onTabChanged(String tabId) {  
  41.         // TODO Auto-generated method stub  
  42.         Activity activity = getLocalActivityManager().getActivity(tabId);  
  43.         if (activity != null) {  
  44.             activity.onWindowFocusChanged(true);  
  45.         }  
  46.     }  
  47.        
  48.        
  49. }  

二个tab对应的Activity,先看TabWidget1,这个类在第二种实现中还会用到,因此我们可以看到对Action的判断。

[java] view plaincopy
  1. import android.app.Activity;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import com.android.exampledemo.R;  
  5. import com.android.exampledemo.util.DemoUtils;  
  6. public class TabWidget1 extends Activity {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         // TODO Auto-generated method stub  
  10.         super.onCreate(savedInstanceState);  
  11.           
  12.         Intent intent = this.getIntent();  
  13.         if (intent.getAction().equals(Intent.ACTION_MAIN)){  
  14.             setContentView(R.layout.tabwidgetdemo2_1);  
  15.         }  
  16.         else {  
  17.             setContentView(R.layout.tabwidget_1);  
  18.             DemoUtils.updateButtonBar((Activity)this,R.id.contactstab);  
  19.         }  
  20.     }  
  21. }  

 

再看一下TabWidget2,这个Activity我们在第二种实现方法中也会用到。

[java] view plaincopy
  1. import com.android.exampledemo.R;  
  2. import com.android.exampledemo.util.DemoUtils;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. public class TabWidget2 extends Activity {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         // TODO Auto-generated method stub  
  10.         super.onCreate(savedInstanceState);  
  11.           
  12.         Intent intent = this.getIntent();  
  13.           
  14.         if (intent.getAction().equals(Intent.ACTION_MAIN)){  
  15.             setContentView(R.layout.tabwidgetdemo2_1);  
  16.         }  
  17.         else {  
  18.             setContentView(R.layout.tabwidget_2);  
  19.             DemoUtils.updateButtonBar((Activity)this,R.id.groupstab);  
  20.         }  
  21.     }  
  22. }  

 

最后就是各个Activity对应的layout

1.tabwidgetdemo2.xml

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:id="@android:id/tabhost"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent">  
  7.   <LinearLayout   
  8.     android:orientation="vertical"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="fill_parent">  
  11.     <TabWidget android:id="@android:id/tabs"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="68dip"  
  14.         android:paddingLeft="1dip"  
  15.         android:paddingRight="1dip"  
  16.         android:paddingTop="4dip"  
  17.         />  
  18.     <FrameLayout android:id="@android:id/tabcontent"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="0dip"  
  21.         android:layout_weight="1"  
  22.         />  
  23.     </LinearLayout>   
  24. </TabHost>  

2.二个sub tab对应的layout

[xhtml] view plaincopy
  1. Layout1  
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <LinearLayout  
  4.   xmlns:android="http://schemas.android.com/apk/res/android"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent"  
  7.   android:background="#FFF">  
  8.   <TextView android:id="@+id/textview"   
  9.     android:layout_width="wrap_content"   
  10.     android:layout_height="wrap_content"  
  11.     android:text="Tab Widget first">  
  12.    </TextView>  
  13. </LinearLayout>  
  14. Layout2  
  15. <?xml version="1.0" encoding="utf-8"?>  
  16. <LinearLayout  
  17.   xmlns:android="http://schemas.android.com/apk/res/android"  
  18.   android:layout_width="fill_parent"  
  19.   android:layout_height="fill_parent"  
  20.   android:background="#FFF">  
  21.   <TextView android:id="@+id/textview"   
  22.     android:layout_width="wrap_content"   
  23.     android:layout_height="wrap_content"  
  24.     android:text="Tab Widget second">  
  25.    </TextView>  
  26. </LinearLayout>  

 

 

方法2:

先创立一个Activity (TabWidgetDemo)

[c-sharp] view plaincopy
  1. 1.TabWidgetDemo.java  
  2. import com.android.exampledemo.R;  
  3. import com.android.exampledemo.util.DemoUtils;  
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.content.SharedPreferences;  
  7. import android.os.Bundle;  
  8. //not use tabhost to organized   
  9. public class TabWidgetDemo extends Activity {  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         // TODO Auto-generated method stub  
  13.         super.onCreate(savedInstanceState);  
  14.         //int activeTab = DemoUtils.getIntPref(this, "activetab", R.id.artisttab);  
  15.         SharedPreferences prefs =  
  16.             getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);  
  17.         int activeTab = prefs.getInt("activetab", R.id.contactstab);  
  18.         if (activeTab != R.id.contactstab  
  19.                 && activeTab != R.id.groupstab) {  
  20.             activeTab = R.id.contactstab;  
  21.         }  
  22.         DemoUtils.activateTab(this, activeTab);  
  23.     }  
  24. }  
  25. 2.DemoUtils  
  26. import android.app.Activity;  
  27. import android.content.Intent;  
  28. import android.net.Uri;  
  29. import android.view.View;  
  30. import android.widget.TabWidget;  
  31. import com.android.exampledemo.R;  
  32. public class DemoUtils {  
  33.     static int sActiveTabIndex = -1;  
  34.       
  35.     public static void activateTab(Activity a,int active_id){  
  36.         Intent intent = new Intent(Intent.ACTION_PICK);  
  37.         switch (active_id) {  
  38.         case R.id.contactstab:  
  39.             intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/tb_contacts");  
  40.             break;  
  41.         case R.id.groupstab:  
  42.             intent.setDataAndType(Uri.EMPTY, "vnd.android.cursor.dir/tb_groups");  
  43.             break;  
  44.         default:  
  45.             return;  
  46.         }  
  47.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  48.         a.startActivity(intent);  
  49.         a.finish();  
  50.         a.overridePendingTransition(0,0);  
  51.     }  
  52.       
  53.       
  54.     public static void updateButtonBar(Activity a, int highlight) {  
  55.         final TabWidget ll = (TabWidget) a.findViewById(R.id.buttonbar);  
  56.           
  57.          for (int i = ll.getChildCount() - 1; i >= 0; i--) {  
  58.              View v = ll.getChildAt(i);  
  59.              boolean isActive = (v.getId() == highlight);  
  60.              if (isActive) {  
  61.                     ll.setCurrentTab(i);  
  62.                     sActiveTabIndex = i;  
  63.              }  
  64.                
  65.              v.setTag(i);  
  66.              v.setOnClickListener(new View.OnClickListener() {  
  67.                     public void onClick(View v) {  
  68.                         int id = v.getId();  
  69.                         if (id == ll.getChildAt(sActiveTabIndex).getId()) {  
  70.                             return;  
  71.                         }  
  72.                         activateTab((Activity)ll.getContext(),id );  
  73.                         ll.setCurrentTab((Integer) v.getTag());  
  74.                     }});  
  75.          }  
  76.     }  
  77. }  

 

 

二个Tab sub activity前一方法中经已给出,这里我们只需要看一下layout的实现就能够了

1>buttonbar.xml

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabWidget xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/buttonbar"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content" >  
  6.     <TextView  
  7.         android:id="@+id/contactstab"  
  8.         android:focusable="true"  
  9.         android:drawableTop="@drawable/icon"  
  10.         android:background="@drawable/buttonbarbackground"  
  11.         android:text="Contacts"  
  12.         android:textColor="@color/tab_indicator_text"  
  13.         android:textAppearance="?android:attr/textAppearanceSmall"  
  14.         android:paddingTop="7dip"  
  15.         android:paddingBottom="2dip"  
  16.         android:gravity="center"  
  17.         android:layout_weight="1"  
  18.         android:layout_marginLeft="-3dip"  
  19.         android:layout_marginRight="-3dip"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="84dip"  
  22.         android:singleLine="true"  
  23.         android:ellipsize="marquee" />  
  24.     <TextView  
  25.         android:id="@+id/groupstab"  
  26.         android:focusable="true"  
  27.         android:drawableTop="@drawable/icon"  
  28.         android:background="@drawable/buttonbarbackground"  
  29.         android:text="Group"  
  30.         android:textColor="@color/tab_indicator_text"  
  31.         android:textAppearance="?android:attr/textAppearanceSmall"  
  32.         android:paddingTop="7dip"  
  33.         android:paddingBottom="2dip"  
  34.         android:gravity="center"  
  35.         android:layout_weight="1"  
  36.         android:layout_marginLeft="-3dip"  
  37.         android:layout_marginRight="-3dip"  
  38.         android:layout_width="match_parent"  
  39.         android:layout_height="84dip"  
  40.         android:singleLine="true"  
  41.         android:ellipsize="marquee" />  
  42. </TabWidget>  

2>tabwidget_1.xml

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">  
  6.     
  7.   <include layout="@layout/battonbar" />  
  8.     
  9.   <ExpandableListView android:id="@+id/android:list"  
  10.                   android:layout_width="fill_parent"   
  11.                   android:layout_height="wrap_content"  
  12.                   android:footerDividersEnabled="true"  
  13.                   android:fadeScrollbars="true"  
  14.                   android:drawSelectorOnTop="true">  
  15.   </ExpandableListView>  
  16.     
  17. </LinearLayout>  

3> tabwidget_2.xml

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">  
  6.     
  7.   <include layout="@layout/battonbar" />  
  8.     
  9. </LinearLayout>  

 

文章结束给大家分享下程序员的一些笑话语录: 真正的程序员喜欢兼卖爆米花,他们利用CPU散发出的热量做爆米花,可以根据米花爆裂的速度听出正在运行什么程序。

转载于:https://www.cnblogs.com/jiangu66/archive/2013/04/14/3020375.html

实现、设置-Android TabWidget-by小雨相关推荐

  1. Android TabWidget

    首先先看一个小例子,接着讲解原理 TabTest.java view plaincopy to clipboardprint? package org.hualang.tab; import andr ...

  2. android 固定底部导航,如何设置android底部导航栏位置固定在android

    请帮我设置底部导航栏位置固定在底部, ,因为我在输入editText字段时遇到问题,底部导航栏向上移动并覆盖其他领域如何设置android底部导航栏位置固定在android 代码: xmlns:and ...

  3. 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性

    在进行UI布局的时候,可能常常会用到 android:gravity  和 android:layout_Gravity 这两个属性. 关于这两个属性的差别,网上已经有许多人进行了说明,这边再简单说一 ...

  4. android重新编译res,使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中lt;meta-datagt;变量的值...

    你也能够查看我的其它同类文章.也会让你有一定的收货 关于使用Gradle来控制版本号和生成不同版本号的代码.我总结了三篇文章,网上关于这些知识,都比較零散.我在学习这些的之前.根本不知道还有这种方法. ...

  5. (转)Android属性设置android:noHistory=true

    设置 android:noHistory="true"后,该Activity在statck中不留历史痕迹.默认的值是false. 举例说明,假设有三个Activity分别是:A,B ...

  6. GridView xml中设置android:focusable=false无效的原因

    最近Tv项目中有个小问题,需要gridview展示内容,但是不可获取焦点,于是xml中设置android:focusable="false",设想会成功,但是实际操作,发现还是可以 ...

  7. 使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中lt;meta-datagt;变量的值...

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/49247815 文章出自:薛瑄的博客 你也能够查看我的其它同类文章.也会让你有一定的 ...

  8. android gradle 设置,android gradle配置指南

    Gradle简介 Gradle 是一个基于Ant和Maven概念的项目自动化建构工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,这比我们的ANT使用XML构建配置要灵活的多.在 ...

  9. android ctrl 左键鼠标左键直接打开xml文件夹,设置Android Studio通过Ctrl+左键查看源码...

    开始学习android的时候希望能点进系统提供的控件中查看源码,但是实际操作发现,看到的每个源文件方法都是抛出的Exception,所以想要设置成可以直接查看具体实现,记录下我自己的操作方法. 1.首 ...

最新文章

  1. [POI2009]KAM-Pebbles BZOJ1115 [ 待填坑 ] 博弈
  2. 我把帮带份饭的信息错发给导师后.......
  3. Windows 如何在cmd命令行中查看、修改、删除与添加环境变量
  4. 用aspx开发html5页面,ASP.NET使aspx页面能接受HTML,asp的页面传送的文件-.NET教程,Asp.Net开发...
  5. pyspark报错java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
  6. mysql8.0提示命令_Mysql 8.0 相关命令
  7. CF 1475 D. Cleaning the Phone 思维模型
  8. 操作系统学习笔记-2.1.1.进程的定义、组成、组织方式、特征
  9. mysql连接数据库的包_java连接mysql数据库包
  10. Sqoop导入数据发生数据倾斜问题 及更好解决
  11. 在Sql Server 2008上安装SDE 9.3
  12. 共轭函数Fenchel不等式
  13. SAP在阿里云白皮书-第二章 阿里云概念解析
  14. matlab自动运行,在指定时间自动运行Matlab程序
  15. HTML——选择器(1)
  16. linux远程登录maridb,linux – 无法使用phpMyAdmin登录到远程MariaDB服务器,但在shell中工作...
  17. 2021年R1快开门式压力容器操作考试题及R1快开门式压力容器操作实操考试视频
  18. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分解决办法
  19. 网络设置巨形帧_网卡设置 网卡的高级设置说明
  20. git 查看自己秘钥_git秘钥配置--转

热门文章

  1. 前端学习(3280):iterator
  2. 前端学习(3249):react的文件src
  3. react学习(21)---接口加回调显示
  4. 工作372-cookie和sessionstroage以及localstroage区别
  5. [css] 手动写动画最小时间间隔是多少,为什么?
  6. [css] 说出至少十条你理解的css规范
  7. 前端学习(2656):vue2中用v-model实现
  8. 前端学习(2522):添加博客的样式
  9. “睡服”面试官系列第六篇之set数据结构(建议收藏学习)
  10. 前端学习(603):计算机基础