Android TabHost的标签放在底部,已写成框架,继承即可

合集下载

Android编程之TabWidget选项卡用法实例分析

Android编程之TabWidget选项卡用法实例分析

Android编程之TabWidget选项卡⽤法实例分析本⽂实例讲述了Android编程之TabWidget选项卡⽤法。

分享给⼤家供⼤家参考,具体如下:1 概览TabWidget与TabHost。

tab组件⼀般包括TabHost和TabWidget、FrameLayout,且TabWidget、FrameLayout属于TabHost。

是否继承TabActivity的问题实现步骤。

两种实现⽅式,⼀种是将每个Tab的布局嵌在TabHost中的FrameLayout中,每个Tab的内容布局与显⽰都在FrameLayout中进⾏,缺点是布局会显得很臃肿;⼀种是每个Tab从FrameLayout中独⽴出来,以Activity呈现,这样使得每个Tab有单独的布局。

2 效果图Widget在顶部的情形:3 主要布局3.1 TabMain布局⽅式⼀:<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="/apk/res/android"android:id="@+id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="60dip"android:layout_alignParentBottom="true"android:background="#424242" ></TabWidget><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent" ><LinearLayoutandroid:id="@+id/theme"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/theme_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Tab1" /></LinearLayout><LinearLayoutandroid:id="@+id/wallpaper"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/wallpaper_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Tab2" /></LinearLayout><LinearLayoutandroid:id="@+id/iconbg"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/iconbg_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Tab3" /></LinearLayout><LinearLayoutandroid:id="@+id/screenlock"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/screenlock_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Tab4" /></LinearLayout><LinearLayoutandroid:id="@+id/effect"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/effect_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Tab5" /></LinearLayout></FrameLayout></RelativeLayout></TabHost>⽅式⼆:<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="/apk/res/android"android:id="@android:id/tabhost"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/wcity_normal_bg" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1" ></FrameLayout><TabWidgetandroid:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@drawable/tab"/></LinearLayout></TabHost>3.2 TabItem布局这⼀部分中⽅式⼀与⽅式⼆没有什么区别,只有表⽰形式的区别。

Tab与TabHost

Tab与TabHost

2
------ ------- ------- ------- --Andr oid 编程基础
④ 在 Manifest.xml 文件中注册一下 MyTab 类(Activity) <activity android:name=".MyTab"> <intent-filter> <action android:name="android.intent.action.MAIN"></action> <category android:name="UNCHER"></category> </intent-filter> </activity> ⑤ 这时候,需要设计一下标签页对应的布局,一般采用 FrameLayout 作为根布局,每个标 签页面对应一个子节点的 Layout <?xml version="1.0" encoding="utf-8"?> <!-- 这里是根节点布局 -- > <FrameLayout xmlns:android="/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 第一个 Tab 对应的布局 -- > <LinearLayout android:id="@+id/widget_layout_Blue" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/widget34" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="EditText" android:textSize="18sp"> </EditText> <Button android:id="@+id/widget30" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button"> </Button> </LinearLayout> <!-- 第二个 Tab 对应的布局 -- > <LinearLayout android:id="@+id/widget_layout_red" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <AnalogClock android:id="@+id/widget36" android:layout_width="wrap_content" android:layout_height="wrap_content"> </AnalogClock> </LinearLayout> <!-- 第三个 Tab 对应的布局 -- > <LinearLayout android:id="@+id/widget_layout_green" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <RadioGroup android:id="@+id/widget43" android:layout_width="166px" android:layout_height="98px" android:orientation="vertical"> <RadioButton android:id="@+id/widget44" android:layout_width="wrap_content" android:layout_height="wrap_content"

复习答案 ANDROID

