本文是参考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. ThreadPool线程池 小结

    ThreadPool类提供一个线程池,该线程池可用于发送工作项.处理异步 I/O.代表其他线程等待以及处理计时器 线程池通过为应用程序提供一个由系统管理的辅助线程池使您可以更为有效地使用线程.一个线程 ...

  2. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  3. Unity3d 枚举某个目录下所有资源

    using UnityEngine; using System.Collections; using UnityEditor; using System.Collections.Generic; us ...

  4. ios 在程序中使用iCloud

    注意,这里说的使用icould不是用icloud进行系统备份,那个功能不需要我们写代码,备份到icloud的东西我们也不能操作.我们指的是以下这3种icloud使用方法: 这里有3中使用方法, Key ...

  5. ios NSNotificationCenter 收到通知后的执行线程

    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Articles/Thread ...

  6. STL vector按多字段值排序

    #include <iostream> #include <vector> #include <string> #include <algorithm> ...

  7. ACM/ICPC 之 模拟 (HNUOJ 13391-换瓶模拟)

    题意:汽水瓶有三个部分cap+plastic bottle+ label(瓶盖-瓶身-瓶底),给出数据:n为原瓶数,x,y,z为这三个部分可以用相应的数字换取新瓶子,求最大总瓶数. 模拟(暴力) // ...

  8. monitor disk

    #!/bin/bash # #top #Big_USERS - find big disk space users in various directories ################### ...

  9. 【python】入门学习(三)

    for循环 for i in range():   #注意冒号 range中默认从0开始 或者从指定的数字开始 到给定数字的前一个数字结束 递增递减皆是如此 for循环提供变量的自动初始化 for i ...

  10. osg四元数设置roll pitch heading角度

    roll绕Y轴旋转 pitch绕X轴旋转 heading绕Z轴旋转 单位是弧度,可以使用osg::inDegrees(45)将45角度转换为弧度 定义一个四元数 osg::Quat q( roll,o ...