本文是参考Android官方提供的sample里面的ApiDemos的学习总结。
 

TabActivity

  首先Android里面有个名为TabActivity来给我们方便使用。其中有以下可以关注的函数:
  public TabHost getTabHost ()  获得当前TabActivity的TabHost
  public TabWidget getTabWidget () 获得当前TabActivity的TabWidget
 
  public void setDefaultTab (String tag) 这两个函数很易懂,就是设置默认的Tab
  public void setDefaultTab (int index)  通过tab名——tag或者index(从0开始)
  
  protected void onRestoreInstanceState (Bundle state) 这两个函数的介绍可以
  protected void onSaveInstanceState (Bundle outState) 参考 Activity的生命周期
 

TabHost

  那么我们要用到的Tab载体是TabHost,需要从TabActivity.getTabHost获取。
  现在看看TabHost类,它有3个内嵌类:1个类TabHost.TabSpec,2个接口TabHost.TabContentFactory和TabHost.OnTabChangeListener。后面会介绍这些类和接口。
 
  TabHost类的一些函数:
  public void addTab (TabHost.TabSpec tabSpec) 添加tab,参数TabHost.TabSpec通过下面的函数返回得到
  public TabHost.TabSpec newTabSpec (String tag) 创建TabHost.TabSpec
  
  public void clearAllTabs () remove所有的Tabs
  public int getCurrentTab ()
  public String getCurrentTabTag ()
  public View getCurrentTabView ()
  public View getCurrentView ()
  public FrameLayout getTabContentView () 返回Tab content的FrameLayout
 
  public TabWidget getTabWidget ()
  public void setCurrentTab (int index)       设置当前的Tab by index
  public void setCurrentTabByTag (String tag) 设置当前的Tab by tag
  public void setOnTabChangedListener (TabHost.OnTabChangeListener l) 设置TabChanged事件的响应处理
  public void setup () 这个函数后面介绍
 

TabHost.TabSpec

  从上面的函数可以知道如何添加tab了,要注意,这里的Tag(标签),不是Tab按钮上的文字。
  而要设置tab的label和content,需要设置TabHost.TabSpec类。 引用SDK里面的话——“A tab has a tab indicator, content, and a tag that is used to keep track of it.”,TabHost.TabSpec就是管理这3个东西:
  public String getTag ()
  public TabHost.TabSpec setContent
  public TabHost.TabSpec setIndicator
 
  我理解这里的Indicator就是Tab上的label,它可以
  设置label: setIndicator (CharSequence label)
  或者同时设置label和iconsetIndicator (CharSequence label, Drawable icon)
  或者直接指定某个view: setIndicator (View view)
  
  对于Content,就是Tab里面的内容,可以
  设置View的id: setContent(int viewId)
  或者TabHost.TabContentFactory的createTabContent(String tag)来处理:setContent(TabHost.TabContentFactory contentFactory)
  或者用new Intent来引入其他Activity的内容:setContent(Intent intent)
  
  现在来看官方的Views/Tabs/Content By Id例子:

   代码
  1. public class Tabs1 extends TabActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. TabHost tabHost = getTabHost();
  6. LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
  7. tabHost.addTab(tabHost.newTabSpec("tab1")
  8. .setIndicator("tab1")
  9. .setContent(R.id.view1));
  10. tabHost.addTab(tabHost.newTabSpec("tab3")
  11. .setIndicator("tab2")
  12. .setContent(R.id.view2));
  13. tabHost.addTab(tabHost.newTabSpec("tab3")
  14. .setIndicator("tab3")
  15. .setContent(R.id.view3));
  16. }
  17. }
   原来在获取TabHost后,需要用LayoutInflater来得到Layout,LayoutInflater在后面就详细介绍。R.layout.tabs1的内容:
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent">
  4. <TextView android:id="@+id/view1"
  5. android:background="@drawable/blue"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. android:text="@string/tabs_1_tab_1"/>
  9. <TextView android:id="@+id/view2"
  10. android:background="@drawable/red"
  11. android:layout_width="fill_parent"
  12. android:layout_height="fill_parent"
  13. android:text="@string/tabs_1_tab_2"/>
  14. <TextView android:id="@+id/view3"
  15. android:background="@drawable/green"
  16. android:layout_width="fill_parent"
  17. android:layout_height="fill_parent"
  18. android:text="@string/tabs_1_tab_3"/>
  19. </FrameLayout>
  20. <! -- strings.xml
  21. <string name="tabs_1_tab_1">tab1</string>
  22. <string name="tabs_1_tab_2">tab2</string>
  23. <string name="tabs_1_tab_3">tab3</string>
  24. -->
  原来是用FrameLayout的!
  而让Tab1的内容显示tab1且背景为Blue,是setContent(R.id.view1)这里引用了TextView1。现在就基本明白如何添加tab以及如何设置label和content了。
 
  接下来看看Views/Tabs/Content By Factory的例子:

  代码
  1. public class Tabs2 extends TabActivity implements TabHost.TabContentFactory {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. final TabHost tabHost = getTabHost();
  6. tabHost.addTab(tabHost.newTabSpec("tab1")
  7. .setIndicator("tab1", getResources().getDrawable(R.drawable.star_big_on))
  8. .setContent(this));
  9. tabHost.addTab(tabHost.newTabSpec("tab2")
  10. .setIndicator("tab2")
  11. .setContent(this));
  12. tabHost.addTab(tabHost.newTabSpec("tab3")
  13. .setIndicator("tab3")
  14. .setContent(this));
  15. }
  16. public View createTabContent(String tag) {
  17. final TextView tv = new TextView(this);
  18. tv.setText("Content for tab with tag " + tag);
  19. return tv;
  20. }
  21. }
    可以看到通过override重写(重新实现)父类TabHost.TabContentFactory中的方法View createTabContent(String tag)来实现不同tab的不同content。同时在setContent的参数设置为相应的TabContentFactory。
   原来createTabContent是在每个tab第一次显示时才调用的,随后再次显示该tab就不会再次调用的,我自己用Logcat查看到的!这一点很关键,就是说在createTabContent是在tab没有完全创建前调用的,这意味在createTabContent里面是不能调用getCurrentTabView等之类的函数的,否则就出错!
 
   至于Views/Tabs/Content By Intent例子,就只是贴出代码,不给截图了:
  1. public class Tabs3 extends TabActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. final TabHost tabHost = getTabHost();
  6. tabHost.addTab(tabHost.newTabSpec("tab1")
  7. .setIndicator("list")
  8. .setContent(new Intent(this, List1.class)));
  9. tabHost.addTab(tabHost.newTabSpec("tab2")
  10. .setIndicator("photo list")
  11. .setContent(new Intent(this, List8.class)));
  12. // This tab sets the intent flag so that it is recreated each time
  13. // the tab is clicked.
  14. tabHost.addTab(tabHost.newTabSpec("tab3")
  15. .setIndicator("destroy")
  16. .setContent(new Intent(this, Controls2.class)
  17. .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
  18. }
  19. }
效果:Tab1的内容是List1的Activity,Tab2的是List8的Activity,Tab3的是controls2.Activity。
 

TabHost.OnTabChangeListener

   TabHost.OnTabChangeListener接口只有一个抽象方法onTabChanged(String tagString),明显地,在onTabChanged(String tagString)方法里面swtich..case..来判断tagString分别处理就行了。
 

TabHost.setup()

  在此贴出SDK doc里面的相关解释:
public void setup ()         Since: API Level 1
Call setup() before adding tabs if loading TabHost using findViewById(). However,You do not need to call setup() after getTabHost() in TabActivity. Example:
 
     mTabHost = (TabHost)findViewById(R.id.tabhost);
     mTabHost.setup();
     mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");
 
//我的理解是,如果要用到findViewById来获取TabHost,然后add tabs的话,需要在addTab前call setup();

public void setup (LocalActivityManager activityGroup)         Since: API Level 1
If you are using setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity. This is done for you if you extend TabActivity.
 
Parameters
activityGroup Used to launch activities for tab content.

本文出自 “学习Android” 博客,请务必保留此出处http://android.blog.51cto.com/268543/315208

转自:链接

