界面比较简单,要想做得漂亮换几张图片就可以了。

第一步:先在布局(这里用了main.xml创建时自动生成的)里面放上TabHost ,只要将TabHost控件托至屏幕中就可:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/taskdescribe_buildingmeter_tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:id="@+id/buildingmeter_layout_unitname"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white">
</LinearLayout>
<LinearLayout
android:id="@+id/buildingmeter_layout_installstatus"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white">
</LinearLayout>
<LinearLayout
android:id="@+id/buildingmeter_layout_storey"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white">
</LinearLayout> </FrameLayout>
</LinearLayout>
</TabHost>

第二步:创建显示此TabWidget的布局tab项tabmini.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"> <TextView
android:id="@+id/tab_lable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" /> </LinearLayout>

第三步:在Activity里实现TabHost:

private TabHost tabHost;
private TabWidget tabWidget;
LayoutInflater inflater = activity.getLayoutInflater();

View view1 = inflater.inflate(R.layout.tabhost_tag, null);
TextView txt1 = (TextView) view1.findViewById(R.id.tab_lable);
txt1.setText("按单元名"); View view2 = inflater.inflate(R.layout.tabhost_tag, null);
TextView txt2 = (TextView) view2.findViewById(R.id.tab_lable);
txt2.setText("按状态"); View view3 = inflater.inflate(R.layout.tabhost_tag, null);
TextView txt3 = (TextView) view3.findViewById(R.id.tab_lable);
txt3.setText("按楼层"); //得到TabHost对象实例
tabHost =(TabHost) activity.findViewById(R.id.taskdescribe_buildingmeter_tabhost);
//调用 TabHost.setup()
tabHost.setup();
tabWidget = tabHost.getTabWidget();
//创建Tab标签
tabHost.addTab(tabHost.newTabSpec("one").setIndicator(view1).setContent(R.id.buildingmeter_layout_unitname));
tabHost.addTab(tabHost.newTabSpec("two").setIndicator(view2).setContent(R.id.buildingmeter_layout_installstatus));
tabHost.addTab(tabHost.newTabSpec("three").setIndicator(view3).setContent(R.id.buildingmeter_layout_storey)); //设置第一次打开时默认显示的标签
tabHost.setCurrentTab(0);
//初始化Tab的颜色,和字体的颜色
updateTab(tabHost);
//选择监听器
tabHost.setOnTabChangedListener(new tabChangedListener());
  /**
* 更新Tab标签的颜色,和字体的颜色
* @param tabHost
*/
private void updateTab(final TabHost tabHost)
{
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
View view = tabHost.getTabWidget().getChildAt(i);
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(R.id.tab_lable);
tv.setTextSize(16);
tv.setTypeface(Typeface.SERIF, 0); // 设置字体和风格
if (tabHost.getCurrentTab() == i)
{
//选中
view.setBackground(getResources().getDrawable(R.drawable.tabhost_current));//选中后的背景
tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
}
else
{
//不选中
view.setBackground(getResources().getDrawable(R.drawable.tabhost_default));//非选择的背景
tv.setTextColor(this.getResources().getColorStateList(android.R.color.black));
}
}
} /**
* TabHost选择监听器
* @author
*
*/
private class tabChangedListener implements OnTabChangeListener { @Override
public void onTabChanged(String tabId)
{
tabHost.setCurrentTabByTag(tabId);
updateTab(tabHost);
}
}

Android 自定义TabHost,TabWidget样式的更多相关文章

  1. Android 自定义RadioButton的样式

    Android 自定义RadioButton的样式 我们知道Android控件里的button,listview可以用xml的样式自定义成自己希望的漂亮样式. 最近用到RadioButton,利用xm ...

  2. android自定义tabhost,tabcontent用intent获得

    地址:http://my.oschina.net/aowu/blog/36282 自己改的自定义tabhost组建,效果图如左.有更好的朋友可以相互交流一下,嘿嘿. 1.先上AndroidManife ...

  3. 最简单的android自定义进度条样式

    一.自定义圆形进度条样式 1.在安卓项目drawable目录下新建一个xml文件如下:<?xml version="1.0" encoding="utf-8&quo ...

  4. Android 自定义ListView滚动条样式

    使用ListView FastScroller,默认滑块和自定义滑块图片的样子: 设置快速滚动属性很容易,只需在布局的xml文件里设置属性即可: <ListView android:id=&qu ...

  5. android自定义TabWidget样式

    先看看效果图吧,个人觉得图标丑了点,不过还行,自己用PS做的 下面是全部代码和流程,一定要按流程顺序来,不然错误! 1.tabhost.xml <TabHost xmlns:android=&q ...

  6. 自定义TabHost,TabWidget样式

    先看效果: 京东商城底部菜单栏 新浪微博底部菜单栏 本次学习效果图:

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

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

  8. Android:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换

    viewPager是v4包里的一个组件,可以实现滑动显示多个界面. android也为viewPager提供了一个adapter,此adapter最少要重写4个方法: public int getCo ...

  9. Android Tab -- 使用TabWidget、TabHost、TabActivity来实现

    原文地址:http://blog.csdn.net/crazy1235/article/details/42678877 TabActivity在API13之后被fragment替代了,所以不建议使用 ...

随机推荐

  1. python获取代理IP并测试是否可用

    # coding: utf-8 import urllib2 import re import time def getDL(page): url = 'http://www.xicidaili.co ...

  2. mariadb开机自启

    执行命令:systemctl enable mariadb 并由此想到,添加服务自启的命令格式: systemctl enable 服务名 当然关闭服务自启也是可以得: systemctl disab ...

  3. ubuntu--Supervisor进程管理工具

    安装,这个程序使用python写的 sudo apt-get install supervisor 配置一个你需要的配置文件 //进入 /etc/supervisor/conf.d文件目录,配置一个r ...

  4. sql 数据类型转换

    1.convert(float,endtimepart)——conver(数据类型,字段名称) 2.cast(endtimepart as float)——cast(字段名称 as 数据类型)

  5. Ubuntu 14.10 下编译Hadoop2.4.0

    在http://www.aboutyun.com/thread-8130-1-1.html 这里看到到,安装过程遇到了上面说的问题,所以将此文转载过来,以备不时之需,感谢此作者. 问题导读: 1.如果 ...

  6. HTML背景图片自适应

    由于<body>标签的图片不能够拉伸, 解决办法: 1.图片不够大,又background属性不能拉伸图片: 2.只能用个div,把其z-index值设为负,并使这个div大小为整个bod ...

  7. 廖雪峰Java5Java集合-5Queue-1使用Queue

    Queue特性和基本方法 Queue实现一个先进先出(FIFO, First In First Out)的队列.如收银台排队支付. Java中LinkedList实现了Queue接口,可以直接把Lin ...

  8. 3d图像坐标轴及css3属性的讲解

    3d立体坐标轴 如有不知道各种插件的怎么办? 网上查,百度 1.css选择器: 1.id 2.class 3.标签 4.子代 5.后代 6.交集 7.并级 8.通配符 9.结构 10.伪类 11.属性 ...

  9. OpenVZ管理

    查找内存超过5%,CPU超过10% CPU=${:-} MEM=${:-} for CTID in `vzlist|sed '1d'|awk '{print $1}'` { echo "== ...

  10. 使用命名管道的OVERLAPPED方式实现非阻塞模式编程 .

    命令管道是进程间通讯的一种常用方式,对于命令管道的介绍可以参考别的资料和书籍,这里推荐一个<VC++下命名管道编程的原理及实现>这篇博文,写得比较清楚.但是都是介绍了阻塞模式的编程,我这里 ...