TabHost    命名空间:        android.widget.TabHost    初始化函数(必须在addTab之前调用):        setup();     包含两个子元素:        1.Tab标签容器TabWidget(@android:id/tabs)        2.Tab内容容器FrameLayout(@android:id/tabcontent)代码如下
   

TabHostActivity.java

public class TabHostActivity extends TabActivity {

    private TabHost tabHost;

    private Intent certificateIntent;

    private Intent feeIntent;

    private Intent scoreIntent;

    private Intent studyIntent;

    private Intent moreIntent;

 

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       tabHost = getTabHost();

       initIntent();

       addSpec();

    }

    /**

     * 初始化各个tab标签对应的intent

     */

    privatevoid initIntent() {

       studyIntent = new Intent(this, StudyActivity.class);

       scoreIntent = new Intent(this, ScoreActivity.class);

       feeIntent = new Intent(this, FeeActivity.class);

       certificateIntent = new Intent(this, CertificateActivity.class);

       moreIntent = new Intent(this, MoreActivity.class);

    }

    /**

     * tabHost添加各个标签项

     */

    privatevoid addSpec() {

      tabHost.addTab(this.buildTagSpec("tab_study",

R.string.study_progress,R.drawable.account01studyIntent));

    tabHost.addTab(this.buildTagSpec("tab_score",

R.string.test_score,R.drawable.account02scoreIntent));

        tabHost.addTab(this.buildTagSpec("tab_fee",

R.string.fee_pay,R.drawable.account03feeIntent));

       tabHost.addTab(this.buildTagSpec("tab_certificate", R.string.certificate_grant,R.drawable.accountcertificateIntent));

       tabHost.addTab(this.buildTagSpec("tab_more", R.string.more,

              R.drawable.account05moreIntent));

    }

    /**

     * 自定义创建标签项的方法

     * @param tagName 标签标识

     * @param tagLable 标签文字

     * @param icon  标签图标

     * @param content 标签对应的内容

     * @return

     */

    private TabHost.TabSpec buildTagSpec(String tagName, int tagLable,

           int icon, Intent content) {

       returntabHost

              .newTabSpec(tagName)

              .setIndicator(getResources().getString(tagLable),

                     getResources().getDrawable(icon)).setContent(content);

    }}

FragmentTabHost 命名空间: android.support.v4.app.FragmentTabHost android.support.v13.app.FragmentTabHost 初始化函数(必须在addTab之前调用): setup(this, getSupportFragmentManager(), R.id.realtabcontent); 包含三个子元素: 1.Tab标签 容器TabWidget(@android:id/tabs) 2.Tab内容 容器FrameLayout [宽高皆为0dp](@android:id/tabcontent) 3.Tab内容 容器FrameLayout [真正的容器](@+id/RealTabContent)

经验 - 实现Tab功能的几种方式        1.(废弃)继承自TabActivity + TabHost 布局 + Activity 内容                1.1:TabActivity为ActivityGroup[在版本13里被放弃]子类                1.2:使用getTabHost() 获得TabHost 对象                1.3:使用 newTabSpec(...).setContent(Intent) 添加Tab标签与内容        2.(废弃)继承自ActivityGroup + TabHost布局 + Activity 内容                2.1:ActivityGroup在版本13里被放弃                2.2:使用findViewById 获得TabHost 对象                2.3:使用setup(new LocalActivityManager(this, true))初始化                2.4:使用 newTabSpec(...).setContent(Intent) 添加Tab标签与内容        3.(简单)继承自FragmentActivity + FragmentTabHost 布局 + Fragment 内容                3.1:使用findViewById 获得FragmentTabHost 对象                3.2:使用setup(this, getSupportFragmentManager(), R.id.realtabcontent)初始化                3.3:使用addTab(newTabSpec().setIndicator(),Fragment.class, null) 添加Tab标签与内容        4.(复杂)继承自FragmentActivity + TabHost 布局 + ViewPager布局 + Fragment 内容                4.1:使用findViewById 获得TabHost 对象                4.2:使用setup()初始化                4.3:使用TabHost.addTab(tabSpec.setContent(DummyTabFactory));添加Tab标签与空内容                4.4:在TabHost.onTabChanged中控制ViewPager的实际显示                4.5:滑动时在ViewPager.onPageSelected 中控制Tab标签的选择。                PS:当Tab标签数量固定且不追求和系统保持一致的标签效果,的情况下这里的TabHost布局有些多余。                如 :特有应用需要的Tab标签固定,且有自己的一套显示效果,那么这里完全无需TabHost布局。        5.(DIY)继承自FragmentActivity + 自定义布局 + ViewPager布局 + Fragment 内容                5.1:自定义布局中嵌入固定Tab标签元素                5.2:在标签元素点击事件中控制ViewPager的实际显示                5.3:滑动时在ViewPager.onPageSelected 中控制标签元素的选择。
自定义tabhost的代码
   
public class TabHoastDemo extends FragmentActivity{    //定义FragmentTabHost对象    private FragmentTabHost mTabHost;    //定义一个布局    private LayoutInflater layoutInflater;    //定义数组来存放Fragment界面    private Class fragmentArray[] = {fragment1.class,fragment2.class,fragment3.class,fragment5.class};    //定义数组来存放按钮图片    private int mImageViewArray[] = {R.drawable.vi_2s,R.drawable.vi_3s,R.drawable.vi_4s,          R.drawable.vi_5s};    //Tab选项卡的文字    private String mTextviewArray[] = {"首页", "消息", "好友", "广场"};    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_tab_hoast_demo);        initView();    }    /**     * 初始化组件     */    private void initView(){        //实例化布局对象        layoutInflater = LayoutInflater.from(this);        //实例化TabHost对象,得到TabHost        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);        //得到fragment的个数        int count = fragmentArray.length;        for(int i = 0; i < count; i++){            //为每一个Tab按钮设置图标、文字和内容            TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));            //将Tab按钮添加进Tab选项卡中            mTabHost.addTab(tabSpec, fragmentArray[i], null);            //设置Tab按钮的背景            mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.seletors);        }    }    /**     * 给Tab按钮设置图标和文字     */    private View getTabItemView(int index){        View view = layoutInflater.inflate(R.layout.activity_tab_items, null);        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);        imageView.setImageResource(mImageViewArray[index]);        TextView textView = (TextView) view.findViewById(R.id.textview);        textView.setText(mTextviewArray[index]);        return view;    }


更多相关文章

  1. android Log.isLoggable步骤的使用
  2. Android(安卓)Camera使用小结
  3. android API8以上版本使用GridLayout
  4. 使用反射调用android API中的hide方法
  5. android Log.isLoggable步骤的使用
  6. Android(安卓)中使用自定义字体的方法
  7. 总结EditText,TextView使用的点点滴滴
  8. android databinding的使用技巧
  9. Android(安卓)Notification的使用

随机推荐

  1. 如何编译运行Android系统自带桌面Launche
  2. Android:将AVD文件搬家(修改AVD路径)
  3. [导入]2010-03-06 传智播客—Android(六)通
  4. Android网络相关
  5. AIDE——运行于Android上的Android Java
  6. Android Studio 修改常用设置
  7. Android(安卓)事件分发
  8. Android Developers: 创建一个Android项
  9. Android(安卓)开发之拦截EditText的输入
  10. android菜单详解一:概述