自定义Tab选项卡

来源:互联网 发布:服务器加密软件 编辑:程序博客网 时间:2024/05/16 11:04

这个需求估计大家都是需要,这几天刚好做了一个项目,也大概的研究了一下,下面将自己的研究成果展现给大家,希望对大家有用!

我就直接贴核心部分的源码了,其他东西大家自己添加,不要懒到只跟我要全部源码,大家自己做一遍才能真正学到东西!

先贴效果给大家看看:
10052312227f4c547e2ed95abf.png1005231223a5fd7bb5e6057c73.png1005231222e5e54b4960c77378.png 
原文来自:http://www.youmi.net/bbs/viewthread.php?tid=102&extra=page%3D1&ordertype=1

  1. import android.app.TabActivity;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.widget.*;
  5. import android.widget.TabHost.OnTabChangeListener;
  6. import android.os.Build;
  7. import android.view.View;
  8. import java.lang.reflect.Field;
  9. import android.view.LayoutInflater;
  10. public class testTabActivity extends TabActivity {
  11.   /** Called when the activity is first created. */
  12.      @Override
  13.      public void onCreate(Bundle savedInstanceState) {
  14.          super.onCreate(savedInstanceState);
  15.          
  16.          int width =45;
  17.          int height =48;
  18.          
  19.          final TabHost tabs = getTabHost();
  20.          final TabWidget tabWidget = tabs.getTabWidget();
  21.          
  22.          Field mBottomLeftStrip; 
  23.          Field mBottomRightStrip; 
  24.        
  25.          LayoutInflater.from(this).inflate(R.layout.tab_views, tabs.getTabContentView(), true); 
  26.        
  27.          tabs.addTab(tabs.newTabSpec("first tab")
  28.               .setIndicator("信息",getResources().getDrawable(R.drawable.m))
  29.               .setContent(new Intent(testTabActivity.this,OneActivty.class))
  30.               );
  31.          
  32.          tabs.addTab(tabs.newTabSpec("second tab")
  33.           .setIndicator("收藏",getResources().getDrawable(R.drawable.n))
  34.           .setContent(R.id.content));
  35.          
  36.          tabs.addTab(tabs.newTabSpec("second tab")
  37.               .setIndicator("设置",getResources().getDrawable(R.drawable.s))
  38.               .setContent(R.id.content));
  39.          
  40.          for (int i =0; i < tabWidget.getChildCount(); i++) {
  41.              /**
  42.               * 设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果
  43.               */
  44.              tabWidget.getChildAt(i).getLayoutParams().height = height;
  45.              tabWidget.getChildAt(i).getLayoutParams().width = width;
  46.             
  47.           
  48.           /**
  49.            * 设置tab中标题文字的颜色,不然默认为黑色
  50.            */
  51.            final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
  52.           
  53.            tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
  54.           
  55.              
  56.          
  57.              
  58.              /**
  59.               * 此方法是为了去掉系统默认的色白的底角
  60.               * 
  61.               * 在 TabWidget中mBottomLeftStrip、mBottomRightStrip
  62.               * 都是私有变量,但是我们可以通过反射来获取
  63.               * 
  64.               * 由于还不知道Android 2.2的接口是怎么样的,现在先加个判断好一些
  65.               */
  66.           if (Float.valueOf(Build.VERSION.RELEASE) <= 2.1) {
  67.                 try { 
  68.                    mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip"); 
  69.                    mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip"); 
  70.                    if(!mBottomLeftStrip.isAccessible()) { 
  71.                      mBottomLeftStrip.setAccessible(true); 
  72.                    } 
  73.                    if(!mBottomRightStrip.isAccessible()){ 
  74.                      mBottomRightStrip.setAccessible(true); 
  75.                    } 
  76.                   mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.no)); 
  77.                   mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.no)); 
  78.                    
  79.                 } catch (Exception e) { 
  80.                   e.printStackTrace(); 
  81.                 } 
  82.           } else {
  83.           /**
  84.           * 不做任何处理
  85.           */
  86.           }
  87.          View vvv = tabWidget.getChildAt(i);
  88.    if(tabs.getCurrentTab()==i){
  89.            vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button));
  90.    }
  91.    else {
  92.            vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar));
  93.    }
  94.           
  95.          }
  96.          /**
  97.           * 当点击tab选项卡的时候,更改当前的背景
  98.           */
  99.          tabs.setOnTabChangedListener(new OnTabChangeListener(){
  100.     @Override
  101.     public void onTabChanged(String tabId) {
  102.      // TODO Auto-generated method stub
  103.      for (int i =0; i < tabWidget.getChildCount(); i++) {
  104.       View vvv = tabWidget.getChildAt(i);
  105.       if(tabs.getCurrentTab()==i){
  106.               vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button));
  107.       }
  108.       else {
  109.               vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar));
  110.       }
  111.      }
  112.     }});
  113.          
  114.      }
  115.      
  116.      
  117. }
复制代码

原创粉丝点击