复习答案 ANDROID
2013 年 嵌入式专业 android 复习题库 (内部资料 ,禁止 QQ 共享、网络共享)
一、选择题
1.Android 是一种以 Linux 为基础的开放源码操作系统,主要应用于便携设备。Android 股份有限公 司于 2003 年在美国加州成立,在 2005 年被 Google 收购,从 2009 年 2 月开始推出 Android 1.1,到 2012 年已经推出 Android 4.0,发展非常的迅速,以下叙述有问题的是。 () A、 2009 年 4 月,推出了 Android 1.5 ,名称是 Cupcake 纸杯蛋糕 B、 Android 1.5 主要改进:摄像头开启和拍照速度更快;GPS 定位速度大幅提升;支持触屏虚拟键盘 输入 C、 2009 年 9 月,推出了 Android 1.6,名称是 Donut 甜甜圈 D、 2009 年 4 月,微软推出了 Android 1.5 ,名称是 Gingerbread 姜饼 2.Activity 是 Android 程序的呈现层,显示可视化的用户界面,并接收与用户交互所产生的界面事 件,以下叙述有问题的是。 () A、 Android 应用程序可以包含一个或多个 Activity,一般在程序启动后会呈现一个 Activity,用 于提示用户程序已经正常启动 B、 在界面上的表现形式:全屏窗体,非全屏悬浮窗体,对话框 C、 Activity 形象的说就是一个容器,在里面放置各种控件(按钮,文本,复选框等),就形成了软 件的界面 D、 Activity 和 Service 一样,都没有用户界面,适合用于需要长时间在后台运行的应用 3.Button,就是按钮,是 Android 中应用最多的控件之一,也是应用程序和用户实现 相互 交互的功能之一,以下叙述错误的是。 () A、 Button 是一种按钮控件,用户能够在该控件上点击,并后引发相应的事件处理函数 B、 ImageButton 是一种带图片显示的控件按钮 C、 使用 Button,必须导入包 android.widget.Button D、 Button 是一种文本显示控件,主要功能就是显示文本 4. Android 系统中会存在很多应用程序,Content Provider 属于 Android 应用程序的组件之一,作 为应用程序之间主要的共享数据的途径,以下叙述有问题的是。 () A、ContentProvider 的不必进行权限许可,开发者可以任意访问和使用 B、android 有一个独特之处就是,数据库只能被它的创建者所使用,其他的应用是不能访问到的, 所以如果你想实现不同应用之间的数据共享, content provider 是一种非常重要的方法 C、在 Android 中,content provider 是一个特殊的存储数据的类型,它提供了一套标准的接口用来 获取以及操作数据 D、Content Provider 主要的功能就是存储并检索数据以及向其他应用程序提供访问数据的接口 5. Dalvik 调试监视服务 DDMS 是由 Android 软件开发包()提供的调试工具。 A、 JDK B. SDK C.Logcat D. console 6. 下列哪个是 Android 的集成开发环境( ) A、 Mozilla Firefox B、 eclipse C、 Visual C++ 6.0 D、 CodeBlocks 7. 下列哪个是 Java 的集成开发环境() A、eclipse B、Microsoft Office PowerPoint C、SQL Server 2008 D、Adobe Dreamweaver 8. Java 源文件和编译后的文件扩展名分别为( ) A、 .class 和 .java B、 .java 和 .class C、 .class 和 .class D、 .java 和 .java 9. 设 int x=1,float y=2,则表达式 x / 2 10. 下列哪一个关键字用于实现接口来定义类? ( ) A、extends B、implements C、abstract D、interface 11. 若 a 的值为 3 时,下列程序段被执行后,c 的值是多少?( ) if ( a>0 ) if ( a>3 ) c = 12; else c = 3; else c = 7; A、1 B、2 C、3 D、4 12. 下列程序段执行后,变量 a 的值是多少?() int a = 5, b =12; a = ++a+b; A、10 B、18 C、12 D、5 13. 下列程序段执行后,变量 a 的值是多少?() int a = 0; for(int i = 0; i < 10; i++){ if(i%2 == 0) a++; } A、1 B、5 C、7 D、12 14. 下列哪个可以用于创建一个新的线程() A、从 Thread 类派生一个新的线程类,并重载它的 run()方法 B、Java 无法创建新线程 C、只需定义一个 Thread 对象、只需要派生 Runnalbe 接口即可 D、实现 ThreadRunnalbe 接口,并重载 start()方法 15. 当一个线程调用 start 方法后,该线程处于什么状态() A、新建状态 B、就绪状态 C、运行状态 D、暂停状态 16. Android 应用程序开发中,开发者经常使用 XML 文件声明界面布局的特点,以下对界面设置相关 叙述正确的是要求的是() A、Android 开发中,对于界面布局的开发方式是将程序的表现层和控制层分离 B、Android 开发中,在后期简单调整修改用户界面时,既需对 XML 界面布局文件进行修改,也需更 改程序的源代码 C、android:layout_height="wrap_content" 表示此控件的宽度等于父控件的宽度 D、Android 开发中,界面布局的布局和功能都必须在代码中实现,没有其他的文件对布局进行描述 17. 在 android 开发中列表 ListView 是比较常用的组件,它以列表的形式展示具体内容,并且能够 根据数据的长度自适应显示,以下对 ListView 表述不正确的是() A、列表的显示需要三个元素:ListVeiw 控件(用来展示列表的 View)属于其中之一; B、ListView 不一定需要使用 Adapter 适配器,它可以直接把数组列表的值显示到界面控件 C、列表的显示需要三个元素:适配器 Adapter(用来把数据映射到 ListView 上的中介) 属于其中之一 D、列表的显示需要三个元素:数据源(具体的将被映射的字符串,图片,或者数组) 属于其中之一 18. 在 android 开发中,Adapter 是连接后端数据和前端显示的适配器接口,是数据和 UI(View)之 间 一 个 重 要 的 纽 带 。 在 常 见 的 View(ListView,GridView , Spinner)等 地 方 都 需 要 用到 Adapter, 以下对 Adapter 表述不正确的是() A、一般来说,数据可以来自一个数组,一个 List,或者数据库中的游标。这些是程序开发中用到的

