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

android底部菜单栏实现原理与代码,由于上个项目已完成,这周正好闲着没事,所以就整理了下以前的项目,想把一些通用的部分封装起来,这样以后遇到相似的项目就不用重复发明轮子了,也节省了开发效率。今天爱站小编把demo贴出来,一是方便以后自己查询,二是希望同时也能帮到大家。

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

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

046c2b93fd8022fb7e63fc0e8e39fe11.png 

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

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

android:id="@android:id/tabhost"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@color/bg_gray"

android:orientation="vertical" >

android:id="@android:id/tabcontent"

android:layout_width="fill_parent"

android:layout_height="0.0dip"

android:layout_weight="1.0" />

android:id="@android:id/tabs"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="0.0"

android:visibility="gone" />

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

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"

>

android:id="@+id/main_tab_addExam"

style="@style/MMTabButton"

android:layout_weight="1.0"

android:drawableTop="@drawable/bg_checkbox_icon_menu_0"

android:text="添加考试" />

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="我的考试" />

android:id="@+id/main_tab_message"

style="@style/MMTabButton"

android:layout_weight="1.0"

android:drawableTop="@drawable/bg_checkbox_icon_menu_2"

android:text="我的通知" />

android:id="@+id/main_tab_settings"

style="@style/MMTabButton"

android:layout_weight="1.0"

android:drawableTop="@drawable/bg_checkbox_icon_menu_3"

android:text="设置" />

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" />

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

接下来是样式布局:

12.0dip

center_horizontal

@drawable/bg_checkbox_menus

fill_parent

wrap_content

@null

@color/white

1.0

2.0dip

2.0dip

在drawable下bg_checkbox_menus.xml

android:state_checked="false"

android:drawable="@drawable/mm_trans" />

android:state_checked="true"

android:drawable="@drawable/home_btn_bg" />

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

最后看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实现顶部滑动栏,就像网易新闻的顶部滑动栏我相信也是只缓存了一个界面,切换的时候是从数据库加载的,所以第二次滑动加载会比较快。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值