AndroidGUI24:TabHost常用技巧

系统 1671 0

<!-- [if !mso]> <mce:style><!-- v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} w/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} --> <!-- [endif]----><!-- [if gte mso 9]><xml> <o:OfficeDocumentSettings> <o:RelyOnVML/> <o:AllowPNG/> </o:OfficeDocumentSettings> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves>false</w:TrackMoves> <w:TrackFormatting/> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>ZH-CN</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:SplitPgBreakAndParaMark/> <w:DontVertAlignCellWithSp/> <w:DontBreakConstrainedForcedTables/> <w:DontVertAlignInTxbx/> <w:Word11KerningPairs/> <w:CachedColBalance/> <w:UseFELayout/> </w:Compatibility> <m:mathPr> <m:mathFont m:val="Cambria Math"/> <m:brkBin m:val="before"/> <m:brkBinSub m:val="--"/> <m:smallFrac m:val="off"/> <m:dispDef/> <m:lMargin m:val="0"/> <m:rMargin m:val="0"/> <m:defJc m:val="centerGroup"/> <m:wrapIndent m:val="1440"/> <m:intLim m:val="subSup"/> <m:naryLim m:val="undOvr"/> </m:mathPr></w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true" DefSemiHidden="true" DefQFormat="false" DefPriority="99" LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Normal"/> <w:LsdException Locked="false" Priority="9" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="heading 1"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/> <w:LsdException Locked="false" Priority="39" Name="toc 1"/> <w:LsdException Locked="false" Priority="39" Name="toc 2"/> <w:LsdException Locked="false" Priority="39" Name="toc 3"/> <w:LsdException Locked="false" Priority="39" Name="toc 4"/> <w:LsdException Locked="false" Priority="39" Name="toc 5"/> <w:LsdException Locked="false" Priority="39" Name="toc 6"/> <w:LsdException Locked="false" Priority="39" Name="toc 7"/> <w:LsdException Locked="false" Priority="39" Name="toc 8"/> <w:LsdException Locked="false" Priority="39" Name="toc 9"/> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/> <w:LsdException Locked="false" Priority="10" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Title"/> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/> <w:LsdException Locked="false" Priority="11" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/> <w:LsdException Locked="false" Priority="22" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Strong"/> <w:LsdException Locked="false" Priority="20" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/> <w:LsdException Locked="false" Priority="59" SemiHidden="false" UnhideWhenUsed="false" Name="Table Grid"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/> <w:LsdException Locked="false" Priority="1" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 1"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 1"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 1"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/> <w:LsdException Locked="false" Priority="34" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/> <w:LsdException Locked="false" Priority="29" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Quote"/> <w:LsdException Locked="false" Priority="30" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 1"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 1"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 2"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 2"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 2"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 2"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 2"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 3"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 3"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 3"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 3"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 3"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 4"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 4"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 4"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 4"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 4"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 5"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 5"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 5"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 5"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 5"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 6"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 6"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 6"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 6"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 6"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/> <w:LsdException Locked="false" Priority="19" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/> <w:LsdException Locked="false" Priority="21" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/> <w:LsdException Locked="false" Priority="31" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/> <w:LsdException Locked="false" Priority="32" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/> <w:LsdException Locked="false" Priority="33" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Book Title"/> <w:LsdException Locked="false" Priority="37" Name="Bibliography"/> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} --> <!-- [endif]---->

在很多其他语言进行界面编程的时候,都有 Tab 这样的控件,在 Android 编程环境下也不例外。 TabHost 由一个 TabSpecs 和一个嵌套的 TabHost 组成,该嵌套的 TabHost 包含 tab 的标题以及 tab 的内容。一个 tab 的内容,可以是一个预先定义好的 View ,或者是通过 Intent 对象启动的 Activity ,或者是利用 TabContentFactory 所创建出来的 View

Tab 并没有看起来那么复杂。每个 tab 实际上就是一个 View 的容器。

有两种方式可以实现 tab 。一种是直接使用 TabActivity ,一种是不使用 TabActivity 。我们首先来看看使用 TabActivity 实现 tab 的情况。

第一种情况:使用 TabActivity

1. 创建一个 Android Project

2. 新建一个 xml 文件: tab_demo.xml ,使其内容如下:

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

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

android:orientation = "vertical"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

>

< TextView android:id = "@+id/tab_demo_tv1"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:text = "tab_demo_tv1"

/>

< LinearLayout android:id = "@+id/tab_linearlayout2"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:orientation = "vertical"

>

< TextView android:id = "@+id/tab_demo_tv2"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = "tab_demo_tv2"

/>

< Button

android:layout_width = "fill_parent"

android:layout_height = "wrap_content"

android:text = "Tab demo Button"

