android Tabhost部件(二)

来源:互联网 发布:淘宝店铺数据异常 编辑:程序博客网 时间:2024/05/22 05:33


第一弹

底部菜单栏很重要,我看了一下很多应用软件都是用了底部菜单栏做。我这里使用了tabhost做了一种通用的(就是可以像微信那样显示未读消息数量的,虽然之前也做过但是layout下的xml写的太臃肿,这里去掉了很多不必要的层,个人看起来还是不错的,所以贴出来方便以后使用)。

          先看一下做出来之后的效果:

 

       以后使用的时候就可以换成自己项目的图片和字体了,主框架不用变哈哈,

     首先是要布局layout下xml文件 main.xml:

 

[html] view plaincopy
  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.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:background="@color/bg_gray"  
  11.         android:orientation="vertical" >  
  12.   
  13.         <FrameLayout  
  14.             android:id="@android:id/tabcontent"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="0.0dip"  
  17.             android:layout_weight="1.0" />  
  18.   
  19.         <TabWidget  
  20.             android:id="@android:id/tabs"  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:layout_weight="0.0"  
  24.             android:visibility="gone" />  
  25.   
  26.         <FrameLayout  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="wrap_content" >  
  29.   
  30.             <RadioGroup  
  31.                 android:id="@+id/main_tab_group"  
  32.                 android:layout_width="fill_parent"  
  33.                 android:layout_height="wrap_content"  
  34.                 android:layout_gravity="bottom"  
  35.                 android:background="@drawable/bottom1"  
  36.                 android:gravity="bottom"  
  37.                 android:orientation="horizontal"  
  38.                  >  
  39.   
  40.                 <RadioButton  
  41.                     android:id="@+id/main_tab_addExam"  
  42.                     style="@style/MMTabButton"  
  43.                     android:layout_weight="1.0"      
  44.                     android:drawableTop="@drawable/bg_checkbox_icon_menu_0"  
  45.                     android:text="添加考试" />  
  46.   
  47.                 <RadioButton  
  48.                     android:id="@+id/main_tab_myExam"  
  49.                     style="@style/MMTabButton"  
  50.                     android:layout_weight="1.0"  
  51.                     android:checked="true"  
  52.                     android:drawableTop="@drawable/bg_checkbox_icon_menu_1"  
  53.                     android:text="我的考试" />  
  54.   
  55.                 <RadioButton  
  56.                     android:id="@+id/main_tab_message"  
  57.                     style="@style/MMTabButton"  
  58.                     android:layout_weight="1.0"  
  59.                     android:drawableTop="@drawable/bg_checkbox_icon_menu_2"  
  60.                     android:text="我的通知" />  
  61.   
  62.                 <RadioButton  
  63.                     android:id="@+id/main_tab_settings"  
  64.                     style="@style/MMTabButton"  
  65.                     android:layout_weight="1.0"     
  66.                     android:drawableTop="@drawable/bg_checkbox_icon_menu_3"  
  67.                     android:text="设置" />  
  68.             </RadioGroup>  
  69.   
  70.             <TextView  
  71.                 android:id="@+id/main_tab_new_message"  
  72.                 android:layout_width="wrap_content"  
  73.                 android:layout_height="wrap_content"  
  74.                 android:layout_gravity="center_horizontal|top"  
  75.                 android:layout_marginLeft="60dip"  
  76.                 android:layout_marginTop="1dip"  
  77.                 android:background="@drawable/tips"  
  78.                 android:gravity="center"  
  79.                 android:text="1"  
  80.                 android:textColor="#ffffff"  
  81.                 android:textSize="10sp"  
  82.                 android:visibility="gone" />  
  83.         </FrameLayout>  
  84.     </LinearLayout>  
  85.   
  86. </TabHost>  

        在RadioGroup的外面加了一个FrameLayout,主要是为了使用TextView显示消息数量,这里是居中靠左60dip,可能你会问直接写死能支持多分辨率吗,这个我在320*480的手机上试过没问题的,因为dip是与设备无关的支持多分辨率,至于1280*800平板电脑这样的分辨率我就不能保证了,哈哈!

      接下来是样式布局:

[html] view plaincopy
  1. <style name="MMTabButton">  
  2.     <item name="android:textSize">12.0dip</item>  
  3.     <item name="android:gravity">center_horizontal</item>  
  4.     <item name="android:background">@drawable/bg_checkbox_menus</item>  
  5.     <item name="android:layout_width">fill_parent</item>  
  6.     <item name="android:layout_height">wrap_content</item>  
  7.     <item name="android:button">@null</item>  
  8.     <item name="android:textColor">@color/white</item>  
  9.     <item name="android:layout_weight">1.0</item>  
  10.     <item name="android:paddingBottom">2.0dip</item>  
  11.     <item name="android:paddingTop">2.0dip</item>  
  12. </style>  

       在drawable下bg_checkbox_menus.xml

   

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <item   
  4.     android:state_checked="false"   
  5.     android:drawable="@drawable/mm_trans" />   
  6.     <item   
  7.     android:state_checked="true"   
  8.     android:drawable="@drawable/home_btn_bg" />   
  9. </selector>   

      其他的那四个都合这个一样点击后图片换成亮色的,所以就不一一贴出来了。

     最后看MainActivity这个类:

[html] view plaincopy
  1. package cn.com.karl.test;  
  2.   
  3.   
  4.   
  5. import android.app.TabActivity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.Window;  
  10. import android.widget.RadioGroup;  
  11. import android.widget.RadioGroup.OnCheckedChangeListener;  
  12. import android.widget.TabHost;  
  13. import android.widget.TextView;  
  14.   
  15. public class MainActivity extends TabActivity {  
  16.     /** Called when the activity is first created. */  
  17.     private TabHost tabHost;  
  18.     private TextView main_tab_new_message;  
  19.       
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  24.         setContentView(R.layout.main);  
  25.           
  26.         main_tab_new_message=(TextView) findViewById(R.id.main_tab_new_message);  
  27.         main_tab_new_message.setVisibility(View.VISIBLE);  
  28.         main_tab_new_message.setText("10");  
  29.           
  30.         tabHost=this.getTabHost();  
  31.         TabHost.TabSpec spec;  
  32.         Intent intent;  
  33.   
  34.         intent=new Intent().setClass(this, AddExamActivity.class);  
  35.         spec=tabHost.newTabSpec("添加考试").setIndicator("添加考试").setContent(intent);  
  36.         tabHost.addTab(spec);  
  37.           
  38.         intent=new Intent().setClass(this,MyExamActivity.class);  
  39.         spec=tabHost.newTabSpec("我的考试").setIndicator("我的考试").setContent(intent);  
  40.         tabHost.addTab(spec);  
  41.           
  42.         intent=new Intent().setClass(this, MyMessageActivity.class);  
  43.         spec=tabHost.newTabSpec("我的通知").setIndicator("我的通知").setContent(intent);  
  44.         tabHost.addTab(spec);  
  45.           
  46.        
  47.         intent=new Intent().setClass(this, SettingActivity.class);  
  48.         spec=tabHost.newTabSpec("设置").setIndicator("设置").setContent(intent);  
  49.         tabHost.addTab(spec);  
  50.         tabHost.setCurrentTab(1);  
  51.           
  52.         RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group);  
  53.         radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  54.               
  55.             @Override  
  56.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  57.                 // TODO Auto-generated method stub  
  58.                 switch (checkedId) {  
  59.                 case R.id.main_tab_addExam://添加考试  
  60.                     tabHost.setCurrentTabByTag("添加考试");  
  61.                     break;  
  62.                 case R.id.main_tab_myExam://我的考试  
  63.                     tabHost.setCurrentTabByTag("我的考试");  
  64.                     break;  
  65.                 case R.id.main_tab_message://我的通知  
  66.                     tabHost.setCurrentTabByTag("我的通知");  
  67.                     break;  
  68.                 case R.id.main_tab_settings://设置  
  69.                     tabHost.setCurrentTabByTag("设置");  
  70.                     break;  
  71.                 default:  
  72.                     //tabHost.setCurrentTabByTag("我的考试");  
  73.                     break;  
  74.                 }  
  75.             }  
  76.         });  
  77.     }  
  78.       
  79.      
  80. }  

        这样就完成了,主要还是使用了tabhost完成,tabhost有缓存机制这四个界面都会缓存到内存中,这样即有利也有弊,有利是因为切换的时候不用在重新加载了,有弊是因为缓存四个界面会耗费内存较多一些。如果只想缓存一个界面以后下一篇我会使用ActivityGroup实现顶部滑动栏,就像网易新闻的顶部滑动栏我相信也是只缓存了一个界面,切换的时候是从数据库加载的,所以第二次滑动加载会比较快。

        最后奉上下载地址,如果有需要的希望大家自己去下载吧,有些代码存在本地时间长了我也会弄丢。[rar文件] android底部菜单栏demo