Android UI学习 - Tab的学习和使用(转)的更多相关文章

  1. android UI卡顿问题学习

    转自https://blog.csdn.net/joye123/article/details/79425398 https://blog.csdn.net/zhenjie_chang/article ...

  2. Android UI 之 Tab类型界面总结

    Android 程序中实现Tab类型界面很常见,本人在做项目的时候也经常用到,所以想在这里总结一下,实现tab类型界面的几种方式,供大家参考.如有不对之处,欢迎大家指正! 一.TabActivity ...

  3. Android UI学习 - ListView (android.R.layout.simple_list_item_1是个什么东西)

    Android UI学习 - ListView -- :: 标签:Android UI 移动开发 ListView ListActivity 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...

  4. Android UI学习前言:Android UI系统的知识结构

    Android UI系统的知识结构如下图所示: 对于 一个GUI系统地使用,首先是由应用程序来控制屏幕上元素的外观和行为,这在各个GUI系统中是不相同的,但是也具有相通性.Android系统在这方面, ...

  5. 【转】Android之内存泄漏调试学习与总结

    大家有或经常碰到OOM的问题,对吧?很多这样的问题只要一出现相信大家的想法跟小马的一样,就是自己的应用:优化.优化.再优化!而且如果出现类似于OOM这样级别的问题,根本就不好处理,LogCat日志中显 ...

  6. (转).net开发者对android开发一周的学习体会

    春节期间,相对比较闲,上班时也没什么事情做.利用这一周的时间,简单的学习了一下移动方面的开发.主要是针对android,其实我对IOS更感兴趣 (因为我用iphone),苦于暂时没有苹果电脑,只能把它 ...

  7. Android开发技术周报183学习记录

    Android开发技术周报183学习记录 教程 Android性能优化来龙去脉总结 记录 一.性能问题常见 内存泄漏.频繁GC.耗电问题.OOM问题. 二.导致性能问题的原因 1.人为在ui线程中做了 ...

  8. 百度mobile UI组件GMU demo学习1-结构和初始化

    移动web现在已经是zepto的天下,但是一直找不到合适UI库,找了一段时间,终于找到了百度的ui库gum和inter 的 appframework UI库 相比之下,百度的UI库更接地气,配合百度强 ...

  9. Android中的SQLite使用学习

    Android中的SQLite使用学习 SQLite是非常流行的嵌入式关系型数据库,轻载, 速度快,而且是开源.在Android中,runtime提供SQLite,所以我们可以使用SQLite,而且是 ...

随机推荐

  1. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  2. 6.js模式-中介者模式

    1. 中介者模式 所有对象通过中介者进行通信 var playDirector = (function(){ var players = []; var options = {}; options.a ...

  3. Make My GitHub Pages

    https://git-scm.com/ https://pages.github.com/ 1.建立repository. 2.settings 3.选模板 4.Publish http://use ...

  4. 4.openstack之mitaka搭建glance镜像服务

    部署镜像服务 一:安装和配置服务 1.建库建用户 mysql -u root -p CREATE DATABASE glance; GRANT ALL PRIVILEGES ON glance.* T ...

  5. shell变量判空几种方法

    强烈声明:关于对数字的比较以及判断是否为空 最好在外层添加""引起来,这样可以避免空与其他字符比较时报错的问题. 1. 变量通过" "引号引起来 #!/bin/ ...

  6. Enum:Fliptile(POJ 3279)

    Fliptile 题目大意:农夫想要测牛的智商,于是他把牛带到一个黑白格子的地,专门来踩格子看他们能不能把格子踩称全白 这一题其实就是一个枚举题,只是我们只用枚举第一行就可以了,因为这一题有点像开关一 ...

  7. Divide and conquer:Matrix(POJ 3685)

    矩阵 题目大意:矩阵里面的元素按i*i + 100000 * i + j*j - 100000 * j + i*j填充(i是行,j是列),求最小的M个数 这一题要用到两次二分,实在是二分法的经典,主要 ...

  8. RSA 加解密转换

    由于项目的原因,原来的项目使用.net 进行开发,现在需要转成java, 所以原来的加解密就成了一个棘手的问题.由于数据使用RSA签名加密,又因为.net 和 Java 加解密算法上的差异,并不能使用 ...

  9. iOS 中CoreData的简单使用

    原文链接:http://www.jianshu.com/p/4411f507dd9f 介绍:本文介绍的CoreData不在AppDelegate中创建,在程序中新建工程使用,即创建本地数据库,缓存数据 ...

  10. py随笔

    while true,无限循环 str.isdigit判断是不是数字 +只能在两个两个相同的类型之间执行 iter(i)将i加入迭代器