/>

</ LinearLayout >

< TextView android:id = "@+id/tab_demo_tv3"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:text = "tab_demo_tv3"

/>

</ FrameLayout >

上面的第一和第三个 TextView ,也可以是 LinearLayout(tab_linearlayout2)

3. 修改 Activity 所对应的代码,使之如下:

package com.pat.gui;

import android.app.TabActivity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.widget.TabHost;

public class AndroidGUI24Activity extends TabActivity

{

private TabHost tabhost ;

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

// 获取 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.

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

// 上面这句话,就是将 tab_demo.xml 的内容,嵌入到 tabhost.getTabContentView() 所返回的 FrameLayout

// tabhost 增加 tab

// newTabSpec(String tag) 返回一个 TabHost.TabSpec 对象,其参数用于标识一个 tab tag ,为 String 类型

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

// setContent(R.id.tab_demo_tv1) :指定 tab 的内容,必须为 id ,比如空间的 id 或者 layout id

tabhost .addTab( tabhost .newTabSpec( "tab1" ).setIndicator( " 国籍 " ).setContent(R.id. tab_demo_tv1 ));

tabhost .addTab( tabhost .newTabSpec( "tab2" ).setIndicator( " 爱好 " ).setContent(R.id. tab_linearlayout2 ));

tabhost .addTab( tabhost .newTabSpec( "tab3" ).setIndicator( " 职业 " ).setContent(R.id. tab_demo_tv3 ));

// 指定显示第几个 tab

tabhost .setCurrentTab(1);

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

//setContentView(R.layout.main);

setContentView( tabhost );

}

}

运行结果:

AndroidGUI24:TabHost常用技巧

可以看到是第 2 tab 出现在屏幕上。点击“国籍”和“职业”两个 tab 会看到与之分别对应的界面。

4. 在前面的代码中,我们将 3 tab 需要显示的内容均在 tab_demo.xml 这一个文件中规定了。事实上,我们还可以有另外一种做法,那就是让不同的 tab 分别对应不同的 xml 布局文件,为此,新建三个布局文件,并使之如下:

tab1.xml:

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

< LinearLayout

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

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:id = "@+id/tab1"

android:orientation = "vertical" >

< TextView

android:id = "@+id/tab1tv1"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = "Tab1"

/>

< Button

android:id = "@+id/tab1btn1"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = " 按钮 1"

/>

</ LinearLayout >

tab2.xml:

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

< LinearLayout

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

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:id = "@+id/tab2"

android:orientation = "vertical" >

< TextView

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = "Tab2"

/>

< Button

android:id = "@+id/tab2btn1"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = " 按钮 1"

/>

< Button

android:id = "@+id/tab2btn2"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = " 按钮 2"

/>

</ LinearLayout >

tab3.xml:

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

< LinearLayout

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

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:id = "@+id/tab3"

android:orientation = "vertical" >

< TextView

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = "Tab3"

/>

< Button

android:id = "@+id/tab3btn1"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = " 按钮 1"

/>

< Button

android:id = "@+id/tab3btn2"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = " 按钮 2"

/>

< Button

android:id = "@+id/tab3btn3"

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = " 按钮 3"

/>

</ LinearLayout >

5. 对应地修改 Activity 的代码,使之如下:

package com.pat.gui;

import android.app.TabActivity;

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

// 获取 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(R.id.tab_demo_tv1) :指定 tab 的内容

tabhost .addTab( tabhost .newTabSpec( "tab1" ).setIndicator( " 国籍 " ).setContent(R.id. tab1 ));

tabhost .addTab( tabhost .newTabSpec( "tab2" ).setIndicator( " 爱好 " ).setContent(R.id. tab2 ));

tabhost .addTab( tabhost .newTabSpec( "tab3" ).setIndicator( " 系统 " ).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 ;

}

}

}

运行结果如下:

AndroidGUI24:TabHost常用技巧

可以发现,点击“按钮 1 ”会跳转到“国籍”,点击“按钮 2 ”会跳转到“系统”

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 ;

}

}

}

运行结果为:

AndroidGUI24:TabHost常用技巧

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

7. 在本文开始出,曾经提到“或者是通过 Intent 对象启动的 Activity ”,也就是说 setContent 的参数可以是一个 Intent 对象,然后用该对象启动另外一个 Activity 。为此我们先创建一个 Activity 类,并为它指定相应的 layout 。假定我们让第三个 tab 显示的内容为 Intent 对象所指定的 Activity Layout 内容如下:

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

< LinearLayout

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

android:layout_width = "wrap_content"

android:layout_height = "wrap_content" >

< TextView

android:layout_width = "wrap_content"

android:layout_height = "wrap_content"

