android底部菜单栏实现原理与代码

android底部菜单栏实现原理与代码,第1张

概述上一个项目已经做完了,这周基本上没事,所以整理了下以前的项目,想把一些通用的部分封装起来,这样以后遇到相似的项目就不用重复发明轮子了,也节省了开发效率。今天把demo贴出来一是方便以后自己查询,二是希望同 上一个项目已经做完了,这周基本上没事,所以整理了下以前的项目,想把一些通用的部分封装起来,这样以后遇到相似的项目就不用重复发明轮子了,也节省了开发效率。今天把demo贴出来一是方便以后自己查询,二是希望同时也能帮到大家。

底部菜单栏很重要,我看了一下很多应用软件都是用了底部菜单栏做。我这里使用了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"

androID:layout_weight="1.0"
androID:drawabletop="@drawable/bg_checkBox_icon_menu_0"
androID:text="添加考试" />
<Radiobutton
androID:ID="@+ID/main_tab_myExam"

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"

androID:layout_weight="1.0"
androID:drawabletop="@drawable/bg_checkBox_icon_menu_2"
androID:text="我的通知" />
<Radiobutton
androID:ID="@+ID/main_tab_settings"

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有缓存机制这四个界面都会缓存到内存中,这样即有利也有弊,有利是因为切换的时候不用在重新加载了,有弊是因为缓存四个界面会耗费内存较多一些。如果只想缓存一个界面以后下一篇我会使用ActivityGroup实现顶部滑动栏,就像网易新闻的顶部滑动栏我相信也是只缓存了一个界面,切换的时候是从数据库加载的,所以第二次滑动加载会比较快。 您可能感兴趣的文章:安卓(Android)实现3DTouch效果Android左右滑出菜单实例分析android popwindow实现左侧d出菜单层及PopupWindow主要方法介绍基于Android实现点击某个按钮让菜单选项从按钮周围指定位置d出Android ListView长按d出菜单二种实现方式示例Android界面设计(APP设计趋势 左侧隐藏菜单右边显示content)Android开发技巧之我的菜单我做主(自定义菜单)Android仿QQ空间底部菜单示例代码Android实现原生侧滑菜单的超简单方式Android实现类似3D Touch菜单功能 总结

以上是内存溢出为你收集整理的android底部菜单栏实现原理与代码全部内容,希望文章能够帮你解决android底部菜单栏实现原理与代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1142922.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存