• android的TabActivity


    转自:http://www.cnblogs.com/keyindex/articles/1815074.html

    随笔- 39  文章- 2  评论- 113 

    android的TabActivity

     

    前言

      这段时间在研究android平台上的开源项目——StandupTimer,这是由jwood所设计的一个较为简单android应用,用于控制会议时间,类似秒表倒计时。

    TabActivity & TabHost

      tabActivity继承自Activity,其内部定义好了TabHost,可以通过getTabHost()获取。TabHost 包含了两种子元素:一些可以自由选择的Tab 和tab对应的内容tabContentto,在Layout的<TabHost>下它们分别对应 TabWidget和FrameLayout。
      使用TabActivity可以让同一个界面容纳更多的内容。我们将按照Standup Timer里的TeamDetailsActivity来讲述TabActivity的使用。在该例中,包含了两个Tab一个用于展示team的统计信 息,一个用于展示team所参加的会议的列表(这是一个ListView)。

    创建Layout 

      这里需要注意的是不管你是使用TabActivity 还是自定义TabHost,都要求以TabHost作为XML布局文件的根。
    复制代码
    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id
    ="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height
    ="fill_parent">
    <LinearLayout android:orientation="vertical"
    android:layout_width
    ="fill_parent" android:layout_height="fill_parent">
    <TabWidget android:id="@android:id/tabs"
    android:layout_width
    ="fill_parent" android:layout_height="wrap_content"/>
    <FrameLayout android:id="@android:id/tabcontent"
    android:layout_width
    ="fill_parent" android:layout_height="fill_parent">


    <!--省略部分代码-->

    <TextView android:id="@+id/no_team_meetings"
    android:textSize
    ="18sp" android:layout_width="fill_parent"
    android:layout_height
    ="fill_parent"/>

    <TextView android:id="@+id/no_team_meeting_stats"
    android:textSize
    ="18sp" android:layout_width="fill_parent"
    android:layout_height
    ="fill_parent"/>
    </FrameLayout>
    </LinearLayout>
    </TabHost>
    复制代码
    复制代码
    通常我们采用线性布局所以<TabHost> 的子元素是 <LinearLayout>。<TabWidger>对应Tab。<FrameLayout>则用于包含Tab需 要展示的内容。需要注意的是<TabWidger> 和<FrameLayout>的Id 必须使用系统id,分别为android:id/tabs 和 android:id/tabcontent 。因为系统会使用者两个id来初始化TabHost的两个实例变量(mTabWidget 和 mTabContent)。

    编写Java代码

      我们可以采用两种方法编写标签页:一种是继承TabActivity ,然后使用getTabHost()获取TabHost对象;第二种方法是使用自定的TabHost在布局文件上<TabHost>的自定义 其ID,然后通过findViewById(),方法获得TabHost对象。
      本文中采用继承TabActivity的方法。
    复制代码
    复制代码
    privatevoid createTabs() {
    TabHost tabhost
    =getTabHost();
    tabhost.addTab(tabhost.newTabSpec(
    "stats_tab").
    setIndicator(
    this.getString(R.string.stats)).
    setContent(createMeetingDetails(team)));

    tabhost.addTab(tabhost.newTabSpec(
    "meetings_tab").
    setIndicator(
    this.getString(R.string.meetings)).
    setContent(createMeetingList()));
    getTabHost().setCurrentTab(
    0);
    }
    复制代码
    复制代码
    Java代码中我们首先需要做的是获取TabHost对象,可以通过TabActivtiy里的getTabHsot()方法。如果是自定义TabHost,在添加Tabs前 应该调用 setUp()方法。
    mTabHost = (TabHost)findViewById(R.id.tabhost);
    mTabHost.setup();
    mTabHost.addTab(TAB_TAG_1,
    "Hello, world!", "Tab 1");
    SDK上的原文:
      Call setup() before adding tabs if loading TabHost using findViewById(). However You do not need to call setup() after getTabHost() in TabActivity

      接着向TabHost添加tabs.即调用tabHost.addTab(TabSpec) 方法。TabSpec主要包含了setIndicator 和 setContent 方法,通过这两个方法来指定Tab 和 TanContent。
      TabSpec 通过 .newTabSpec(String tag)来创建实例。实例化后对其属性进行设置 。setIndicator()设置tab,它有3个重载的函数
    •  public TabHost.TabSpec setIndicatior(CharSwquence label,Drawable icon).指定tab的标题和图标。
    • public TabHost.TabSpec (View view)通过View来自定义tab
    • public TabHost.TabSpec(CharSequence label) 指定tab的标题,此时无图标。
       setContent 指定tab的展示内容,它也有3种重载
    • public TabHost.TabSpec setContent(TabHost.TabContentFactory )
    • public TabHost.TabSpec setContent(int ViewId)
    • public TabHost.TabSpec setContent(Intent intent)
      后两种方法比较后理解一个是通过 ViewId指定显示的内容,如.setContent(R.id.Team_EditText)。第三种则是直接通过Intent加载一个新的 Activity页。如.setContent(new Intent(this, MeetingActivity.class)));
      本例中是通过TabContentFactory 来指定对应的TabContent。TabContentFactory 是一个接口,其只包含了 一个返回 View 的createTabContent(String tag)方法。
    复制代码
    复制代码
    private TabContentFactory createMeetingDetails(Team team2) {

    returnnew TabHost.TabContentFactory() {

    @Override
    public View createTabContent(String tag) {
              //设置View
    setStatsTabContent();
    return findViewById(R.id.teamStats);
    }
    };
    }

    private TabHost.TabContentFactory createMeetingList()
    {
    returnnew TabHost.TabContentFactory() {

    @Override
    public View createTabContent(String tag) {
          
    meetingListAdapter
    = createMeetingListAdapter();
    meetingList.setAdapter(meetingListAdapter);
    return meetingList;
    }
    };
    }
    复制代码
    复制代码
     
    事先声明好的
    private ListView meetingList=null;
    private ArrayAdapter<String> meetingListAdapter =null;
    我们也可以让TabActivity去实现TabContentFactory 接口
    publicclass Tabs2 extends TabActivity implements TabHost.TabContentFactory
    然后在TabActiviy类中实现createTabContent方法
    @Override
    public View createTabContent(String tag) {
    final TextView tv =new TextView(this);
    tv.setText(
    "Content for tab with tag "+ tag);
    return tv;
    }
    setStatsTabContent();方法
    setStatsTabContent
      最后将TabSpec 添加到 TabHost上,即tabHost.addTab(tabSpec)。我们发现TabSpec 的setIndicator 和 setContent 方法返回的都是 TabSpec 自身所以可以使用窜的方式编写代码:
    tabhost.addTab(tabhost.newTabSpec("stats_tab")
    .setIndicator(
    this.getString(R.string.stats))
    .setContent(createMeetingDetails(team)));

    其他参考资料

    SDK:TabHost.TabSpec

    SDK:TabHost.TabContentFactory

    SDK:TabHost

    SDK:Tab Layout

    android Tabhost部件

    apiDemo也有3个例子可以参考。

    系列索引

      
    Android 开源项目-StandupTimer学习笔记索引  
     
     
     
    转自:http://yangguangfu.iteye.com/blog/679001

    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)
     
     
    主程序代码
    Acitvit里面的代码代码 复制代码 收藏代码
    1. package com.yang.tabletest;   
    2.   
    3. import android.app.TabActivity;   
    4. import android.os.Bundle;   
    5. import android.view.LayoutInflater;   
    6. import android.widget.TabHost;   
    7.   
    8. public class TableTestAcitivity extends TabActivity{   
    9.     /** Called when the activity is first created. */   
    10.     @Override   
    11.     public void onCreate(Bundle savedInstanceState) {   
    12.         super.onCreate(savedInstanceState);   
    13.         //setContentView(R.layout.main);   
    14.            
    15.         //获得当前TabActivity的TabHost   
    16.        TabHost tabHost = getTabHost();    
    17.                     
    18.                LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);    
    19.            
    20.                 tabHost.addTab(tabHost.newTabSpec("tab1")    
    21.                          .setIndicator("主页")   
    22.                          .setContent(R.id.view1));   
    23.                             
    24.                    
    25.                  tabHost.addTab(tabHost.newTabSpec("tab2")    
    26.                          .setIndicator("标题")    
    27.                          .setContent(R.id.view2));    
    28.                  tabHost.addTab(tabHost.newTabSpec("tab3")    
    29.                          .setIndicator("简介")    
    30.                          .setContent(R.id.view3));   
    31.                  tabHost.addTab(tabHost.newTabSpec("tab4")    
    32.                          .setIndicator("关于")    
    33.                          .setContent(R.id.view4));   
    34.     }   
    35.   
    36.        
    37. }   
     
     tabls.xml里面的代码
    Tabi.xml代码 复制代码 收藏代码
    1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    2.     android:layout_width="fill_parent"    
    3.     android:layout_height="fill_parent">    
    4.     
    5.     <TextView android:id="@+id/view1"    
    6.         android:background="@drawable/blue"    
    7.         android:layout_width="fill_parent"    
    8.         android:layout_height="fill_parent"    
    9.         android:text="@string/tabs_1_tab_1"/>    
    10.     
    11.     <TextView android:id="@+id/view2"    
    12.         android:background="@drawable/red"    
    13.         android:layout_width="fill_parent"    
    14.         android:layout_height="fill_parent"    
    15.         android:text="@string/tabs_1_tab_2"/>    
    16.     
    17.     <TextView android:id="@+id/view3"    
    18.         android:background="@drawable/green"    
    19.         android:layout_width="fill_parent"    
    20.         android:layout_height="fill_parent"    
    21.         android:text="@string/tabs_1_tab_3"/>    
    22.            
    23.     <TextView android:id="@+id/view4"    
    24.       android:background="@drawable/green"    
    25.       android:layout_width="fill_parent"    
    26.       android:layout_height="fill_parent"    
    27.       android:text="@string/tabs_1_tab_4"/>    
    28.   
    29. </FrameLayout>     
    string.xml的代码
    Java代码 复制代码 收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>   
    2. <resources>   
    3.     <string name="hello">Hello World, TableTestAcitivity!</string>   
    4.     <string name="app_name">阿福学习</string>   
    5.     <string name="tabs_1_tab_1">主页</string>   
    6.     <string name="tabs_1_tab_2">标题</string>   
    7.     <string name="tabs_1_tab_3">关于</string>   
    8.     <string name="tabs_1_tab_4">返回</string>   
    9. </resources>  
     color.xml代码
    Java代码 复制代码 收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>   
    2. <resources>   
    3.   <drawable name="darkgray">#404040ff</drawable>   
    4.      
    5.   <drawable name="red">#ff00ff</drawable>   
    6.   <drawable name="green">#0ff0ff</drawable>   
    7.   <drawable name="lightgray">#c0c0c0ff</drawable>   
    8.      
    9.   <drawable name="yellow">#ffFF33ff</drawable>   
    10.   <drawable name="blue">#00ffff</drawable>   
    11.   <drawable name="gray">#808080ff</drawable>   
    12.   <drawable name="magenta">#ff6699ff</drawable>   
    13.   <drawable name="cyan">#66ffffff</drawable>   
    14.   <drawable name="black">#000000</drawable>    
    15.   <drawable name="white">#FFFFFF</drawable>    
    16. </resources>  
      第二个例子的Activity代码
    Java代码 复制代码 收藏代码
    1. package com.yang.tabletest;   
    2.   
    3. import android.app.TabActivity;   
    4. import android.os.Bundle;   
    5. import android.view.View;   
    6. import android.widget.TabHost;   
    7. import android.widget.TextView;   
    8.   
    9. public class TableTestAcitivity extends TabActivity implements TabHost.TabContentFactory{   
    10.     /** Called when the activity is first created. */  
    11.     @Override  
    12.     public void onCreate(Bundle savedInstanceState) {   
    13.          super.onCreate(savedInstanceState);    
    14.              
    15.                  final TabHost tabHost = getTabHost();    
    16.                  tabHost.addTab(tabHost.newTabSpec("tab1")    
    17.                          .setIndicator("主页", getResources().getDrawable(R.drawable.test))    
    18.                          .setContent(this));    
    19.                  tabHost.addTab(tabHost.newTabSpec("tab2")    
    20.                          .setIndicator("标题",getResources().getDrawable(R.drawable.test))    
    21.                          .setContent(this));    
    22.                  tabHost.addTab(tabHost.newTabSpec("tab3")    
    23.                          .setIndicator("关于",getResources().getDrawable(R.drawable.test))    
    24.                          .setContent(this));    
    25.          }   
    26.   
    27.     @Override  
    28.     public View createTabContent(String arg0) {   
    29.           final TextView tv = new TextView(this);    
    30.                  tv.setText("Content for tab with tag " + arg0);    
    31.                     
    32.                  return tv;   
    33.     }    
    34.              
    35.              
    36.   
    37.        
    38. }  
  • 相关阅读:
    自定义View
    Android Parcelable
    java IO
    如何安全退出已调用多个Activity的Application?
    cookie和session
    Excel 使用AutoFill提示“类Range的AutoFill方法无效”
    解决“配置系统未能初始化”问题
    Android控件第7类——对话框
    Android控件第6类——杂项控件
    Android控件第5类——ViewAnimator
  • 原文地址:https://www.cnblogs.com/yuxiang204/p/2964062.html
Copyright © 2020-2023  润新知