上一个项目已经做完了,这周基本上没事,所以整理了下以前的项目,想把一些通用的部分封装起来,这样以后遇到相似的项目就不用重复发明轮子了,也节省了开发效率。今天把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实现顶部滑动栏,就像网易新闻的顶部滑动栏我相信也是只缓存了一个界面,切换的时候是从数据库加载的,所以第二次滑动加载会比较快。
相关推荐:
SEO单页网站:助力企业在线营销的一站式解决方案,湖南视频网站优化方式
如何利用AI生成高质量文章,提升写作效率与创意?
优化标题:如何让你的文章更具吸引力与点击力,整站网站优化解决方案
自动写文章的AI,提升效率的创作利器
好用的人工智能AI软件推荐,让你的生活更智能!
seo黑帽是什么,列举几种seo黑帽行为 ,穿老款的ai丢人吗
ChatGPT为什么网址打不开?原因分析与解决方法,文档审核ai
SEO深度解析:如何通过深度优化提升网站排名,带来流量和转化,咸宁网站建设大概费用
seo网站是什么找行者SEO,seo分析网站 ,ai图文梅花
ChatGPT打开后空白:如何解决这个困扰并高效使用AI助手,ai四声怎么写
ChatGPT登录界面都不显示了?可能是这些原因导致的!,上海小学ai智能课
《*采集站:带你领略全球最全*资源的宝藏平台》,seo优化易下拉瞧瞧
AI免费免登录:轻松体验人工智能的魔力,无需繁琐注册,华为AI音箱2音质如何
AI一键生成文章网页版,让内容创作更简单高效
SEO出超:如何通过精准优化实现网站流量大爆发,营销推广方式联系f火15星
ChatGPT暂时不可用?如何高效应对并寻找最佳替代方案!,logo ai教程视频
SEO优化排名原理解析:如何提高网站排名,实现精准流量获取,奥迪ai售价
主流seo是什么,seo是什么推广网站 ,AI心理师
SEO提高:如何通过精准优化让网站流量翻倍,优化排名seo加盟费用
seo的推广工具,seo推广软件哪个好 ,国内ai写作论文怎么样
SEO优化大全:让你的网站排名轻松破局,精准引流更高效!,274357524ai
SEO做好,企业网站流量翻倍的关键,seo白帽技术有哪些
OpenAI无法验证支付方式?解决方案与常见问题解析,你好月光ai
AI一键生成文章免费版:颠覆写作新体验
seo网赚什么意思,网站seo赚钱 ,ai打不开ai
seo需要懂什么源码,seo需要懂什么源码技术 ,ai 纤维
优化平台:让数字化转型更简单、更高效,莆田谷歌seo品牌排行
ChatGPT怎么打不开了?解决办法,轻松恢复畅通无阻!,ai订酒店ai对话
SEO优化需要花钱吗?从零起步,如何让SEO成为企业的“隐形财富”,字体如何往ai里面倒
AI缩写文本:助力智能生活的革新力量,ai智能写作生成神器下载
SEO薪资这些,你也能月入过万!,天水网站建设公司
SEM做得好可以取代SEO吗?浅析两者的异同与未来趋势,王道ai
未来写作新方式原创AI文章的无限可能
ChatGPT-深度学习与自然语言处理的革命性突破,金华ai视觉锁螺丝机
ChatGPT免费订阅的使用限制:其潜力与挑战,ai辅助线无法对齐画板
seo诊断什么意思,seo诊断a5 ,约瑟夫ai
SEO北京:数字时代,企业成功的关键,湖南网站建设湖南岚鸿
ChatGPT支持多种语言输入输出,让全球资讯触手可及,联想拯救者的ai写作
主题导航-引领互联网世界的智慧之路,大渡口网站建设方案
seo站内优化包括什么营销,seo站内优化操作流程 ,ai 玻璃图标
二级泛站群,zblog二级泛站群 ,李宗盛ai
打造内容创作新时代:有言AI生成助力创作者释放灵感
SEO找词:如何精准找到高效关键词,提升排名和流量,河源网站优化平台
seo站内链接有什么作用,seo中网站内链的作用 ,781900ai
ChatGPT为什么打不开?背后原因与解决方案,慧ai写作
线上AI写作免费一键生成,轻松提升写作效率,解放创作思维
为什么网站要做seo,网站做seo的目的是什么 ,ai初选
seo运营经理是什么,seo和运营的区别 ,皖妍ai宁慕晴o
seo相当于什么职业,seo相当于什么职业类别 ,usatisfy ai
SEO怎么排名?这5大技巧,轻松提升网站排名,人人都可ai