你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发:TabHost常用技巧

[日期:2011-09-17] 来源:Linux社区  作者:pathuang68 [字体:大 中 小]

6.     在上面的基础上,我们给每个 tab 增加一个图标。为此我们将 amplifer_1.png 、 basketball.png 和 cn.png 等 3 个图片文件拷贝到 res/drawable-mdpi 文件夹下,然后修改 Activity 的代码使之如下 ( 注意下面的粗体字部分未改动部分 ) :

package com.pat.gui;

 

import Android.app.TabActivity;

import android.content.res.Resources;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TabHost;

 

public class AndroidGUI24Activity extends TabActivity

implements

OnClickListener

{

private TabHost tabhost ;

private Resources res ;

private Button tab1btn1 ;

private Button tab2btn1 ;

private Button tab2btn2 ;

private Button tab3btn1 ;

private Button tab3btn2 ;

private Button tab3btn3 ;

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super .onCreate(savedInstanceState);

        // 获取该 Activity 用于容纳 tab 的 TabHost 对象

        // Returns the TabHost the activity is using to host its tabs.

        tabhost = getTabHost();

       

        res = getResources();

       

        // 获取 LayoutInflater 对象

        LayoutInflater inflater = LayoutInflater.from ( this );

       

        // TabHost.getTabContentView 方法的作用: Get the FrameLayout which holds tab content

        // LayoutInflater.inflate 方法的作用:

        // Inflate a new view hierarchy from the specified xml resource.

        // 其原型为: public View inflate (int resource, ViewGroup root)

        // 参数:

        // resource   ID for an XML layout resource to load (e.g., R.layout.main_page)

        // root            Optional view to be the parent of the generated hierarchy.

        // 返回值:

        // The root View of the inflated hierarchy. If root was supplied, this is the root

        // View; otherwise it is the root of the inflated XML file.

       

        // 下面这几句话,就是将 tab1.xml 、 tab2.xml 、 tab3.xml 的内容,全嵌入到 tabhost.getTabContentView()

        // 所返回的 FrameLayout 中。

        inflater.inflate(R.layout. tab1 , tabhost .getTabContentView());

        inflater.inflate(R.layout. tab2 , tabhost .getTabContentView());

        inflater.inflate(R.layout. tab3 , tabhost .getTabContentView());

       

        // 给 tabhost 增加 tab

        // newTabSpec(String tag) 返回一个 TabHost.TabSpec 对象, TabHost.TabSpec

        // setIndicator(" 国籍 "): 显示 tab 上的文字

        // setContent(int id) :指定 tab 的内容

        tabhost .addTab( tabhost .newTabSpec( "tab1" ).setIndicator( " 国籍 " , res .getDrawable(R.drawable. cn ) )

.setContent(R.id. tab1 ));

        tabhost .addTab( tabhost .newTabSpec( "tab2" ).setIndicator( " 爱好 " , res .getDrawable(R.drawable. basketball ) )

.setContent(R.id. tab2 ));

        tabhost .addTab( tabhost .newTabSpec( "tab3" ).setIndicator( " 系统 " , res .getDrawable(R.drawable. amplifer_1 ) )

.setContent(R.id. tab3 ));

       

        // 指定显示第几个 tab

        tabhost .setCurrentTab(1);

       

        // 在上面的工作都做完后,调用 setContentView

        //setContentView(R.layout.main);

        setContentView( tabhost );

      

        // 获取 6 个按钮对象,并分别给它们增加 OnClickListener

        tab1btn1 = (Button)findViewById(R.id. tab1btn1 );

        tab1btn1 .setOnClickListener( this );

       

        tab2btn1 = (Button)findViewById(R.id. tab2btn1 );

        tab2btn2 = (Button)findViewById(R.id. tab2btn2 );

        tab2btn1 .setOnClickListener( this );

        tab2btn2 .setOnClickListener( this );

       

        tab3btn1 = (Button)findViewById(R.id. tab3btn1 );

         tab3btn2 = (Button)findViewById(R.id. tab3btn2 );

        tab3btn3 = (Button)findViewById(R.id. tab3btn3 );

        tab3btn1 .setOnClickListener( this );

        tab3btn2 .setOnClickListener( this );

        tab3btn3 .setOnClickListener( this );

    }

 

//@Override

public void onClick(View v)

{

           switch (v.getId())

           {

           case R.id. tab1btn1 :

                    tabhost .setCurrentTab(1);          // 跳转到第二个 tab

                    break ;

           case R.id. tab2btn1 :

                    tabhost .setCurrentTab(0);          // 跳转到第一个 tab

                    break ;

           case R.id. tab2btn2 :

                    tabhost .setCurrentTab(2);          // 跳转到第三个 tab

                    break ;

           case R.id. tab3btn1 :

                    tabhost .setCurrentTab(0);          // 跳转到第一个 tab

                    break ;      

           case R.id. tab3btn2 :

                    tabhost .setCurrentTab(1);          // 跳转到第二个 tab

                    break ;

           case R.id. tab3btn3 :

                    tabhost .setCurrentTab(2);          // 跳转到第三个 tab( 自己 )

                    break ;

           }

}

}

运行结果为:


其它和 5 中的运行结果相同。

linux
相关资讯       Android开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款