自定义Tab选项卡样式

综合以下两篇文章应该可以做出所有简单的效果


第一篇-------------------------------------->转载自http://www.youmi.net/bbs/thread-102-1-1.html


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

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

先贴效果给大家看看:

device11.png device22.pngdevice33.png 


  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. }
第二篇------------------------------------------------>转载自 http://hkp.iteye.com/blog/1168507


这是前段时间做的一个项目的一部分,我把它单独提取出来!效果如下图:

 

 

可以把选项卡放在一个Activity的任何位置,自定义了选项卡按钮的背景颜色!

 

basicview_tab.xml

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="#852741"  
  7.     >  
  8.     <span style="white-space: pre;">    </span><!-- 导入别的xml文件 -->  
  9.     <include layout="@layout/title_bar"/>  
  10.     <TabHost xmlns:android="http://schemas.android.com/apk/res/android"    
  11.         android:id="@android:id/tabhost" android:layout_width="fill_parent"    
  12.         android:layout_height="fill_parent">    
  13.    <LinearLayout android:orientation="vertical"    
  14.     android:layout_width="fill_parent" android:layout_height="fill_parent">   
  15.      
  16.       <TabWidget android:id="@android:id/tabs"    
  17.       android:layout_width="fill_parent"     
  18.       android:layout_height="wrap_content" />   
  19.       <FrameLayout android:id="@android:id/tabcontent"    
  20.       android:layout_width="fill_parent"     
  21.       android:layout_height="wrap_content"    
  22.       android:layout_weight="3" >    
  23.            
  24.       </FrameLayout>  
  25.         
  26.          
  27.     </LinearLayout>    
  28. </TabHost>    
  29. </LinearLayout>  

 

自定义选项卡按钮  TabButton

 

 

Java代码   收藏代码
  1. public class TabButton extends LinearLayout {  
  2.   
  3.     public TabButton(Context c, int drawable, String text) {  
  4.         super(c);  
  5.         LayoutInflater inflater = LayoutInflater.from(c);  
  6.         View view = inflater.inflate(R.layout.tab_button_layout, null);  
  7.   
  8.         addView(view);  
  9.   
  10.         TextView tv = (TextView)     view.findViewById(R.id.tab_button_text);  
  11.   
  12.         ImageView iv = (ImageView) view.findViewById(R.id.tab_button_img);  
  13.         iv.setImageResource(drawable);  
  14.         tv.setText(text);  
  15.     }  
  16. }  
 

 

布局 tab_button_layout.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="62dip"  
  6.     android:background="@drawable/tab_selector_bg"  
  7.     android:gravity="center"  
  8.     >  
  9.     <ImageView   
  10.         android:layout_width="240dip"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/tab_button_img"  
  13.         android:layout_gravity="center_horizontal"  
  14.         />  
  15.     <TextView    
  16.         android:layout_width="wrap_content"   
  17.         android:layout_height="wrap_content"   
  18.         android:id="@+id/tab_button_text"  
  19.         android:layout_gravity="center"  
  20.         android:textColor="#ffffff"  
  21.     />  
  22. </LinearLayout>  
 

主界面代码 BasicViewTab

 

Java代码   收藏代码
  1. public class BasicViewTab extends TabActivity {  
  2.   
  3.     private TabHost tabHost;  
  4.     private TextView textView;  
  5.   
  6.     /* (non-Javadoc) 
  7.      * @see android.app.ActivityGroup#onCreate(android.os.Bundle) 
  8.      */  
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         // TODO Auto-generated method stub  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.basicview_tab);  
  14.           
  15.         Intent intent = new Intent(BasicViewTab.this,TabExpenses.class);  
  16.         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  17.         textView = (TextView)findViewById(R.id.tv_title_bar);  
  18.         textView.setText("Basic View Factor");  
  19.           
  20.         tabHost = getTabHost();   
  21.         tabHost.addTab(tabHost.newTabSpec("tab1")  
  22.                 .setIndicator(new TabButton(this,  
  23.                         R.drawable.tabbar_icon_basic_expenses_selector,  
  24.                         "Expenses"))  
  25.                 .setContent(intent));  
  26.         tabHost.addTab(tabHost.newTabSpec("tab2")  
  27.                 .setIndicator(new TabButton(this,  
  28.                         R.drawable.tabbar_icon_basic_setting_selector,  
  29.                         "Setting"))  
  30.                 .setContent(new Intent(this, TabSetting.class)));  
  31.   
  32.         tabHost.setCurrentTab(0);  
  33.           
  34.         ImageView backHome = (ImageView) findViewById(R.id.btn_back_home);  
  35.         backHome.setOnClickListener(new OnClickListener() {  
  36.               
  37.             @Override  
  38.             public void onClick(View v) {  
  39.                 // TODO Auto-generated method stub  
  40.                 BasicViewTab.this.finish();  
  41.             }  
  42.         });  
  43.     }  
  44. }  

 

 

附件里有完整代码!


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值