先看看效果图吧,个人觉得图标丑了点,不过还行,自己用PS做的

下面是全部代码和流程,一定要按流程顺序来,不然错误!

1.tabhost.xml

  1. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content" >
  6. <RelativeLayout
  7. android:id="@+id/relativelayout"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent" >
  10. <FrameLayout
  11. android:id="@android:id/tabcontent"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content" >
  14. <LinearLayout
  15. android:id="@+id/tab1"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:orientation="vertical" >
  19. </LinearLayout>
  20. <LinearLayout
  21. android:id="@+id/tab2"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:orientation="vertical" >
  25. </LinearLayout>
  26. <LinearLayout
  27. android:id="@+id/tab3"
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. android:orientation="vertical" >
  31. </LinearLayout>
  32. <LinearLayout
  33. android:id="@+id/tab4"
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. android:orientation="vertical" >
  37. </LinearLayout>
  38. <LinearLayout
  39. android:id="@+id/tab5"
  40. android:layout_width="fill_parent"
  41. android:layout_height="wrap_content"
  42. android:orientation="vertical" >
  43. </LinearLayout>
  44. </FrameLayout>
  45. <TabWidget
  46. android:id="@android:id/tabs"
  47. android:layout_width="fill_parent"
  48. android:layout_height="wrap_content"
  49. android:layout_alignParentBottom="true"
  50. android:background="@drawable/tabwidget_bj" >
  51. </TabWidget>
  52. </RelativeLayout>
  53. </TabHost>

2.tab_item_view.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6. <ImageView
  7. android:id="@+id/imageview"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_horizontal|top"
  11. android:padding="3dp" />
  12. <TextView
  13. android:id="@+id/textview"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center_horizontal|bottom"
  17. android:textColor="#fff"
  18. android:textSize="13sp"
  19. style="bold"/>
  20. </LinearLayout>

3.样式选择器selector:tab_item_style.xml,新建文件夹drawable,然后将该xml文件放进去

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/unpressed_bj" android:state_selected="false"/>
  4. <item android:drawable="@drawable/pressed_bj" android:state_selected="true"/>
  5. </selector>

4.java代码实现:MyTabHost.java

    1. package com.example.androidtabhost4;
    2. import android.os.Bundle;
    3. import android.app.Activity;
    4. import android.app.TabActivity;
    5. import android.content.Intent;
    6. import android.view.LayoutInflater;
    7. import android.view.Menu;
    8. import android.view.View;
    9. import android.widget.ImageView;
    10. import android.widget.TabHost;
    11. import android.widget.TextView;
    12. import android.widget.TabHost.TabSpec;
    13. public class MyTabHost extends TabActivity {
    14. private TabHost tabHost;
    15. private LayoutInflater layoutInflater;
    16. String[] mTitle = new String[] { "首页", "留言", "评论", "收藏", "更多" };
    17. int[] mIcon = new int[] { R.drawable.home, R.drawable.saying,
    18. R.drawable.zan, R.drawable.collect, R.drawable.more };
    19. int[] mTab = new int[] { R.id.tab1, R.id.tab2, R.id.tab3, R.id.tab4,
    20. R.id.tab5 };
    21. @Override
    22. protected void onCreate(Bundle savedInstanceState) {
    23. super.onCreate(savedInstanceState);
    24. setContentView(R.layout.tabhost);
    25. init();
    26. }
    27. public View getTabItemView(int i) {
    28. // TODO Auto-generated method stub
    29. View view = layoutInflater.inflate(R.layout.tab_item_view, null);
    30. ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
    31. imageView.setImageResource(mIcon[i]);
    32. TextView textView = (TextView) view.findViewById(R.id.textview);
    33. textView.setText(mTitle[i]);
    34. return view;
    35. }
    36. public void init() {
    37. // TODO Auto-generated method stub
    38. tabHost = getTabHost();
    39. layoutInflater = LayoutInflater.from(this);
    40. for (int i = 0; i < mTitle.length; i++) {
    41. TabSpec tabSpec = tabHost.newTabSpec(mTitle[i])
    42. .setIndicator(getTabItemView(i)).setContent(mTab[i]);
    43. tabHost.addTab(tabSpec);
    44. tabHost.getTabWidget().getChildAt(i)
    45. .setBackgroundResource(R.drawable.tab_item_style);
    46. tabHost.setup();
    47. }
    48. }
    49. }

android自定义TabWidget样式的更多相关文章

  1. Android: 自定义Tab样式,一种简单的方式。

    之前看到过论坛里已经有人发过自定义Tab样式的帖子,感觉有些复杂了,这里分享个简单的方法. 1.制作4个9patch的tab样式,可参考android默认的资源 tab_unselected.9.pn ...

  2. Android 自定义title样式

    requestWindowFeature(featrueId),它的功能是启用窗体的扩展特性.参数是Window类中定义的常量.一.枚举常量1.DEFAULT_FEATURES:系统默认状态,一般不需 ...

  3. Android自定义ProgressBar样式

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

  4. android 自定义progressbar 样式

    在res下创建drawable文件夹,新建文件drawable/progressbar_color.xml <layer-list xmlns:android="http://sche ...

  5. Android 自定义CheckBox 样式

    新建Android XML文件,类型选Drawable,根结点选selector,在这定义具体的样式. <?xml version="1.0" encoding=" ...

  6. android自定义TabWidget

    在做项目的时候,需要用到这个选项卡,刚开始看了系统的tabwidget,囧了,底边有黑线不说,还不美观,扒了好多的网页发现前辈做的能够满足自己的需求,将代码修改了下,就能用喽,伟人说过,站在前辈的肩膀 ...

  7. 转:android 自定义RadioButton样式

    http://gundumw100.iteye.com/blog/1146527  上面这种3选1的效果如何做呢?用代码写? 其实有更简单的办法,忘了RadioButton有什么特性了吗? 我就用Ra ...

  8. Android 自定义CheckBox样式

    1.首先在drawable文件夹中添加drawable文件checkbox_style.xml. <selector xmlns:android="http://schemas.and ...

  9. Android 自定义光标样式

    今天自定义光标,自己切图,不过怎么切都是很宽.不是一个很细的条.我用ps花了一个像素的直线,放上去还是不行.后来在网上找到方法,那就是用shape.不得不说,shape真的是太吊了. 给EditTex ...

随机推荐

  1. [质疑]编程之美求N!的二进制最低位1的位置的问题

    引子:编程之美给出了求N!的二进制最低位1的位置的二种思路,但是呢?但是呢?不信你仔细听我道来. 1.编程之美一书给出的解决思路 问题的目标是N!的二进制表示中最低位1的位置.给定一个整数N,求N!二 ...

  2. 数往知来 ASP.NET 模拟服务器:服务端_静态页面_动态页面的响应<十七>

      一.客户端是怎么看到我们的网页的呢/ 在浏览器端,如果用汉语请求的是一普通的HTML网页,呢么我们的IIS服务器, 接收到请求以后,那么从IIS服务器所在的电脑区查找该HTML网页, 找到以后将该 ...

  3. erlang observer工具

    1.服务器安装wxWidgets,之前需要装gtk+库 2.客户端安装otp_win64_17.5.exe 3.快捷方式点属性,在D:\erl6.4\bin\werl.exe后面加上参数 -setco ...

  4. Yarn通信过程

    yarn包括两块,一个是ResourceManager,主要的作用是管理集群上的资源,目前hadoop版本上,管理的只有cpu和内存. 另外一个叫NodeManager,这上面会跑我们的程序,叫App ...

  5. 理解OAuth 2.0 -摘自网络

    OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版.                                      本文对OA ...

  6. [cocos2d-js]长按按钮事件

    定义两个全局变量 var bLeftButtonClick = false; var bRightButtonClick = false; var MainLayer = cc.Layer.exten ...

  7. BOX2D测试

    ; ; Box2DTestLayer = cc.Layer.extend({ world:null, //GLESDebugDraw *m_debugDraw; ctor:function () { ...

  8. iPhone 6/6 Plus 出现后,如何改进工作流以实现一份设计稿支持多个尺寸?

    iPhone 6/6 Plus 出现后,如何改进工作流以实现一份设计稿支持多个尺寸? 2014-12-05 09:33 编辑: suiling 分类:iOS开发 来源:知乎(pigtwo)  2 22 ...

  9. Android NDK调试C++源码(转)

    [原创文章,转载请保留或注明出处,http://download.csdn.net/download/bigmaxim/5474055] 1. 相关软件 adt-bundle-windows-x86. ...

  10. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...