Android-TabHost_用布局管理器实现标签选项卡

Android-TabHost_用布局管理器实现标签选项卡

A VDSDKActivity_main.xml<TabHost xmlns:android="/apk/res/android"android:id="@+id/TabHost1"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TabWidgetandroid:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"/><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:id="@+id/tab_edit"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><EditTextandroid:id="@+id/edit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="请输入检索关键字..."android:textSize="18px"/><Buttonandroid:id="@+id/but"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="搜索"/></LinearLayout><LinearLayoutandroid:id="@+id/tab_clock"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><AnalogClockandroid:id="@+id/myAnalogClock"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout><LinearLayoutandroid:id="@+id/tab_sex"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><RadioGroupandroid:id="@+id/sex"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"><RadioButtonandroid:id="@+id/male"android:checked="true"android:text="性别:男"/><RadioButtonandroid:id="@+id/female"android:text="性别:女"/></RadioGroup></LinearLayout></FrameLayout></LinearLayout></TabHost>Mainactivity.javapackage com.example.androidtest_tabhost02;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class MainActivity extends Activity {private TabHost myTabHost=null;private int[] layRes={R.id.tab_edit,R.id.tab_clock,R.id.tab_sex};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(yout.activity_main);myTabHost=(TabHost)super.findViewById(R.id.TabHost1);myTabHost.setup();for(int x=0;x<layRes.length;x++){TabSpec myTab=myTabHost.newTabSpec("tab"+x);myTab.setIndicator("标签"+x);myTab.setContent(yRes[x]);myTabHost.addTab(myTab);}myTabHost.setCurrentTab(1);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}。

android bottomnavigationbar+fragment 实现原理

android bottomnavigationbar+fragment 实现原理

android bottomnavigationbar+fragment 实现原理Android的BottomNavigationBar和Fragment是两个不同的概念。

BottomNavigationBar是一个用户界面组件,用于在应用程序底部显示导航菜单或选项卡。

它通常包含多个选项,每个选项表示不同的功能或页面。

当用户点击某个选项时,可以切换到相应的页面。

Fragment是Android中的一个概念,用于表示应用程序的独立模块或UI界面的一部分。

它可以被重用、组合和替换,用于构建复杂的用户界面。

每个Fragment都有自己的生命周期,并可以与其他Fragment进行交互。

要实现BottomNavigationBar和Fragment的联动,可以按照以下步骤进行操作:1. 在布局文件中添加BottomNavigationBar组件,并定义每个选项的图标和文本标签。

