Android编程实现自定义Tab选项卡功能示例

所属分类: 软件编程 / Android 阅读数: 34
收藏 0 赞 0 分享

本文实例讲述了Android编程实现自定义Tab选项卡功能。分享给大家供大家参考,具体如下:

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import android.widget.TabHost.OnTabChangeListener;
import android.os.Build;
import android.view.View;
import java.lang.reflect.Field;
import android.view.LayoutInflater;
public class testTabActivity extends TabActivity {
 /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     int width =45;
     int height =48;
     final TabHost tabs = getTabHost();
     final TabWidget tabWidget = tabs.getTabWidget();
     Field mBottomLeftStrip;
     Field mBottomRightStrip;
     LayoutInflater.from(this).inflate(R.layout.tab_views, tabs.getTabContentView(), true);
     tabs.addTab(tabs.newTabSpec("first tab")
       .setIndicator("信息",getResources().getDrawable(R.drawable.m))
       .setContent(new Intent(testTabActivity.this,OneActivty.class))
       );
     tabs.addTab(tabs.newTabSpec("second tab")
     .setIndicator("收藏",getResources().getDrawable(R.drawable.n))
     .setContent(R.id.content));
     tabs.addTab(tabs.newTabSpec("second tab")
       .setIndicator("设置",getResources().getDrawable(R.drawable.s))
       .setContent(R.id.content));
     for (int i =0; i < tabWidget.getChildCount(); i++) {
       /**
       * 设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果
       */
       tabWidget.getChildAt(i).getLayoutParams().height = height;
       tabWidget.getChildAt(i).getLayoutParams().width = width;
     /**
      * 设置tab中标题文字的颜色,不然默认为黑色
      */
      final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
      tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
       /**
       * 此方法是为了去掉系统默认的色白的底角
       *
       * 在 TabWidget中mBottomLeftStrip、mBottomRightStrip
       * 都是私有变量,但是我们可以通过反射来获取
       *
       * 由于还不知道Android 2.2的接口是怎么样的,现在先加个判断好一些
       */
     if (Float.valueOf(Build.VERSION.RELEASE) <= 2.1) {
        try {
          mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip");
          mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip");
          if(!mBottomLeftStrip.isAccessible()) {
           mBottomLeftStrip.setAccessible(true);
          }
          if(!mBottomRightStrip.isAccessible()){
           mBottomRightStrip.setAccessible(true);
          }
         mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.no));
         mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.no));
        } catch (Exception e) {
         e.printStackTrace();
        }
     } else {
     /**
     * 不做任何处理
     */
     }
     View vvv = tabWidget.getChildAt(i);
  if(tabs.getCurrentTab()==i){
      vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button));
  }
  else {
      vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar));
  }
     }
     /**
     * 当点击tab选项卡的时候,更改当前的背景
     */
     tabs.setOnTabChangedListener(new OnTabChangeListener(){
  @Override
  public void onTabChanged(String tabId) {
   // TODO Auto-generated method stub
   for (int i =0; i < tabWidget.getChildCount(); i++) {
   View vvv = tabWidget.getChildAt(i);
   if(tabs.getCurrentTab()==i){
       vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button));
   }
   else {
       vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar));
   }
   }
  }});
   }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android编程之activity操作技巧总结》、《Android资源操作技巧汇总》、《Android文件操作技巧汇总》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程开发之SD卡操作方法汇总》、《Android视图View技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

android byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多