今天开始一个新专题:创新源于模仿。

第一篇从TabActivity着手,一直以为Android中的TabActivity只能放在上面,只能如此丑陋,直到有一天看到“米聊”。

咋一看,软件下面的那个菜单栏,觉得像是用LinearLayout+Button来实现的,但事实上,它却是一个Tab!
怎么看出来的?我就不多说了,你懂的。

下面我们来抽丝剥茧,一步步分析它的实现过程。

1.TabActivity的布局

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:gravity="center" android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" > <LinearLayout android:id="@+id/first" android:orientation="vertical" android:background="#ffffff" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="first tab" /> </LinearLayout> <LinearLayout android:id="@+id/second" android:orientation="vertical" android:background="#ffffff" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="second tab" /> </LinearLayout> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:background="@drawable/tab_bottom_bg" android:padding="3.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" /> </LinearLayout> </TabHost>

我们看到,要自定义一个位于屏幕下方的TAB标签,首先我们不能使用缺省的TabActivity实现了,啥事都要自己亲力亲为。
很好理解,把tabs放在tabcontent下面就可以了。其它不多说了。

2.MainActivity的实现代码


先看看缺省的实现代码,不复杂,省略一些无关的东西:

private void setIndicator(int icon, int title, int view) { String str = getResources().getString(title); TabHost.TabSpec localTabSpec=tabhost.newTabSpec(str).setIndicator(str,getResources().getDrawable(icon)).setContent(view); tabhost.addTab(localTabSpec); }

可以测试一下,效果出来了吧,虽然有点丑,但它真的在屏幕下方了。

3.美化TAB标签

现在我们来定制这个TAB的标签。先看代码:

private void setIndicator(int icon, int title, int view) { View localView = LayoutInflater.from(this.tabhost.getContext()).inflate(R.layout.main_activity_tab, null); ((ImageView)localView.findViewById(R.id.main_activity_tab_image)).setBackgroundResource(icon); ((TextView)localView.findViewById(R.id.main_activity_tab_text)).setText(title); String str = getResources().getString(title); TabHost.TabSpec localTabSpec = tabhost.newTabSpec(str).setIndicator(localView).setContent(view); tabhost.addTab(localTabSpec); }

注意到这个main_activity_tab的layout了吧,看看它的内容:

<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center" android:orientation="vertical" android:id="@id/tabsLayout" android:background="@drawable/tab_item_bkg" android:padding="3.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3.0dip" android:layout_marginBottom="3.0dip" > <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.6"> <ImageView android:layout_gravity="center" android:id="@id/main_activity_tab_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="2.0dip" android:scaleType="center" /> </FrameLayout> <TextView android:textSize="12.0dip" android:textColor="@drawable/tab_text_selector" android:id="@id/main_activity_tab_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Title" /> </LinearLayout

这个文件定义了每个TAB标签的样式,tab_item_bkg定义每个item的背景(包括focused/selected/pressed),每个item上面的图标和下面的文字都在代码中动态定义即可。其中tab_text_selector则定义文字的颜色。

剩下的事情就越发明显了,不用多说了。