2. 创建一个Activity作为容器,用于承载Fragment。

3. 创建每个页面对应的Fragment,并将它们添加到Activity中。

4. 在Activity中处理BottomNavigationBar的点击事件。

5. 在点击事件中根据选项的索引,使用FragmentManager切换到相应的Fragment。

6. 在Fragment中进行相应的业务逻辑处理。

具体实现步骤可能会因应用的需求而有所不同,但以上是一个基本的实现原理。

在实现过程中,可以根据需要进行扩展和定制化,例如添加动画效果、处理Fragment的生命周期等。

需要注意的是,BottomNavigationBar和Fragment的实现是可自由组合的,可以根据应用的需要选择合适的方式进行开发。

android 第三方库中theme 继承规则

android 第三方库中theme 继承规则

android 第三方库中theme 继承规则在Android开发中,第三方库中的theme继承规则是指Android应用程序在使用第三方库时,如何继承和应用该库中定义的主题(theme)。

通过正确使用主题继承规则,开发者可以方便地为应用程序添加样式和风格。

Android的主题(theme)是一种定义了应用程序外观和样式的资源,可以通过在AndroidManifest.xml文件中的<application>标签中设置android:theme属性来为整个应用程序指定一个全局主题。

而第三方库中的主题通常是为了给应用程序提供特定的样式效果或功能,开发者可以选择继承这些主题并根据需求进行修改和扩展。

在使用第三方库中的主题时,遵循以下几个继承规则:1. 继承默认主题:第三方库通常提供了一个默认的主题,开发者可以通过在应用程序的主题中使用parent属性指定该默认主题作为基础。

例如,使用以下代码来继承第三方库中名为"LibraryTheme"的默认主题:```xml<style name="AppTheme" parent="LibraryTheme"><!-- 自定义修改和扩展 --></style>```2. 继承本地主题:有时候,开发者可能已经在本地定义了一些自定义的主题,并想要将第三方库中的主题与之进行结合。

可以通过在应用程序的主题中使用parent属性指定本地主题作为基础,并使用"."来分隔库主题和本地主题。

例如,使用以下代码来继承第三方库中名为"LibraryTheme"的主题,并与本地主题"CustomTheme"进行结合:```xml<style name="AppTheme" parent="LibraryTheme.CustomTheme"><!-- 自定义修改和扩展 --></style>```3. 自定义样式扩展:开发者可以在应用程序的主题中添加自定义样式来扩展第三方库中的主题。

Tab标签页布局示例

Tab标签页布局示例
*TabActivity 管理一个 TabHost 对象,因为 TabActivity 封装了一个 TabHost 组建的构建、 恢复、更新过程。
*从而使得客户端可以直接使用 TabHost 的实例,而无须关心 TabHost 的生命周期管理。 而只需要重写 TabActivity 中的 onCreate 方法,设置自定义的 TabHost 布局文件及创建相应 的选项卡。
是为了不让内容和标签重叠 --> <FrameLayout android:id="@android:id/tabcontent"
android:layout_height="fill_parent" android:layout_width="fill_parent" android:paddingTop="65dp"> <!-- LinearLayout 是一个标签里的内容,程序根据你定义的 ID 来把他们放在不同 的标签下面 --> <LinearLayout android:id="@+id/linearLayout1"
杨教授工作室,版权所1有,盗版必究, 1/10页
杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料
1.1.1 Tab 标签页布局示例 1、Tab 标签页控件的主要功能
使用 Tab 标签页控件,可以在同一个空间里放置更多内容。比如如下图所示的 Android 自带的拨号程序及通讯录等,就使用了 Tab 标签功能。
是@android:id/tabs。
杨教授工作室,版权所2有,盗版必究, 2/10页
杨教授工作室 精心创作的优秀程序员 职业提升必读系列资料

androidTAb分页菜单实现总结

androidTAb分页菜单实现总结

