之前看到过论坛里已经有人发过自定义Tab样式的帖子,感觉有些复杂了,这里分享个简单的方法。

1.制作4个9patch的tab样式,可参考android默认的资源
 tab_unselected.9.png     tab_selected.9.png    tab_press.9.png    tab_focus.9.png

这4个资源分别代表Tab的4种状态。

2.定义Tab的selector样式(就叫它tab_indicator.xml好了),将其放入drawable文件夹下,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />
</selector>
复制代码

3.编写indicator的布局文件(不妨也叫tab_indicator.xml),将其放入layout文件夹下,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_weight=""
android:layout_marginLeft="-3dip"
android:layout_marginRight="-3dip"
android:orientation="vertical"
android:background="@drawable/tab_indicator">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
/>

4.接下来就是在TabActivity中使用我们自己编写的Tab样式了:

// 首先获取TabWidget
mTabHost = getTabHost();
LinearLayout layout = (LinearLayout)mTabHost.getChildAt();
TabWidget tw = (TabWidget)layout.getChildAt();

这里可能有人会问为什么LinearLayout layout = (LinearLayout)mTabHost.getChildAt(0),接着看,Android源代码中是这样定义TabHost的(下面是原文件tab_content.xml定义),他的第一个也就是索引0的子孩子是一个线性布局,该孩子在里面才是定义了TabWidget视图。

<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">
<TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight=""/>
</LinearLayout>
</TabHost>

文接上文,废话不表。
然后用类似如下代码创建TabSpec,就大功告成了。

RelativeLayout tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_indicator, tw, false);
TextView tvTab1 = (TextView)tabIndicator1.getChildAt();
tvTab1.setText("tab1");//你也可以拿到ImageView设置该Tab的图片Icon
mTabHot = mTabHost.newTabSpec("TAB_1")
.setIndicator(tabIndicator1)
.setContent(contentIntent);
原文:http://www.eoeandroid.com/thread-73012-1-1.html

 

Android: 自定义Tab样式,一种简单的方式。的更多相关文章

  1. Android传递Bitmap的两种简单方式及其缺陷

    Android传递Bitmap的几种简单方式 一,通过Intent的Bundle. 比如有两个activity,A,B,从A进入B.先在A中将Bitmap写进去: Resources res=getR ...

  2. Android编程中的5种数据存储方式

    Android编程中的5种数据存储方式 作者:牛奶.不加糖 字体:[增加 减小] 类型:转载 时间:2015-12-03我要评论 这篇文章主要介绍了Android编程中的5种数据存储方式,结合实例形式 ...

  3. activiti复盘重推的一种简单实现方式:

    activiti复盘重推的一种简单实现方式: 设置流程的每一步让用户选择,比如一共有6步完成,用户选择从第4步开始复盘重推,那么把原来的推演oldId和4传到后台, 首先,后台生成一个新的推演id n ...

  4. Android自定义Notification并没有那么简单

    背景 最近需要实现一个自定义Notification的功能.网上找了找代码,解决方案就是通过RemoteViews来实现.但是在实现过程中遇到不少问题,网上也没有很好的文章描述这些问题,所以在这里做个 ...

  5. Xamarin.Android之Splash的几种简单实现

    对现在的APP软件来说,基本上都会有一个Splash页面,类似大家常说的欢迎页面.启动界面之类的. 正常来说这个页面都会有一些相关的信息,比如一些理念,Logo,版本信息等 下面就来看看在Xamari ...

  6. android开发中的5种存储数据方式

    数据存储在开发中是使用最频繁的,根据不同的情况选择不同的存储数据方式对于提高开发效率很有帮助.下面笔者在主要介绍Android平台中实现数据存储的5种方式. 1.使用SharedPreferences ...

  7. Android开发者须知的几种APP加密方式--备

    作为一个Android开发者,不仅需要使自己的APP功能丰富,便于使用,同时也需要去完善APP的安全性,下面就介绍几种简单而又可靠的加密方法.1.Spongy Castle Spongy Castle ...

  8. Android自定义View的三种实现方式

    在毕设项目中多处用到自定义控件,一直打算总结一下自定义控件的实现方式,今天就来总结一下吧.在此之前学习了郭霖大神博客上面关于自定义View的几篇博文,感觉受益良多,本文中就参考了其中的一些内容. 总结 ...

  9. Android自定义ProgressBar样式

    我们使用的进度条多种多样,下面有几种自定义的进度条的样式,下面介绍几个. 进度条的有基本的四种样式: 默认风格的进度条: android:progressBarStyle 水平长型进度条: andro ...

随机推荐

  1. iis7、iis8配置备份还原

    原文 iis7.iis8配置备份还原 方法1: 1.打开我们的IIS管理器,在功能视图里找到“共享的配置”这个功能然后双击进入. 2.进入“共享的配置”后单机右上方的“导出配置”选项,选择导出配置文件 ...

  2. 数据库性能监测工具——SQL Server Profiler

    使用SQL Server Profiler 进行sql监控需要一些设置: 其他的就是进行分析了~ 清除SQL SERVER缓存 常用的方法: DBCC DROPCLEANBUFFERS 从缓冲池中删除 ...

  3. Andriod Studio科普文章——3.大约gradle常见问题插头

    1.andriod gradle插件版本号过低. 错误位置: dependencies{ classpath 'com.android.tools.build:gradle:0.10.2' } 提示信 ...

  4. Javascript学习5 - 函数

    原文:Javascript学习5 - 函数 在Javascript中,函数和对象是交织在一起的.有些函数的特性与对象相关联.这一点的内容在第六部分会讨论到. 这一部分主要讨论函数与其它比较熟悉的语言( ...

  5. Java数据结构与算法(13) - ch06递归(归并排序)

    时间为O(N*logN). 归并排序的一个缺点是它需要在存储器中有另一个大小等于被排序的数据项数目的数组.归并两个有序的数组.利用递归,不断的将数组进行二分法排序,然后进行归并即可.

  6. zoj 3210 A Stack or A Queue? (数据结构水题)

     A Stack or A Queue? Time Limit: 1 Second      Memory Limit: 32768 KB Do you know stack and queue? ...

  7. Web项目

    Eclipse部署Web项目(图文讲解)   讲解是在linux下完成的,但对windows系统,操作也是一样的,不要被吓到了 1.下载Eclipse

  8. crawler_JVM_DNS_在爬虫中的应用

    DNS解析:即由域名 经过dns解析,跳转到真正服务器的地址,这个重复解析的耗时占请求很大比例. 在设计爬虫时比较细粒度的控制下,需要考虑dns解析. jdk从1.5往后对dns缓存有默认设置, 详见 ...

  9. java_tomcat_Server at localhost was unable to start within 45 seconds 小喵咪死活启动报错-二

    错误:Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds 错误提示就是我们限定了部署的时间导致的错 ...

  10. SSAS系列——【05】多维数据(编程体系结构)

    原文:SSAS系列--[05]多维数据(编程体系结构) 1.什么是AMO? 翻译:AMO是SSAS中一个完整的管理类集合,它在Microsoft.AnalysisServices命名空间下,我们可以在 ...