创新源于模仿之一:TabActivity的美化相关推荐

  1. 呼叫中心的技术创新源于管理文化的变革

    杰克•韦尔奇说过:"如果你想让列车再快10公里,只需要加大油门:而若想使车速增加一倍,你就必须要更换铁轨了.资产重组只可以提高一时的公司生产力,只有管理文化上的改变,才能维持高生产力的发展. ...

  2. #创新应用#点心桌面:桌面美化利器!

    继上次推荐"点心通讯录"之后,本次再推荐一款点心的系列产品"点心桌面". 对于手机发烧友和喜欢美化的童鞋来说,可能对"点心桌面"已经很熟悉了 ...

  3. android 各类demo链接

    http://gundumw100.iteye.com/blog/940084 带磁性的悬浮窗体,类似于360绿色小人  主要实现的是:  1.悬浮所有窗体之上  2.有吸引力,吸附于屏幕边上  3. ...

  4. android代码精华 各路大神写的代码精华,大家一起分享

    http://blog.csdn.net/kan1kan5/article/details/41960259 http://gundumw100.iteye.com/blog/940084 带磁性的悬 ...

  5. Android文档及学习资料整理 【至2011年7月】

    学习参考网站 Android Developers  官方开发网站,有权威的 Dev Guide 和 Reffrence Android Source  官方Android全部开源源码网站,提供下载源 ...

  6. 学英语最有效的办法:模仿+重复!

    李勇,1979年农历10月初十,出生于江西南昌的一个小农村,中专毕业,2002年8月前英语还只有初中水平,后用两个月时间卧薪尝胆,发奋学习英语,战胜自我,取得人生巨大突破.2002年10月起开始从事外 ...

  7. 英语学习的六大黄金原则

    我在英语学习方面是十分幸运的.在下过硬工夫的过程中,我从未感到英语学习的单调和苦闷,也未感到英语学习有多么艰难.我读过一些英语名家谈体会的书,其中有中文译本,也有英文原文本.这些书给了我很多启发,使我 ...

  8. 英语学习六大原则(转)

    我在英语学习方面是十分幸运的.在下过硬工夫的过程中,我从未感到英语学习的单调和苦闷,也未感到英语学习有多么艰难.我读过一些英语名家谈体会的书,其中有中文译本,也有英文原文本.这些书给了我很多启发,使我 ...

  9. 英语学习得六大黄金原则

    (一)简单原则 学习英语:从简单的开始 运用英语:简单-好.更简单-更好.最简单-最好 上大学的时侯,英语老师让我们大量阅读英语.有些同学就借来原著,第一页看下来就有20几个生词,第二页还有20几个- ...

最新文章

  1. office365为新建账号发送欢迎邮件
  2. 南昌理工学院计算机系考研,南昌理工学院的学生可不可以考研和公务员
  3. 分布式文件系统研究-测试-文件下载测试
  4. 玩转oracle 11g(4):连接,文件操作,交互命令
  5. 5、Fiddler如何捕获HTTPS会话
  6. Java MinIO文件上传返回访问路径及访问配置
  7. shell脚本:判断本地和远程文件是否存在
  8. php 解析返回的xml,php解析xml的几种方式
  9. 基(là)础(jī)线段树详解
  10. PHP集成环境MxSrvs如何安装pcntl扩展
  11. SOLIDWORKS工程图教程:如何进行SOLIDWORKS工程图标注
  12. 4400php兑换美金是多少,美金换算人民币怎么算(美金换人民币怎样计算)
  13. HDU 4093 Xavier is Learning to Count FFT + 容斥原理 2011年上海现场赛C题
  14. 图片文字识别的方法有哪些?
  15. uwp之图片旋转动画实现
  16. 微信登录显示连接失败,请检查网络
  17. 资源网站模板Emlog程序zytheme模板
  18. 力扣347 前 K 个高频元素 -- JS
  19. 树莓派4B无线鼠标延迟解决办法
  20. 人月神话(各章精选)

热门文章

  1. kali修改时间/日期
  2. 关于python内存管理下列说法中错误的是_2.关于Python内存管理,下列说法错误的是_学小易找答案...
  3. 爱情呼叫转移-深度广度遍历合集
  4. 物联网,人工智能专业毕业论文选题推荐
  5. jquery封装插件的方法
  6. python零基础书推荐_零基础学习Python(Python初学者、Python入门)常见问题:资料、社区、书籍推荐...
  7. 动态设置路由,扫码进入页面,从二维码里拿到参数
  8. tf2.0先试试图片(七)——CNN卷积神经网络
  9. [创业之路-66]:创业注定是一场孤独的修行
  10. GoJS 2.3 Crack