android:text = "Hello, Android"

android:textSize = "30px"

android:textColor = "#FF0" />

</ LinearLayout >

对应的 Activity 代码如下:

package com.pat.gui;

import android.app.Activity;

import android.os.Bundle;

public class ThirdTab extends Activity

{

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.thirdtab);

}

}

AndroidManifest.xml 中的 Application 标签内,增加对 ThirdTab 的描述:

<activity android:name= ".ThirdTab" />

8. 现在,咱们来修改 AndroidGUI24Activity 的代码,使之如下 ( 注意粗体字部分的代码变化 )

package com.pat.gui;

import android.app.TabActivity;

import android.content.Intent;

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;

@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 的内容,全嵌入到 tabhost.getTabContentView()

// 所返回的 FrameLayout 中。这次我们仅 inflate 两个 tab 的内容,第三个 tab 将有一个 Intent 对象指定

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

inflater.inflate(R.layout. tab2 , 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( new Intent( this , ThirdTab. class ) ));

// 指定显示第几个 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 );

}

//@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 ;

}

}

}

运行结果如下 ( 点击“系统”这个 tab)

AndroidGUI24:TabHost常用技巧

点击“国籍”、“爱好”和“系统”,各 tab 之间可以进行很好的切换。

第二种情况:不使用 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中增加如下内容:

<!-- [if gte mso 9]><xml> <o:OfficeDocumentSettings> <o:RelyOnVML/> <o:AllowPNG/> </o:OfficeDocumentSettings> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves/> <w:TrackFormatting/> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>ZH-CN</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:SplitPgBreakAndParaMark/> <w:DontVertAlignCellWithSp/> <w:DontBreakConstrainedForcedTables/> <w:DontVertAlignInTxbx/> <w:Word11KerningPairs/> <w:CachedColBalance/> <w:UseFELayout/> </w:Compatibility> <m:mathPr> <m:mathFont m:val="Cambria Math"/> <m:brkBin m:val="before"/> <m:brkBinSub m:val="--"/> <m:smallFrac m:val="off"/> <m:dispDef/> <m:lMargin m:val="0"/> <m:rMargin m:val="0"/> <m:defJc m:val="centerGroup"/> <m:wrapIndent m:val="1440"/> <m:intLim m:val="subSup"/> <m:naryLim m:val="undOvr"/> </m:mathPr></w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true" DefSemiHidden="true" DefQFormat="false" DefPriority="99" LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Normal"/> <w:LsdException Locked="false" Priority="9" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="heading 1"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/> <w:LsdException Locked="false" Priority="39" Name="toc 1"/> <w:LsdException Locked="false" Priority="39" Name="toc 2"/> <w:LsdException Locked="false" Priority="39" Name="toc 3"/> <w:LsdException Locked="false" Priority="39" Name="toc 4"/> <w:LsdException Locked="false" Priority="39" Name="toc 5"/> <w:LsdException Locked="false" Priority="39" Name="toc 6"/> <w:LsdException Locked="false" Priority="39" Name="toc 7"/> <w:LsdException Locked="false" Priority="39" Name="toc 8"/> <w:LsdException Locked="false" Priority="39" Name="toc 9"/> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/> <w:LsdException Locked="false" Priority="10" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Title"/> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/> <w:LsdException Locked="false" Priority="11" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/> <w:LsdException Locked="false" Priority="22" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Strong"/> <w:LsdException Locked="false" Priority="20" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/> <w:LsdException Locked="false" Priority="59" SemiHidden="false" UnhideWhenUsed="false" Name="Table Grid"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/> <w:LsdException Locked="false" Priority="1" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 1"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 1"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 1"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/> <w:LsdException Locked="false" Priority="34" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/> <w:LsdException Locked="false" Priority="29" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Quote"/> <w:LsdException Locked="false" Priority="30" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 1"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 1"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 2"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 2"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 2"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 2"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 2"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 3"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 3"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 3"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 3"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 3"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 4"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 4"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 4"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 4"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 4"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 5"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 5"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 5"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 5"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 5"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 6"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 6"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 6"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 6"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 6"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/> <w:LsdException Locked="false" Priority="19" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/> <w:LsdException Locked="false" Priority="21" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/> <w:LsdException Locked="false" Priority="31" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/> <w:LsdException Locked="false" Priority="32" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/> <w:LsdException Locked="false" Priority="33" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Book Title"/> <w:LsdException Locked="false" Priority="37" Name="Bibliography"/> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} --> <!-- [endif]---->

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

}

}

运行结果:

AndroidGUI24:TabHost常用技巧

AndroidGUI24:TabHost常用技巧

AndroidGUI24:TabHost常用技巧

AndroidGUI24:TabHost常用技巧


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论