当前位置: 移动技术网 > IT编程>移动开发>Android > Android仿微信底部菜单栏功能显示未读消息数量

Android仿微信底部菜单栏功能显示未读消息数量

2019年07月24日  | 移动技术网IT编程  | 我要评论

中秋祝福的话,陆丰租房,别再犹豫下载

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


以后使用的时候就可以换成自己项目的图片和字体了,主框架不用变哈哈,
首先是要布局layout下xml文件 main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<tabhost xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@android:id/tabhost" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" > 
 
  <linearlayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/bg_gray" 
    android:orientation="vertical" > 
 
    <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:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.0" 
      android:visibility="gone" /> 
 
    <framelayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
 
      <radiogroup 
        android:id="@+id/main_tab_group" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom" 
        android:background="@drawable/bottom1" 
        android:gravity="bottom" 
        android:orientation="horizontal" 
         > 
 
        <radiobutton 
          android:id="@+id/main_tab_addexam" 
          style="@style/mmtabbutton" 
          android:layout_weight="1.0"   
          android:drawabletop="@drawable/bg_checkbox_icon_menu_0" 
          android:text="添加考试" /> 
 
        <radiobutton 
          android:id="@+id/main_tab_myexam" 
          style="@style/mmtabbutton" 
          android:layout_weight="1.0" 
          android:checked="true" 
          android:drawabletop="@drawable/bg_checkbox_icon_menu_1" 
          android:text="我的考试" /> 
 
        <radiobutton 
          android:id="@+id/main_tab_message" 
          style="@style/mmtabbutton" 
          android:layout_weight="1.0" 
          android:drawabletop="@drawable/bg_checkbox_icon_menu_2" 
          android:text="我的通知" /> 
 
        <radiobutton 
          android:id="@+id/main_tab_settings" 
          style="@style/mmtabbutton" 
          android:layout_weight="1.0"   
          android:drawabletop="@drawable/bg_checkbox_icon_menu_3" 
          android:text="设置" /> 
      </radiogroup> 
 
      <textview 
        android:id="@+id/main_tab_new_message" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal|top" 
        android:layout_marginleft="60dip" 
        android:layout_margintop="1dip" 
        android:background="@drawable/tips" 
        android:gravity="center" 
        android:text="1" 
        android:textcolor="#ffffff" 
        android:textsize="10sp" 
        android:visibility="gone" /> 
    </framelayout> 
  </linearlayout> 
 
</tabhost> 

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

<style name="mmtabbutton"> 
  <item name="android:textsize">12.0dip</item> 
  <item name="android:gravity">center_horizontal</item> 
  <item name="android:background">@drawable/bg_checkbox_menus</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:textcolor">@color/white</item> 
  <item name="android:layout_weight">1.0</item> 
  <item name="android:paddingbottom">2.0dip</item> 
  <item name="android:paddingtop">2.0dip</item> 
</style> 

在drawable下bg_checkbox_menus.xml

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
  <item  
  android:state_checked="false"  
  android:drawable="@drawable/mm_trans" />  
  <item  
  android:state_checked="true"  
  android:drawable="@drawable/home_btn_bg" />  
</selector>  

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

package cn.com.karl.test;  
 
import android.app.tabactivity; 
import android.content.intent; 
import android.os.bundle; 
import android.view.view; 
import android.view.window; 
import android.widget.radiogroup; 
import android.widget.radiogroup.oncheckedchangelistener; 
import android.widget.tabhost; 
import android.widget.textview; 
 
public class mainactivity extends tabactivity { 
  /** called when the activity is first created. */ 
  private tabhost tabhost; 
  private textview main_tab_new_message; 
   
  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    this.requestwindowfeature(window.feature_no_title); 
    setcontentview(r.layout.main); 
     
    main_tab_new_message=(textview) findviewbyid(r.id.main_tab_new_message); 
    main_tab_new_message.setvisibility(view.visible); 
    main_tab_new_message.settext("10"); 
     
    tabhost=this.gettabhost(); 
    tabhost.tabspec spec; 
    intent intent; 
 
    intent=new intent().setclass(this, addexamactivity.class); 
    spec=tabhost.newtabspec("添加考试").setindicator("添加考试").setcontent(intent); 
    tabhost.addtab(spec); 
     
    intent=new intent().setclass(this,myexamactivity.class); 
    spec=tabhost.newtabspec("我的考试").setindicator("我的考试").setcontent(intent); 
    tabhost.addtab(spec); 
     
    intent=new intent().setclass(this, mymessageactivity.class); 
    spec=tabhost.newtabspec("我的通知").setindicator("我的通知").setcontent(intent); 
    tabhost.addtab(spec); 
     
    
    intent=new intent().setclass(this, settingactivity.class); 
    spec=tabhost.newtabspec("设置").setindicator("设置").setcontent(intent); 
    tabhost.addtab(spec); 
    tabhost.setcurrenttab(1); 
     
    radiogroup radiogroup=(radiogroup) this.findviewbyid(r.id.main_tab_group); 
    radiogroup.setoncheckedchangelistener(new oncheckedchangelistener() { 
       
      @override 
      public void oncheckedchanged(radiogroup group, int checkedid) { 
        // todo auto-generated method stub 
        switch (checkedid) { 
        case r.id.main_tab_addexam://添加考试 
          tabhost.setcurrenttabbytag("添加考试"); 
          break; 
        case r.id.main_tab_myexam://我的考试 
          tabhost.setcurrenttabbytag("我的考试"); 
          break; 
        case r.id.main_tab_message://我的通知 
          tabhost.setcurrenttabbytag("我的通知"); 
          break; 
        case r.id.main_tab_settings://设置 
          tabhost.setcurrenttabbytag("设置"); 
          break; 
        default: 
          //tabhost.setcurrenttabbytag("我的考试"); 
          break; 
        } 
      } 
    }); 
  } 
   
   
} 

这样就完成了,主要还是使用了tabhost完成,tabhost有缓存机制这四个界面都会缓存到内存中,这样即有利也有弊,有利是因为切换的时候不用在重新加载了,有弊是因为缓存四个界面会耗费内存较多一些。如果只想缓存一个界面,大家可以参考这篇文章:android项目实战之仿网易顶部导航栏效果,使用activitygroup实现顶部滑动栏,就像网易新闻的顶部滑动栏我相信也是只缓存了一个界面,切换的时候是从数据库加载的,所以第二次滑动加载会比较快。

本文源码下载地址:

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网