android TAb分页菜单实现总结首先说明的是,我们做APP开发,Tab分页不管是顶部还是底部,都是必不可少的,网上也有太多太多的实现方式了,我在这里总结一下:第一种方式:TabHost原始方式:这里实现的是底部菜单:布局文件:(我们通过RelativeLayout 可以把TabWidget定位在底部)android:id="@android:id/tabhost"android:layout_width="fill_parent"android:layout_height="fill_parent" ><RELATIVELAYOUT< p=""></RELATIVELAYOUT<>android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:padding="3dp" ><framelayout< p=""> </framelayout<>android:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1" ><TABWIDGET< p=""></TABWIDGET<>android:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignBottom="@android:id/tabcontent"android:background="@drawable/tabbar_bg" />在这里我们将说明一下:之前我是获取到TabWidget的view试图及内部icon和title,然后控制实现其效果,但是我们也可以用另外一种方式,也就是我们调用TabHost.TabSpec 的setIndicator(View view);这个方法,我们可以定制显示的view,代码片段:/**** 创建footerview*/public void createFooterView() {tabHost = getTabHost(); // The activity TabHostview = new TabView(this, R.drawable.tabbar_icon_home,R.drawable.tabbar_icon_home_selecotr);view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.footer_view_selector));intent = new Intent(MainActivity.this, HomeActivity.class);spec = tabHost.newTabSpec("num1").setIndicator(view).setContent(intent); tabHost.addTab(spec);view = new TabView(this, R.drawable.tabbar_icon_search,R.drawable.tabbar_icon_search_selecotr);view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.footer_view_selector));intent = new Intent(MainActivity.this, HomeActivity.class);spec = tabHost.newTabSpec("num2").setIndicator(view).setContent(intent); tabHost.addTab(spec);view = new TabView(this, R.drawable.tabbar_icon_cart,R.drawable.tabbar_icon_cart_selector);view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.footer_view_selector));intent = new Intent(MainActivity.this, HomeActivity.class);spec = tabHost.newTabSpec("num3").setIndicator(view).setContent(intent); tabHost.addTab(spec);view = new TabView(this, R.drawable.tabbar_icon_more,R.drawable.tabbar_icon_more_selecotr);view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.footer_view_selector));intent = new Intent(MainActivity.this, HomeActivity.class);spec = tabHost.newTabSpec("num4").setIndicator(view).setContent(intent);tabHost.addTab(spec);}/**** 自定义view**/class TabView extends LinearLayout {ImageView imageView;public TabView(Context c, int drawable, int drawableselec) {super(c);imageView = new ImageView(c);// 可以定制点击后状态StateListDrawable listDrawable = new StateListDrawable();// 未选listDrawable.addState(SELECTED_STATE_SET, this.getResources().getDrawable(drawableselec));// 选择listDrawable.addState(ENABLED_STATE_SET, this.getResources().getDrawable(drawable));imageView.setImageDrawable(listDrawable);// 引用 StateListDrawablesetGravity(Gravity.CENTER);addView(imageView);}}这样我们就实现想要的效果了.(建议使用这种方法,我的项目就是用的这个实现的.)如果我是图标和文字分开的,我们也可以用(RadioButton代替,也许大家都不陌生,一会我简单介绍下)这个源码是因为项目里面用的。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

