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

Android开发:TabHost常用技巧

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

第二种情况:不使用 TabActivity

不使用 TabActivity 来 实现Tab 功能 ,情况要稍微复杂一些,但同时也更灵活一些,比如可以把 tab 的位置放在屏幕的下方。为此,需要对 TabHost 、 TabWidget 底层有所了解。在定义包含各 tab 信息的 xml 文件中,必须:

a.     根布局必须是 TabHost

b.     在 TabHost 内,必须包含一个垂直的 LinearLayout

c.     在 LinearLayout 中,必须包含一个 TabWidget 和一个 FrameLayout ,且其中 TabWidget 的 id 必须命名为 @Android:id/tabs , FrameLayout 的 id 必须命名为 @android:id/tabcontent ,各 tab 的内容可以定义在 FrameLayout 中。

 

1.     创建一个新的布局文件: tab_demo2.xml ,并使之如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< TabHost

         xmlns:android = "http://schemas.android.com/apk/res/android"

         android:layout_width = "fill_parent"

         android:layout_height = "fill_parent"

         android:id = "@+id/v_tabhost"

>  

        

         < LinearLayout

                  android:orientation = "vertical"

                  android:layout_width = "fill_parent"  

                android:layout_height = "fill_parent"

         >

                  < FrameLayout

                             android:id = "@android:id/tabcontent "

                           android:layout_width = "fill_parent"  

                 android:layout_height = "fill_parent"

                 android:layout_weight = "1"

                  >

                           < LinearLayout

                                     android:id = "@+id/tab1"

                                     android:orientation = "vertical"

                                     android:layout_width = "fill_parent"  

                                   android:layout_height = "fill_parent"

                           >

                                     < TextView

                                              android:layout_width = "wrap_content"  

                                            android:layout_height = "wrap_content"

                                            android:text = "Hello, Android1"

                                 android:textColor = "#FF0"

                                            android:textSize = "30px"

                                     >

                                     </ TextView >

                                     < Spinner

                                                android:id = "@+id/Spinner01"

                                                android:layout_width = "160px"

                                                android:layout_height = "wrap_content"

                                                android:entries = "@array/cities"

                                                android:prompt = "@string/spin_prompt"

                                       />      

                           </ LinearLayout >

                          

                           < LinearLayout

                                     android:id = "@+id/tab2"

                                     android:orientation = "vertical"

                                     android:layout_width = "fill_parent"  

                                   android:layout_height = "fill_parent"

                           >

                                     < TextView

                                              android:layout_width = "wrap_content"  

                                             android:layout_height = "wrap_content"

                                            android:text = "Hello, Android2"

                                            android:textColor = "#F0F"

                                            android:textSize = "30px"

                                    >

                                     </ TextView >

                                    

                                     < DigitalClock

                                                android:id = "@+id/digital_clock"

                                                android:layout_width = "wrap_content"

                                                android:layout_height = "wrap_content"

                                                android:textSize = "50px"

                                       />               

                           </ LinearLayout >                        

                          

                           < LinearLayout

                                     android:id = "@+id/tab3"

                                     android:orientation = "vertical"

                                     android:layout_width = "fill_parent"  

                                   android:layout_height = "fill_parent"

                           >

                                     < TextView

                                              android:layout_width = "wrap_content"  

                                            android:layout_height = "wrap_content"

                                            android:text = "Hello, Android3"

                                            android:textColor = "#0FF"

                                            android:textSize = "30px"

                                     >

                                     </ TextView >

                                     < AnalogClock

                                                android:id = "@+id/analog_clock"

                                                android:layout_width = "wrap_content"

                                                android:layout_height = "wrap_content"

                                       />

                           </ LinearLayout >                        

                  </ FrameLayout >

                    

        < TabWidget  

            android:id = "@android:id/tabs "

            android:layout_width = "fill_parent"  

            android:layout_height = "60px" />

         </ LinearLayout >

</ TabHost >

         注意上面代码中,粗体字部分的写法。

其中 android:layout_weight="1" 必须要出现在 FrameLayout 的属性中,否则 tab 不可见。

 

2.    在strings.xml中增加如下内容:

  < string name = "spin_prompt" > 请选择城市 </ string >

< string-array name = "cities" >

         < item > 北京 </ item >

         < item > 上海 </ item >

         < item > 南京 </ item >

         < item > 乌鲁木齐 </ item >

         < item > 哈尔滨 </ item >

         < item > 符拉迪沃斯托克 </ item >

</ string-array >


3.     修改 AndroidGUI24Activity 的代码如下:

package com.pat.gui;

import android.app.Activity;

import android.content.res.Resources;

import android.os.Bundle;

import android.widget.TabHost;

 

public class AndroidGUI24Activity extends Activity

{        

   private Resources res ;

    //@Override

    public void onCreate(Bundle savedInstanceState)

    {

        super .onCreate(savedInstanceState);

        setContentView(R.layout. tab_demo2 );

              // 下面两行对于不使用 TabActivity 实现 tab 效果是必须的

        TabHost tabhost = (TabHost)findViewById(R.id. v_tabhost );

        tabhost.setup();

       

        res = getResources();

       

        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 ));

    }

}

运行结果:

 

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

       

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