之前看到过论坛里已经有人发过自定义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="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"     />


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

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

这里可能有人会问为什么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="0" />        <FrameLayout android:id="@android:id/tabcontent"                android:layout_width="fill_parent" android:layout_height="0dip"            android:layout_weight="1"/>        </LinearLayout></TabHost>


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

RelativeLayout tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.tab_indicator, tw, false);TextView tvTab1 = (TextView)tabIndicator1.getChildAt(1);tvTab1.setText("tab1");//你也可以拿到ImageView设置该Tab的图片IconmTabHot = mTabHost.newTabSpec("TAB_1")                .setIndicator(tabIndicator1)                .setContent(contentIntent);

原文:http://www.eoeandroid.com/thread-73012-1-1.html

更多相关文章

  1. android 自定义ripple
  2. Android 自定义标签属性设置及使用
  3. Android样式与主题
  4. android v7 sample ActionBarDisplayOptions没有显示自定义logo
  5. Android:android.git.kernel.org 无法访问时下载源代码的解决方法
  6. Android 5.0 自定义dialog 背景不透明解决方法
  7. [Tools]Android Studio代码提示功能--Ctrl+Alt+Space(空格键)

随机推荐

  1. Android(安卓)动画机制(二)
  2. Android基础入门教程——8.1.1 Android中
  3. android:AsyncTask实现异步处理任务
  4. Android(安卓)改变字体颜色的三种方法
  5. Android socket通信 readline方法阻塞
  6. KJFrameForAndroid 1.3beta 发布,Android
  7. (转)Android从服务器端获取数据的几种方
  8. 分享几个Android很强势的的开源框架
  9. 【Android Training - 04】保存数据 [ Le
  10. Android开发实践:Android交叉编译工具链的