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

tab_unselected.9.pngtab_selected.9.pngtab_press.9.pngtab_focus.9.png

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

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



<? xmlversion="1.0"encoding="utf-8" ?>
< selector xmlns:android ="http://schemas.android.com/apk/res/android" >
<!-- Nonfocusedstates -->
< 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" />
<!-- Focusedstates -->
< 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文件夹下,代码如下:



<? xmlversion="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
="1"
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" mce_style ="?android:attr/tabWidgetStyle"
/>

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



//首先获取TabWidget
mTabHost=getTabHost();
LinearLayoutll=(LinearLayout)mTabHost.getChildAt(0);
TabWidgettw=(TabWidget)ll.getChildAt(0);

然后用类似如下代码创建TabSpec,就大功告成了。



RelativeLayouttabIndicator1=(RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_indicator,tw,false);
TextViewtvTab1=(TextView)tabIndicator1.getChildAt(1);
tvTab1.setText("tab1");
mTabHot=mTabHost.newTabSpec("TAB_1")
.setIndicator(tabIndicator1)
.setContent(contentIntent);

更多相关文章

  1. 【Android单元测试系列】真正的异步单元测试
  2. Android中intent如何传递自定义数据类型
  3. Android(安卓)PackageInstaller:安装应用的应用
  4. Ubuntu下Android(安卓)NDK的安装及配置
  5. android TabHost(选项卡)的使用方法
  6. 修改Android默认的RadioButton样式
  7. android webket的应用,webview,javascript
  8. Android+JNI调用–文件操作
  9. Android菜单应用(Menu)

随机推荐

  1. Android(安卓)Service
  2. 再谈 android 设备SN的获取 续 android
  3. Android引路蜂地图开发示例:第一个地图应
  4. android手机图片查看
  5. android download 下载管理
  6. Android之android:theme设置在Applicatio
  7. Android开发:还是Menu
  8. 【Android】内嵌html页面与native代码简
  9. Android开源项目第二篇——工具库篇
  10. runONUIThread 分析与使用