上一个项目已经做完了,这周基本上没事,所以整理了下以前的项目,想把一些通用的部分封装起来,这样以后遇到相似的项目就不用重复发明轮子了,也节省了开发效率。今天把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"
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有缓存机制这四个界面都会缓存到内存中,这样即有利也有弊,有利是因为切换的时候不用在重新加载了,有弊是因为缓存四个界面会耗费内存较多一些。如果只想缓存一个界面以后下一篇我会使用ActivityGroup实现顶部滑动栏,就像网易新闻的顶部滑动栏我相信也是只缓存了一个界面,切换的时候是从数据库加载的,所以第二次滑动加载会比较快。
最后奉上下载地址,如果有需要的希望大家自己去下载吧,有些代码存在本地时间长了我也会弄丢。[rar文件] android底部菜单栏demo
分享到:
相关推荐
本实例完成了App底部导航菜单自由滑动,仿微信主界面,可直接应用于项目开发。
(1)通过TabWidget实现 (2)隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏
做项目时候下了几个Android底部导航栏,共3个demo。第一个是仿QQ空间实现的demo,用popwindow实现弹窗效果;第二个是仿微信底部导航栏,能够左右滑动。第三个是基于groupradio实现的底部导航栏。经过测试三个导入...
底部菜单栏很重要,我看了一下很多应用软件都是用了底部菜单栏做。我这里使用了tabhost做了一种通用的(就是可以像微信那样显示未读消息数量的,虽然之前也做过但是layout下的xml写的太臃肿,这里去掉了很多不必要...
相关的链接: Android 底部菜单栏实现 最近刚看完ViewPager,就想到做这样一个Demo,当然也参考了高手们的实例里边的网格菜单,开始我打算用自定义的imgBtn,但是发现放在pager选项卡中不好排版,所以最好选了...
本demo适用于Android 初学者观看以及下载是一个完整的demo,是使用Android studio开发的,里面实现包括进入程序闪屏跳转、彷微信底部菜单栏的滑动、图片的轮播以及GridView实现九宫格的点击事件
众所周知,android的底部菜单栏太重要,平时项目一般都是需要用到的,但是网上关于这方面的demo做得太丑了,实在惨不忍睹,所以这里便用RadioGroup+Fragment的方式写了一个,顺便美化了一下,需要的可以看下。...
本文实例讲述了Android使用Activity实现从底部弹出菜单或窗口的方法。分享给大家供大家参考,具体如下: 这里使用activity实现弹出滑动窗口或菜单,主要是使用了一些设置activity的样式来实现弹出窗口和滑动效果,...
今天在github上看到个demo非常好,但是是android studio版的,研究了下不错,改为eclipse版,待用。http://blog.csdn.net/qq273681448/article/details/50541615
Android使用PopupWindow仿QQ底部菜单栏,仿百度地图选择视图。动画效果实现
android 底部顶部切换,有点类似微博的发现页,发现中间有顶部的切换,由于不是很清楚,这个整了很久才整出来的,当时也是网上各种找demo,不好找啊。
简单的PopMenuWindowDemo,实现点击按钮弹出底部菜单栏选择图片的来源(拍照or图库)
SlidingMenu实现侧滑栏菜单.rar SlidingMenu左右宽度可调节菜单.rar Test_Map.zip 一个ExpandableListView的例子,实现多级菜单分类展示.rar 上传百度地图的基本定位操作应用.rar 两个GPS导航定位源码.rar 以圆心...
适配Android6.0,使用popUpWindow实现带有从下至上弹出动画的底部菜单栏,通过点击其中的“打开相册”按钮可以从相册中选取一张图片显示到屏幕的ImageView上。