转载于:http://blog.csdn.net/yuzhiboyi/article/details/7782276

第二弹

  一、效果图

    

    红色部分是本文要实现的目标。

 

  二、实现

    maintabs.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android
="http://schemas.android.com/apk/res/android">
    
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
        
<FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" />
        
<TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" />
        
<RadioGroup android:gravity="center_vertical" android:layout_gravity="bottom" android:orientation="horizontal" android:id="@id/main_radio" android:background="@drawable/maintab_toolbar_bg" android:layout_width="fill_parent" android:layout_height="wrap_content">
            
<RadioButton   android:text="@string/main_home" android:checked="true" android:id="@+id/radio_button0" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_1_n" style="@style/main_tab_bottom" />
            
<RadioButton android:id="@+id/radio_button1" android:layout_marginTop="2.0dip" android:text="@string/main_news" android:drawableTop="@drawable/icon_2_n" style="@style/main_tab_bottom" />
            
<RadioButton android:id="@+id/radio_button2" android:layout_marginTop="2.0dip" android:text="@string/main_my_info" android:drawableTop="@drawable/icon_3_n" style="@style/main_tab_bottom" />
            
<RadioButton android:id="@+id/radio_button3" android:layout_marginTop="2.0dip" android:text="@string/menu_search" android:drawableTop="@drawable/icon_4_n" style="@style/main_tab_bottom" />
            
<RadioButton android:id="@+id/radio_button4" android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" style="@style/main_tab_bottom" />
        
</RadioGroup>
    
</LinearLayout>
</TabHost>
复制代码

    styles.xml

复制代码
    <style name="main_tab_bottom">
        
<item name="android:textSize">@dimen/bottom_tab_font_size</item>
        
<item name="android:textColor">#ffffffff</item>
        
<item name="android:ellipsize">marquee</item>
        
<item name="android:gravity">center_horizontal</item>
        
<item name="android:background">@drawable/home_btn_bg</item>
        
<item name="android:paddingTop">@dimen/bottom_tab_padding_up</item>
        
<item name="android:layout_width">fill_parent</item>
        
<item name="android:layout_height">wrap_content</item>
        
<item name="android:button">@null</item>
        
<item name="android:singleLine">true</item>
        
<item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item>
        
<item name="android:layout_weight">1.0</item>
    
</style>
复制代码

     home_btn_bg.xml

复制代码
        <selector
          
xmlns:android="http://schemas.android.com/apk/res/android">
            
<item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_s" />
            
<item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_s" />
            
<item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/home_btn_bg_d" />
            
<item android:drawable="@drawable/transparent" />
        
</selector>
复制代码

    代码说明:

        1.  需要注意的是他这里把TabWidget的Visibility设置成了gone!也就是默认难看的风格不见了:,取而代之的是5个带风格的单选按钮.

        2.  注意为单选按钮设置的style,其中最重要的是为其background设置了home_btn_bg.xml,也就是自定义了选中效果。

    Java文件

