Android 自定义TabHost,TabWidget样式



界面比较简单,要想做得漂亮换几张图片就可以了。
第一步:先在布局(这里用了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样式的更多相关文章
- Android 自定义RadioButton的样式
Android 自定义RadioButton的样式 我们知道Android控件里的button,listview可以用xml的样式自定义成自己希望的漂亮样式. 最近用到RadioButton,利用xm ...
- android自定义tabhost,tabcontent用intent获得
地址:http://my.oschina.net/aowu/blog/36282 自己改的自定义tabhost组建,效果图如左.有更好的朋友可以相互交流一下,嘿嘿. 1.先上AndroidManife ...
- 最简单的android自定义进度条样式
一.自定义圆形进度条样式 1.在安卓项目drawable目录下新建一个xml文件如下:<?xml version="1.0" encoding="utf-8&quo ...
- Android 自定义ListView滚动条样式
使用ListView FastScroller,默认滑块和自定义滑块图片的样子: 设置快速滚动属性很容易,只需在布局的xml文件里设置属性即可: <ListView android:id=&qu ...
- android自定义TabWidget样式
先看看效果图吧,个人觉得图标丑了点,不过还行,自己用PS做的 下面是全部代码和流程,一定要按流程顺序来,不然错误! 1.tabhost.xml <TabHost xmlns:android=&q ...
- 自定义TabHost,TabWidget样式
先看效果: 京东商城底部菜单栏 新浪微博底部菜单栏 本次学习效果图:
- Android: 自定义Tab样式,一种简单的方式。
之前看到过论坛里已经有人发过自定义Tab样式的帖子,感觉有些复杂了,这里分享个简单的方法. 1.制作4个9patch的tab样式,可参考android默认的资源 tab_unselected.9.pn ...
- Android:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换
viewPager是v4包里的一个组件,可以实现滑动显示多个界面. android也为viewPager提供了一个adapter,此adapter最少要重写4个方法: public int getCo ...
- Android Tab -- 使用TabWidget、TabHost、TabActivity来实现
原文地址:http://blog.csdn.net/crazy1235/article/details/42678877 TabActivity在API13之后被fragment替代了,所以不建议使用 ...
随机推荐
- 排序算法<No.6>【插入排序】
算法,我在路上,将自己了解的算法内容全部梳理一遍! 今天介绍简单点的,插入排序. 首先,什么是插入排序,这个维基百科上有.个人的理解,就是将一个数插入到一个已经排好序的数列当中某个合适的位置,使得增加 ...
- php+google/baidu翻译接口
<?php /** * @link http://www.joinf.com * @copyright Copyright (C) 2017 joinf.com. All rights rese ...
- 【java】JDK与JRE的区别
JRE和JDK区别: JDK和JRE提供的服务包: JDK是整个JAVA的核心,JDK包含如下核心组件: ·javac – 编译器 ·jar – 打包工具 ·javadoc – 文档生成器 ·jdb ...
- mac打开文件提示文件已经坏了的修改
10.12下面,mac做了安全的限制,不能打开任意的文件,需要解除限制 sudo spctl --master-disable
- [VC6,VC9] [ts,nts,deb] [rpm,msi] 你需要下载什么格式的文件
1.VC6与VC9的区别 VC6版本是使用Visual Studio 6编译器编译的,如果你的PHP是用Apache来架设的,那你就选择VC6版本. VC9版本是使用Visual Studio 200 ...
- SQL判断某列中是否包含中文字符或者英文字符
SQL判断某列中是否包含中文字符或者英文字符 [sql] select * from 表名 where 某列 like '%[吖-座]%' select * from 表名 where ...
- 关于JAVA架构师
在我们行业内,我们大致把程序员分为四级 1.初级Java程序员的重心在编写代码.运用框架: 2.中级Java程序员重心在编写代码和框架: 3.高级Java程序员技术攻关.性能调优: 4.架构师 解决业 ...
- Android keystore相关
一.生成keystorekeytool -genkey -alias test.keystore -keyalg RSA -validity -keystore test.keystore 二.查看 ...
- ApplicationDomain
ApplicationDomain 类的用途是存储 ActionScript 3.0 定义表.SWF 文件中的所有代码被定义为存在于ApplicationDomain 中.在使用 Loader 类 A ...
- 使用R语言-计算均值,方差等
R语言对于数值计算很方便,最近用到了计算方差,标准差的功能,特记录. 数据准备 height <- c(6.00, 5.92, 5.58, 5.92) 1 计算均值 mean(height) [ ...