`
endual
  • 浏览: 3506401 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

android 自定义tabhost的tabs (转) 用gridview实现

 
阅读更多

2011.09.14(3)——— android 自定义tabhost的tabs 
参考:http://www.cnblogs.com/over140/archive/2011/03/02/1968042.html 
Java代码  收藏代码
  1. http://www.iteye.com/topic/1116261  


我们直接用系统的tabhost时 如下图 
 

可以看见 两个tab中间有空隙 也许我们不需要这些空隙或者系统的样式 但是没有相关的xml属性来修改 所以我们可以自定义tabs 
效果如下图 可以看见 没有了中间的空隙 

 

我们用单选按钮来实现tabs 


1、看布局文件 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background = "#d7d7d7">  
  7.     <LinearLayout android:orientation="vertical"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent">  
  10.         <FrameLayout android:id="@android:id/tabcontent"  
  11.             android:layout_width="fill_parent"  
  12.             android:layout_height="wrap_content"  
  13.             android:layout_weight = "1">           
  14.         </FrameLayout>      
  15.           
  16.         <TabWidget android:id="@android:id/tabs"  
  17.             android:layout_width="fill_parent"  
  18.             android:layout_height="wrap_content"  
  19.             android:visibility = "gone"/>  
  20.          <RadioGroup android:gravity="right"   
  21.                     android:layout_gravity="bottom"   
  22.                     android:orientation="horizontal"   
  23.                     android:id="@+id/main_radio"                       
  24.                     android:layout_width="fill_parent"   
  25.                     android:layout_height="wrap_content">  
  26.               
  27.             <RadioButton   
  28.                 android:id="@+id/radio_contact"   
  29.                 android:layout_marginTop="2.0dip"  
  30.                 android:background = "@drawable/tabcontact"  
  31.                 android:layout_width="wrap_content"   
  32.                 android:layout_height="fill_parent"  
  33.                 android:button= "@null"        
  34.                 />  
  35.             <RadioButton android:checked="true"                 
  36.                 android:id="@+id/radio_session"   
  37.                 android:layout_marginTop="2.0dip"  
  38.                 android:background = "@drawable/tabsession"  
  39.                 android:layout_width="wrap_content"   
  40.                 android:layout_height="fill_parent"  
  41.                 android:button= "@null"/>    
  42.             <RadioButton   
  43.                 android:id="@+id/radio_setting"   
  44.                 android:layout_marginTop="2.0dip"  
  45.                 android:background = "@drawable/tabsetting"  
  46.                 android:layout_width="wrap_content"   
  47.                 android:layout_height="fill_parent"  
  48.                 android:button= "@null"  
  49.                 />                                 
  50.         </RadioGroup>           
  51.     </LinearLayout>         
  52. </TabHost>  


关键 在于 TabWidget里面 
Java代码  收藏代码
  1. android:visibility = "gone"  


2、代码 

主要是三个方法: 

初始化Tabhost 
Java代码  收藏代码
  1. private void initTabHost() {  
  2.         mTabHost = getTabHost();  
  3.         mTabHost.addTab(mTabHost.newTabSpec("tab session").setIndicator(  
  4.                 "").setContent(new Intent(this,SessionListActivity.class)));  
  5.         mTabHost.addTab(mTabHost.newTabSpec("tab contact").setIndicator(  
  6.                 "").setContent(new Intent(this,ContactListActivity.class)));  
  7.         mTabHost.addTab(mTabHost.newTabSpec("tab setting").setIndicator(  
  8.                 "").setContent(new Intent(this,UserSettingActivitiy.class)));  
  9.           
  10.         mTabHost.setCurrentTabByTag(_contactListTag);  
  11.         mTabHost.setCurrentTabByTag(_settingTag);  
  12.         mTabHost.setCurrentTabByTag(_sessionListTag);  
  13.           
  14.     }  


初始化RadioButton 
Java代码  收藏代码
  1. private void initTabWidget() {  
  2.          ((RadioButton) findViewById(R.id.radio_session)).setOnCheckedChangeListener(this);  
  3.          ((RadioButton) findViewById(R.id.radio_contact)).setOnCheckedChangeListener(this);  
  4.          ((RadioButton) findViewById(R.id.radio_setting)).setOnCheckedChangeListener(this);  
  5.     }  


设置切换事件
  
Java代码  收藏代码
  1. @Override  
  2.    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  3.        if (isChecked) {  
  4.            switch (buttonView.getId()) {  
  5.            case R.id.radio_contact:  
  6.                this.mTabHost.setCurrentTabByTag(_contactListTag);  
  7.                break;  
  8.            case R.id.radio_session:  
  9.                this.mTabHost.setCurrentTabByTag(_sessionListTag);  
  10.                break;  
  11.            case R.id.radio_setting:  
  12.             this.mTabHost.setCurrentTabByTag(_settingTag);  
  13.                break;              
  14.            }  
  15.        }  
  16.    }  



1.  由于TabWidget被隐藏,所以相关的事件也会无效,这里取巧用RadioGroup与RadioButton的特性来处理切换,然后监听事件调用setCurrentTabByTag来切换Activity。 
2.  注意即使TabWidget被隐藏,也要为其设置indicator,否则会保持。
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics