感受一下不错的述说:

【每日话题】Android是自学好还是培训好,你有啥高招?
http://www.eoeandroid.com/thread-193467-1-1.html

【每日话题】留在这城市的理由
http://www.eoeandroid.com/thread-191466-1-1.html

【每日话题】面试那些事儿—面霸是怎样炼成的
http://www.eoeandroid.com/thread-193078-1-1.html

网上已经有很多关于如何将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; /*** 整个流程就像使用ListView自定BaseAdapter一样 * * 如果要自定义TabHostActivity的Theme,并且不想要头部阴影* 一定要添加这个android:windowContentOverlay = null * * 如果想在别的项目里面使用TabHostActivity* 可以项目的属性里面找到Android,然后在Library部分添加这个项目(Api)* <a href='\"http://www.cnblogs.com/qianxudetianxia/archive/2011/05/01/2030232.html\"' target='\"_blank\"'>如何添加</a> * */public class ExampleActivity extends TabHostActivity { List mItems;private LayoutInflater mLayoutInflater; /**在初始化TabWidget前调用* 和TabWidget有关的必须在这里初始化*/@Overrideprotected void prepare() {TabItem home = new TabItem("首页", // titleR.drawable.icon_home, // iconR.drawable.example_tab_item_bg, // backgroundnew 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();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();} @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setCurrentTab(0);} /**tab的title,icon,边距设定等等*/@Overrideprotected 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*/@Overrideprotected String getTabItemId(int position) {return mItems.get(position).getTitle(); // 我们使用title来作为id,你也可以自定} /**点击tab时触发的事件*/@Overrideprotected Intent getTabItemIntent(int position) {return mItems.get(position).getIntent();} @Overrideprotected int getTabItemCount() {return mItems.size();} /**自定义头部文件*/@Overrideprotected View getTop() {return mLayoutInflater.inflate(R.layout.example_top, null);} }

框架代码:

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.TabSpec;import android.widget.TabWidget;import android.widget.TextView; public abstract class TabHostActivity extends TabActivity { private TabHost mTabHost;private TabWidget mTabWidget;private LayoutInflater mLayoutflater; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// set theme because we do not want the shadowsetTheme(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 viewView tabItem = mLayoutflater.inflate(R.layout.api_tab_item, null); TextView tvTabItem = (TextView) tabItem.findViewById(R.id.tab_item_tv);setTabItemTextView(tvTabItem, i);// set idString tabItemId = getTabItemId(i);// set tab specTabSpec 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 itreturn 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);} }


源码下载

更多相关文章

  1. 【AS基础篇二:线性布局和相对布局的使用】:设计一个简单的电影界
  2. Android(安卓)Studio 简单介绍和使用问题小结
  3. Dagger2+Retrofit+RxJava
  4. Android(安卓)Studio 中使用SVN注意事项
  5. android fragment 使用name关联Fragment
  6. [置顶] Android中使用Movie显示gif动态图
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. php将图片以二进制形式保存到mysql数据库
  2. 为什么插入忽略递增的auto_increment主键
  3. 解决The&#39;InnoDB&#39;feature is disa
  4. mysql常识以及存储引擎,锁和事务
  5. 05-mysql中的查询(第一章)
  6. Active Record或mysql中的批次计数
  7. Innode表空间碎片优化
  8. Win2003+IIS6.0+PHP+MYSQL+ASP+ASP.NET全
  9. VS2015 使用Mysql-connector/c++ 链接数
  10. mysql数据库监控利器lepus天兔工具安装和