复制代码
public class MainTabActivity extends TabActivity implements
        OnCheckedChangeListener {

    
private TabHost mHost;
    
private Intent mMBlogIntent;
    
private Intent mMoreIntent;
    
private Intent mInfoIntent;
    
private Intent mSearchIntent;
    
private Intent mUserInfoIntent;

    @Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.maintabs);

        
// ~~~~~~~~~~~~ 初始化
        this.mMBlogIntent = new Intent(this, HomeListActivity.class);
        
this.mSearchIntent = new Intent(this, SearchSquareActivity.class);
        
this.mInfoIntent = new Intent(this, MessageGroup.class);
        
this.mUserInfoIntent = new Intent(this, MyInfoActivity.class);
        
this.mMoreIntent = new Intent(this, MoreItemsActivity.class);

        initRadios();
        
        setupIntent();
    }

    
/**
     * 初始化底部按钮
     
*/
    
private void initRadios() {
         ((RadioButton) findViewById(R.id.radio_button0)).setOnCheckedChangeListener(
this);
         ((RadioButton) findViewById(R.id.radio_button1)).setOnCheckedChangeListener(
this);
         ((RadioButton) findViewById(R.id.radio_button2)).setOnCheckedChangeListener(
this);
         ((RadioButton) findViewById(R.id.radio_button3)).setOnCheckedChangeListener(
this);
         ((RadioButton) findViewById(R.id.radio_button4)).setOnCheckedChangeListener(
this);
    }

    
/**
     * 切换模块
     
*/
    @Override
    
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        
if (isChecked) {
            
switch (buttonView.getId()) {
            
case R.id.radio_button0:
                
this.mHost.setCurrentTabByTag("mblog_tab");
                
break;
            
case R.id.radio_button1:
                
this.mHost.setCurrentTabByTag("message_tab");
                
break;
            
case R.id.radio_button2:
                
this.mHost.setCurrentTabByTag("userinfo_tab");
                
break;
            
case R.id.radio_button3:
                
this.mHost.setCurrentTabByTag("search_tab");
                
break;
            
case R.id.radio_button4:
                
this.mHost.setCurrentTabByTag("more_tab");
                
break;
            }
        }
    }

    
private void setupIntent() {
        
this.mHost = getTabHost();
        TabHost localTabHost 
= this.mHost;

        localTabHost.addTab(buildTabSpec(
"mblog_tab", R.string.main_home,
                R.drawable.icon_1_n, 
this.mMBlogIntent));

        localTabHost.addTab(buildTabSpec(
"message_tab", R.string.main_news,
                R.drawable.icon_2_n, 
this.mInfoIntent));

        localTabHost.addTab(buildTabSpec(
"userinfo_tab", R.string.main_my_info,
                R.drawable.icon_3_n, 
this.mUserInfoIntent));

        localTabHost.addTab(buildTabSpec(
"search_tab", R.string.menu_search,
                R.drawable.icon_4_n, 
this.mSearchIntent));

        localTabHost.addTab(buildTabSpec(
"more_tab", R.string.more,
                R.drawable.icon_5_n, 
this.mMoreIntent));

    }

    
private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
            
final Intent content) {
        
return this.mHost
                .newTabSpec(tag)
                .setIndicator(getString(resLabel),
                        getResources().getDrawable(resIcon))
                .setContent(content);
    }
复制代码

    代码说明

      1.  由于TabWidget被隐藏,所以相关的事件也会无效,这里取巧用RadioGroup与RadioButton的特性来处理切换,然后监听事件调用setCurrentTabByTag来切换Activity。

      2.  注意即使TabWidget被隐藏,也要为其设置indicator,否则会保持。

 

  三、总结

    在这之前如果要做这种效果我恐怕第一时间就会想到用ActivityGroup来做,主要是因为TabHost的TabWidget非常难看,用起来也不方便。其实从源码可以看出,TabActivity也是继承自ActivityGroup,这里结合了单选按钮和TabHost,各取其长


0 0
原创粉丝点击