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

第一步:先在布局(这里用了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. 手动增加pe节并修改oep

    一直想学学怎么动动pe文件,学习了几篇文章尤其是寒晨的文章后,自己动手也尝试了一下加节和修改oep,写出来供和我一样菜的一起进步. 一.       增加pe节需要的操作 1.    确定内存中的节的 ...

  2. 推荐四款 Bug 管理系统,最适合你的才是最好的!

    转载自:https://www.jianshu.com/p/e7d3121eaaec   在这个移动互联网的时代,每天都会涌入大量新的 App,想要留住你的用户,必须时刻保持产品创新与系统的稳定.对于 ...

  3. Ribbon 常用配置

    配置参数 默认值 说明 <client>.<namespace>.listOfServers 配置服务器列表 <client>.<namespace>. ...

  4. 基于openresty的https配置实践

    最近机器人项目的子项目,由于和BAT中的一家进行合作,人家要求用HTTPS连接,于是乎,我们要改造我们的nginx的配置,加添HTTPS的支持. 当然了,HTTPS需要的证书,必须是认证机构颁发的,这 ...

  5. windows10中git-bash闪退的解决办法

    windows10中git-bash闪退的解决办法 出现错误详情 Windows10 64位专业版安装git .18之后出现 Git闪退,报错信息:bash: /dev/null: No such d ...

  6. Block Design 小技巧之添加RTL代码到block_design

    Block Design 小技巧之添加RTL代码到block_design 1.首先得打开Block Design,右击RTL文件,才会出现Add module to Block Design选项. ...

  7. KMP(字符串匹配)算法

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  8. WPF Demo16 资源

    <Window x:Class="RescourceDemo1.MainWindow" xmlns="http://schemas.microsoft.com/wi ...

  9. Bootstrap&bxslider

    Bootstrap 规则 一.响应式布局 @media 二.图标.字体 @font-face 三.基本使用 实例: <!DOCTYPE html> <html lang=" ...

  10. MySQL总论

    1. MySQL基本概念 1.1.  数据库的概念 数据库,简而言之就是存储数据的仓库,可以按照一定的数据结构存储管理大量的数据及数据与数据之间的关系,它本质上是一种信息管理系统.数据库根据存储采用的 ...