package com.api.example.app;import java.util.ArrayList;import java.util.List;import android.content.Intent;import android.os.Bundle;import youtInflater;import android.view.View;import android.widget.T abWidget;import android.widget.T extView;import com.api.R;import com.api.T abHostActivity;/***整个流程就像使用ListView自定BaseAdapter一样**如果要自定义T abHostActivity的Theme,并且不想要头部阴影* 一定要添加这个android:windowContentOverlay = null**如果想在别的项目里面使用T abHostActivity* 可以项目的属性里面找到Android,然后在Library部分添加这个项目(Api)* /qianxudetianxia/archive/2011/05/01/2030232.html">如何添加* */public class ExampleActivity extends T abHostActivity {ListmItems;private LayoutInflater mLayoutInflater;/**在初始化T abWidget前调用* 和T abWidget有关的必须在这里初始化*/@Overrideprotectedvoid prepare() {T abItem home = new T abItem("首页", //titleR.drawable.icon_home, //iconR.drawable.example_tab_item_bg, //backgroundnew Intent(this, T ab1Activity.class)); //intentT abItem info = new T abItem("资料",R.drawable.icon_selfinfo,R.drawable.example_tab_item_bg,new Intent(this, T ab2Activity.class));T abItem msg = new T abItem("信息",R.drawable.icon_meassage,R.drawable.example_tab_item_bg,new Intent(this, T ab3Activity.class));T abItem square = new T abItem("广场",R.drawable.icon_square,R.drawable.example_tab_item_bg,new Intent(this, T ab4Activity.class));T abItem more = new T abItem("更多",R.drawable.icon_more,R.drawable.example_tab_item_bg,new Intent(this, T ab5Activity.class));mItems = new ArrayList();mItems.add(home);mItems.add(info);mItems.add(msg);mItems.add(square);mItems.add(more);//设置分割线T abWidget tabWidget = getT abWidget(); tabWidget.setDividerDrawable(R.drawable.tab_divider);mLayoutInflater = getLayoutInflater();}@Overrideprotectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setCurrentT ab(0);}/**tab的title,icon,边距设定等等*/@Overrideprotectedvoid setT abItemT extView(T extView textView, int position) { textView.setPadding(3, 3, 3, 3);textView.setT ext(mItems.get(position).getTitle());textView.setBackgroundResource(mItems.get(position).getBg()); textView.setCompoundDrawablesWithIntrinsicBounds(0,mItems.get(position).getIcon(), 0, 0);}/**tab唯一的id*/@Overrideprotected String getT abItemId(int position) {return mItems.get(position).getTitle(); //我们使用title来作为id,你也可以自定}/**点击tab时触发的事件*/@Overrideprotected Intent getT abItemIntent(int position) {return mItems.get(position).getIntent();}@Overrideprotectedint getT abItemCount() {return mItems.size();}/**自定义头部文件*/@Overrideprotected View getT op() {return mLayoutInflater.inflate(yout.example_top, null); }}框架代码:T abHostActivity package com.api;import android.app.T abActivity;import android.content.Intent;import android.os.Bundle;import youtInflater;import android.view.View;import android.widget.LinearLayout;import android.widget.T abHost;import android.widget.T abHost.T abSpec;import android.widget.T abWidget;import android.widget.T extView;public abstract class T abHostActivity extends T abActivity {private T abHost mT abHost;private T abWidget mT abWidget;private LayoutInflater mLayoutflater;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);//set theme because we do not want the shadowsetTheme(R.style.Theme_T abhost);setContentView(yout.api_tab_host);mLayoutflater = getLayoutInflater();mT abHost = getT abHost();mT abWidget = getT abWidget();//mT abWidget.setStripEnabled(false); //need android2.2prepare();initT op();initT abSpec();}private void initT op() {View child = getT op();LinearLayout layout = (LinearLayout) findViewById(R.id.tab_top);layout.addView(child);}private void initT abSpec() {int count = getT abItemCount();for (int i = 0; i < count; i++) {//set text viewView tabItem = mLayoutflater.inflate(yout.api_tab_item, null);T extView tvT abItem = (T extView) tabItem.findViewById(R.id.tab_item_tv);setT abItemT extView(tvT abItem, i);//set idString tabItemId = getT abItemId(i);//set tab specT abSpec tabSpec = mT abHost.newT abSpec(tabItemId);tabSpec.setIndicator(tabItem);tabSpec.setContent(getT abItemIntent(i));mT abHost.addT ab(tabSpec);}}/**在初始化界面之前调用*/protected void prepare() {//do nothing or you override it}/**自定义头部布局*/protected View getT op() {//do nothing or you override itreturn null;}protected int getT abCount() {return mT abHost.getT abWidget().getT abCount();}/**设置T abItem的图标和标题等*/abstract protected void setT abItemT extView(T extView textView, int position); abstract protected String getT abItemId(int position);abstract protected Intent getT abItemIntent(int position);abstract protected int getT abItemCount();protected void setCurrentT ab(int index) {mT abHost.setCurrentT ab(index);}protected void focusCurrentT ab(int index) { mT abWidget.focusCurrentT ab(index);}}。

相关文档
最新文档