Android底部TabHost API
今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码:
首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可
package com.api; import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView; public abstract class TabHostActivity extends TabActivity { private TabHost mTabHost;
private TabWidget mTabWidget;
private LayoutInflater mLayoutflater; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set theme because we do not want the shadow
setTheme(R.style.Theme_Tabhost);
setContentView(R.layout.api_tab_host); mLayoutflater = getLayoutInflater(); mTabHost = getTabHost();
mTabWidget = getTabWidget();
//mTabWidget.setStripEnabled(false); // need android2.2 prepare(); initTop();
initTabSpec();
} private void initTop() {
View child = getTop();
LinearLayout layout = (LinearLayout) findViewById(R.id.tab_top);
layout.addView(child);
} private void initTabSpec() { int count = getTabItemCount(); for (int i = 0; i < count; i++) {
// set text view
View tabItem = mLayoutflater.inflate(R.layout.api_tab_item, null); TextView tvTabItem = (TextView) tabItem.findViewById(R.id.tab_item_tv);
setTabItemTextView(tvTabItem, i);
// set id
String tabItemId = getTabItemId(i);
// set tab spec
TabSpec tabSpec = mTabHost.newTabSpec(tabItemId);
tabSpec.setIndicator(tabItem);
tabSpec.setContent(getTabItemIntent(i)); mTabHost.addTab(tabSpec);
} } /** 在初始化界面之前调用 */
protected void prepare() {
// do nothing or you override it
} /** 自定义头部布局 */
protected View getTop() {
// do nothing or you override it
return null;
} protected int getTabCount() {
return mTabHost.getTabWidget().getTabCount();
} /** 设置TabItem的图标和标题等*/
abstract protected void setTabItemTextView(TextView textView, int position); abstract protected String getTabItemId(int position); abstract protected Intent getTabItemIntent(int position); abstract protected int getTabItemCount(); protected void setCurrentTab(int index) {
mTabHost.setCurrentTab(index);
} protected void focusCurrentTab(int index) {
mTabWidget.focusCurrentTab(index);
} }
需要再创建一个javabean TabItem
package com.api.example.app;
import android.content.Intent;
public class TabItem {
private String title; // tab item title
private int icon; // tab item icon
private int bg; // tab item background
private Intent intent; // tab item intent
public TabItem(String title, int icon, int bg, Intent intent) {
super();
this.title = title;
this.icon = icon;
this.bg = bg;
this.intent = intent;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public int getBg() {
return bg;
}
public void setBg(int bg) {
this.bg = bg;
}
public Intent getIntent() {
return intent;
}
public void setIntent(Intent intent) {
this.intent = intent;
}
}
然后创建一个Activity继承上面的TabHostActivity即可使用该自定义的TabHost了
package com.api.example.app; import java.util.ArrayList;
import java.util.List; import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabWidget;
import android.widget.TextView; import com.api.R;
import com.api.TabHostActivity; /**
* <p>整个流程就像使用ListView自定BaseAdapter一样</p>
*
* <p>如果要自定义TabHostActivity的Theme,并且不想要头部阴影
* 一定要添加这个android:windowContentOverlay = null</p>
*
* <p>如果想在别的项目里面使用TabHostActivity
* 可以项目的属性里面找到Android,然后在Library部分添加这个项目(Api)
* <a href="http://www.cnblogs.com/qianxudetianxia/archive/2011/05/01/2030232.html">如何添加</a></p>
* */
public class ExampleActivity extends TabHostActivity { List<TabItem> mItems;
private LayoutInflater mLayoutInflater; /**在初始化TabWidget前调用
* 和TabWidget有关的必须在这里初始化*/
@Override
protected void prepare() {
TabItem home = new TabItem(
"首页", // title
R.drawable.icon_home, // icon
R.drawable.example_tab_item_bg, // background
new Intent(this, Tab1Activity.class)); // intent TabItem info = new TabItem(
"资料",
R.drawable.icon_selfinfo,
R.drawable.example_tab_item_bg,
new Intent(this, Tab2Activity.class)); TabItem msg = new TabItem(
"信息",
R.drawable.icon_meassage,
R.drawable.example_tab_item_bg,
new Intent(this, Tab3Activity.class)); TabItem square = new TabItem(
"广场",
R.drawable.icon_square,
R.drawable.example_tab_item_bg,
new Intent(this, Tab4Activity.class)); TabItem more = new TabItem(
"更多",
R.drawable.icon_more,
R.drawable.example_tab_item_bg,
new Intent(this, Tab5Activity.class)); mItems = new ArrayList<TabItem>();
mItems.add(home);
mItems.add(info);
mItems.add(msg);
mItems.add(square);
mItems.add(more); // 设置分割线
TabWidget tabWidget = getTabWidget();
tabWidget.setDividerDrawable(R.drawable.tab_divider); mLayoutInflater = getLayoutInflater();
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setCurrentTab(0);
} /**tab的title,icon,边距设定等等*/
@Override
protected void setTabItemTextView(TextView textView, int position) {
textView.setPadding(3, 3, 3, 3);
textView.setText(mItems.get(position).getTitle());
textView.setBackgroundResource(mItems.get(position).getBg());
textView.setCompoundDrawablesWithIntrinsicBounds(0, mItems.get(position).getIcon(), 0, 0); } /**tab唯一的id*/
@Override
protected String getTabItemId(int position) {
return mItems.get(position).getTitle(); // 我们使用title来作为id,你也可以自定
} /**点击tab时触发的事件*/
@Override
protected Intent getTabItemIntent(int position) {
return mItems.get(position).getIntent();
} @Override
protected int getTabItemCount() {
return mItems.size();
} /**自定义头部文件*/
@Override
protected View getTop() {
return mLayoutInflater.inflate(R.layout.example_top, null);
} }
效果如下:

Android底部TabHost API的更多相关文章
- android之实现底部TabHost
先说布局文件,如下:利用android:layout_alignParentBottom="true" 实现底部显示 <?xml version="1.0" ...
- VS 2015 开发Android底部导航条----[实例代码,多图]
1.废话背景介绍 在Build 2016开发者大会上,微软宣布,Xamarin将被整合进所有版本的Visual Studio之中. 这也就是说,Xamarin将免费提供给所有购买了Visual ...
- android使用tabhost实现导航
参考 http://blog.csdn.net/xixinyan/article/details/6771341 http://blog.sina.com.cn/s/blog_6b04c8eb0101 ...
- Android底部导航栏
Android底部导航栏 今天简单写了一个底部导航栏,封装了一个库,用法比较简单 效果图 Github地址:https://github.com/kongqw/KqwBottomNavigation ...
- Android 底部TabActivity(1)——FragmentActivity
先看看效果图: 第一篇Tab系列的文章首先实现这样的风格的底部Tab:背景条颜色不变,我们是用了深灰的颜色,图标会发生对应的变化.当选中某个标签后该标签的背板会由正常的颜色变为不正常,哈哈,是变为加深 ...
- Android ViewPager+TabHost实现首页导航
今天发的是TabHost结合ViewPager实现首页底部导航的效果,虽然说网上有很多这样的Demo,不过呢,我还是要把自己练习写的发出来,没错!就是这么任性: 先上效果图,如下: 代码里面有注释,就 ...
- android中TabHost和RadioGroup
android底部菜单应用 博客分类: android--UI示例 TabHostMenuRadioGroupButton 在android中实现菜单功能有多种方法. Options Menu:用户 ...
- 从零開始学android<TabHost标签组件.二十九.>
TabHost主要特点是能够在一个窗体中显示多组标签栏的内容,在Android系统之中每一个标签栏就称为一个Tab.而包括这多个标签栏的容器就将其称为TabHost.TabHost类的继承结构例如以下 ...
- Android SDK之API Level
Android SDK之API Level Android SDK API_LEVEL Platform Version API Level VERSION_CODE Android 4.2, 4.2 ...
随机推荐
- 爬虫技术 -- 进阶学习(八)模拟简单浏览器(附c#代码)
由于最近在做毕业设计,需要用到一些简单的浏览器功能,于是学习了一下,顺便写篇博客~~大牛请勿喷,菜鸟练练手~ 实现界面如下:(简单朴素版@_@||) button_go实现如下: private vo ...
- Linux动态库的搜索路径
下面是目录结构: pengdl@localhost:~$ tree test/test/├── fun.c├── Fun.h└── t1 └── main.c 1 directory, 3 fi ...
- Guzzle – 构建 RESTful Web 服务的 PHP HTTP 框架
Guzzle 减轻了发送 HTTP 请求和创建 Web 服务客户端的痛苦.它包含建立一个强大的网络服务客户端的工具,包括:服务描述定义的输入和输出的 API,资源迭代器遍历分页资源,尽可能有效地发送大 ...
- 哀悼我的第一次"创业"经历
下周考完最后一科,大学四年时光基本上算是落下帷幕,剩下的就只是整整毕业设计了.如果按照正常的节奏,这个学期应该能够搞完毕业设计的2/3,但无奈还在广州的一家公司里面实习,没有多少时间弄,得拖到3月了. ...
- ASP.NET MVC 在控制器中接收视图表单POST过来的数据方法
方法一:通过Request.Form [HttpPost] public ActionResult Test() { string id=Reques ...
- Scrum 项目 7.0 Sprint回顾
7.0------------------------------------------------ Sprint回顾 让我们一次比一次做得更好. 1.回顾组织 主题:“我们怎样才能在下个spr ...
- JS 跨域问题常见的五种解决方式
一.什么是跨域? 要理解跨域问题,就先理解好概念.跨域问题是由于javascript语言安全限制中的同源策略造成的. 简单来说,同源策略是指一段脚本只能读取来自同一来源的窗口和文档的属性,这里的同一来 ...
- MongoDB安装说明以及MongoVUE使用
简单介绍mongoDb MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产 ...
- 关于DOM树的常见增删操作
//具体关于DOM的内容可参考我的另外一篇文章"文本对象模型(Document Object Model)". 案例要点: 1.创建并增加元素节点 2.判断是否存在 ...
- Ajax学习资源大全[本来是转载的,但是现在我增加了很多]
本欲放转载区,但是这样一文章放那里基本是没有用的,帮助不了任何人!所以放新手了!! 我一般非经典或者自己用不上不转载,所以如果你不幸看见了的话,恰恰你又对AJAX有兴趣的话不防看下,也许对你有用的!! ...