最近在研究自定义android的tab样式,网上很多教程。综合看了一下,觉得这篇文章介绍得不错:http://blog.lxbiao.com/2010/10/05/%E5%9C%A8android%E4%B8%8A%E5%AE%9E%E7%8E%B0iphone%E9%A3%8E%E6%A0%BC-bottom-tab-title-bar/
不过按照步骤做下去。发现几个需要注意的地方:
1. 自定义title中的组件和布局
<?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:orientation="horizontal">
<TextView
android:id="@+id/headerTitleTxtVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
>
</TextView>
</LinearLayout>
这里说的这个文件其实是MainActivity 的 R.layout.custom_titlebar,需要把以上代码保存到layout文件夹下,
命名:custom_titlebar.xml
2. <style name="customWindowTitleBackground" parent="@style/WindowTitleBackground">
原文中关于style中的parent定义:android:WindowTitleBackground,在android2.2中已经不可用,
原因参考这个帖子:
http://topic.csdn.net/u/20110811/13/54b9e7e2-055e-4d37-b3a9-857ddefad805.html,
jasonborne:“这个资源项在 Android平台的源码里面是private属性的。private属性的资源,
编译时也会被转换成一个整型常量,但跟public属性的资源不同的是,每次编译
得到的整型值是不一样的。所以,如果在App里面继承这样的private属性资源,可能会导致的一个问题是,
在不同的终端平台上,得到的结果是不一样的。这是之前版本sdk tools里的一个bug:
允许直接继承private属性的资源。
新版的sdk tools已经修复了这个bug,所以会报资源找不到。
处理的方式2种:1)降SDK版本 到r6以前;
2)找到那个private属性的资源源码,复制到工程里面。SDK开发小组的建议是第2种。”
所以修改了style:
<style name="customWindowTitleBackground"
parent="@style/WindowTitleBackground">
<item name="android:background">@drawable/custom_title_shape
</item>
</style>
<style name="WindowTitleBackground">
<item name="android:background">@android:drawable/title_bar</item>
</style>
效果图: