介绍

现在绝大多数主流的应用主界面,都会包含一个底部菜单,就拿腾讯的QQ与微信来说,看起来是这样的

 《---我是底部菜单

原理

在很久以前,可以通过TabActivity实现相关功能,自从Fragment出来后,就被抛弃了。

原理也很简单

1、底部菜单通过自定义RadioGroup实现,通过setOnCheckedChangeListener监听切换内容。

2、内容切换,可以使用ViewPager(可以实现直接滑动切换),TabHost,FragmentManager来实现。、

PS:类似的,这样也可以通过HorizontalScrollView+ViewPager+RadioGroup实现类似网易新闻的顶部栏目效果(或者ViewPageIndicator),通过ScrollView.scrollTo((int)RadioButton.getLeft())来自动滑动到当前选择项,有空再写篇文章。

实现

在几种组合搭配来看,我比较喜欢使用Fragment+Tabhost+RadioGroup搭配实现。

首先上首页布局代码activity_main.xml,注意加粗id

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent" >
  6.  
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:background="#FFFFFFFF"
  11. android:orientation="vertical" >
  12.  
  13. <FrameLayout
  14. android:layout_width="fill_parent"
  15. android:layout_height="fill_parent"
  16. android:layout_weight="1" >
  17.  
  18. <FrameLayout
  19. android:id="@android:id/tabcontent"
  20. android:layout_width="match_parent"
  21. android:layout_height="fill_parent" >
  22.  
  23. <fragment
  24. android:id="@+id/fragment_main"
  25. android:name="com.example.tabmenu.MainFragment"
  26. android:layout_width="fill_parent"
  27. android:layout_height="fill_parent" />
  28.  
  29. <fragment
  30. android:id="@+id/fragment_mycenter"
  31. android:name="com.example.tabmenu.MyCenterFragment"
  32. android:layout_width="fill_parent"
  33. android:layout_height="fill_parent" />
  34.  
  35. <fragment
  36. android:id="@+id/fragment_search"
  37. android:name="com.example.tabmenu.SearchFragment"
  38. android:layout_width="fill_parent"
  39. android:layout_height="fill_parent" />
  40. </FrameLayout>
  41. </FrameLayout>
  42.  
  43. <TabWidget
  44. android:id="@android:id/tabs"
  45. android:layout_width="fill_parent"
  46. android:layout_height="wrap_content"
  47. android:layout_weight="0.0"
  48. android:visibility="gone" />

  49.      <!-- 我只是一条线 -->
  50. <LinearLayout
  51. android:layout_width="fill_parent"
  52. android:layout_height="3dp"
  53. android:background="@drawable/fbutton_color_emerald" >
  54. </LinearLayout>
  55.  
  56. <RadioGroup
  57. android:id="@+id/radiogroup"
  58. android:layout_width="fill_parent"
  59. android:layout_height="wrap_content"
  60. android:layout_gravity="bottom"
  61. android:background="@drawable/fbutton_color_turquoise"
  62. android:gravity="center_vertical"
  63. android:orientation="horizontal" >
  64.  
  65. <!-- android:background="@drawable/bk_footer" -->
  66.  
  67. <RadioButton
  68. android:id="@+id/radio_search"
  69. style="@style/main_tab_bottom"
  70. android:layout_weight="1"
  71. android:background="@drawable/footer_itembg_selector"
  72. android:drawableTop="@drawable/footer_search_selector"
  73. android:text="搜索" />
  74.  
  75. <RadioButton
  76. android:id="@+id/radio_main"
  77. style="@style/main_tab_bottom"
  78. android:layout_weight="1"
  79. android:background="@drawable/footer_itembg_selector"
  80. android:button="@null"
  81. android:checked="true"
  82. android:drawableTop="@drawable/footer_main_selector"
  83. android:text="首 页" />
  84.  
  85. <RadioButton
  86. android:id="@+id/radio_mycenter"
  87. style="@style/main_tab_bottom"
  88. android:layout_weight="1"
  89. android:background="@drawable/footer_itembg_selector"
  90. android:drawableTop="@drawable/footer_mycenter_selector"
  91. android:text="个人中心" />
  92. </RadioGroup>
  93. </LinearLayout>
  94.  
  95. </TabHost>

其中RadioButton的样式按照需要自己定义

  1. <style name="main_tab_bottom">
  2. <item name="android:textSize">10sp</item>
  3. <item name="android:textColor">#ffffffff</item>
  4. <item name="android:ellipsize">marquee</item>
  5. <item name="android:gravity">center_horizontal</item>
  6. <item name="android:background">#00000000</item>
  7. <item name="android:paddingTop">2dp</item>
  8. <item name="android:layout_width">fill_parent</item>
  9. <item name="android:layout_height">wrap_content</item>
  10. <item name="android:button">@null</item>
  11. <item name="android:singleLine">true</item>
  12. <item name="android:drawablePadding">2dp</item>
  13. <item name="android:layout_weight">1.0</item>
  14. </style>

MainActivity代码

  1. package com.example.tabmenu;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.view.Window;
  8. import android.view.animation.AnimationUtils;
  9. import android.widget.RadioGroup;
  10. import android.widget.RadioGroup.OnCheckedChangeListener;
  11. import android.widget.TabHost;
  12.  
  13. public class MainActivity extends ActionBarActivity {
  14. // tab用参数
  15. private TabHost tabHost;
  16. private RadioGroup radiogroup;
  17. private int menuid;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
  24. tabHost = (TabHost) findViewById(android.R.id.tabhost);
  25. tabHost.setup();
  26. tabHost.addTab(tabHost.newTabSpec("main").setIndicator("main")
  27. .setContent(R.id.fragment_main));
  28. tabHost.addTab(tabHost.newTabSpec("mycenter").setIndicator("mycenter")
  29. .setContent(R.id.fragment_mycenter));
  30. tabHost.addTab(tabHost.newTabSpec("search").setIndicator("search")
  31. .setContent(R.id.fragment_search));
  32. radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  33.  
  34. @Override
  35. public void onCheckedChanged(RadioGroup group, int checkedId) {
  36. menuid = checkedId;
  37. int currentTab = tabHost.getCurrentTab();
  38. switch (checkedId) {
  39. case R.id.radio_main:
  40. tabHost.setCurrentTabByTag("main");
  41. //如果需要动画效果就使用
  42. //setCurrentTabWithAnim(currentTab, 0, "main");
  43. getSupportActionBar().setTitle("首页");
  44. break;
  45. case R.id.radio_mycenter:
  46. //tabHost.setCurrentTabByTag("mycenter");
  47. setCurrentTabWithAnim(currentTab, 1, "mycenter");
  48. getSupportActionBar().setTitle("个人中心");
  49.  
  50. break;
  51. case R.id.radio_search:
  52. tabHost.setCurrentTabByTag("search");
  53. getSupportActionBar().setTitle("搜索");
  54. }
  55. // 刷新actionbar的menu
  56. getWindow().invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
  57. }
  58. });
  59.  
  60. }
  61.  
  62. @Override
  63. public boolean onCreateOptionsMenu(Menu menu) {
  64. // Inflate the menu; this adds items to the action bar if it is present.
  65.  
  66. switch (menuid) {
  67. case R.id.radio_main:
  68. getMenuInflater().inflate(R.menu.main, menu);
  69. break;
  70. case R.id.radio_mycenter:
  71. menu.clear();
  72. break;
  73. case R.id.radio_search:
  74. menu.clear();
  75. break;
  76. }
  77. return true;
  78. }
  79.  
  80. @Override
  81. public boolean onOptionsItemSelected(MenuItem item) {
  82. // Handle action bar item clicks here. The action bar will
  83. // automatically handle clicks on the Home/Up button, so long
  84. // as you specify a parent activity in AndroidManifest.xml.
  85. int id = item.getItemId();
  86. if (id == R.id.action_settings) {
  87. return true;
  88. }
  89. return super.onOptionsItemSelected(item);
  90. }
  91.  
  92. // 这个方法是关键,用来判断动画滑动的方向
  93. private void setCurrentTabWithAnim(int now, int next, String tag) {
  94. if (now > next) {
  95. tabHost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
  96. tabHost.setCurrentTabByTag(tag);
  97. tabHost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
  98. } else {
  99. tabHost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
  100. tabHost.setCurrentTabByTag(tag);
  101. tabHost.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
  102. }
  103. }
  104. }

最后demo截图,其他文件件demo

demo下载地址:

链接:http://pan.baidu.com/s/1dbuKA 密码:84iw

参考:

http://blog.csdn.net/loongggdroid/article/details/9469935

http://blog.csdn.net/eyebrows_cs/article/details/8145913

http://www.cnblogs.com/over140/archive/2011/03/02/1968042.html

Android应用主界面底部菜单实现的更多相关文章

  1. Xamarin.Android 利用Fragment实现底部菜单

    效果图: 第一步:添加引用 引用 Crosslight.Xamarin.Android.Support.v7.AppCompat 这个包. 第二步:绘制Main和Fragment界面 fg_home. ...

  2. Android自定义控件系列(四)—底部菜单(下)

    转载请注明出处:http://www.cnblogs.com/landptf/p/6290862.html 在app中经常会用到底部菜单的控件,每次都需要写好多代码,今天我们用到了前几篇博客里的控件来 ...

  3. [模拟Android微信]主界面

    首先看很像模仿: 走出来: 实现过程: 依赖类库:actionbarsherlock 用actionbarsherlock来实现顶部的搜索的效果. tab用的是Viewpaper实现的. 详细细节: ...

  4. ViewPager + Fragment 实现主界面底部导航栏

    1. 四个类似的Frament布局 tab_main_fragment.xml <LinearLayout xmlns:android="http://schemas.android. ...

  5. android笔记---主界面(二)自定义actionbar环境的配置

    第一步,添加java文件 第二步,添加actionbar的item文件 是个选择器,点中状态和选中状态 <?xml version="1.0" encoding=" ...

  6. android笔记---主界面(一)

    <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="htt ...

  7. 【Android】5.0 第5章 常用基本控件--本章示例主界面

    分类:C#.Android.VS2015: 创建日期:2016-02-06 这一章主要介绍Android简单控件的基本用法.本章源程序共有9个示例,这些示例都在同一个项目中. 项目名:ch05demo ...

  8. Android学习系列(22)--App主界面比较

    本文算是一篇漫谈,谈一谈当前几个流行应用的主界面布局,找个经典的布局我们自己也来实现一个.不是为了追求到底有多难,而是为了明白我们确实需要这么做. 走个题,android的UI差异化市场依然很大,依然 ...

  9. Android ActionBar应用实战,高仿微信主界面的设计

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/26365683 经过前面两篇文章的学习,我想大家对ActionBar都已经有一个相对 ...

随机推荐

  1. 疯狂java讲义——初始化块

  2. jquery json遍历和动态绑定事件

    <div id='tmpselectorList' style='border: 1px solid grey;max-height: 150px;position:absolute;text- ...

  3. Win7无法使用VPN的原因与解决方法(一)

    如果Windows 7不是通过正常安装途径的话,像Ghost错误.系统环境改变等,都有可能导致无法使用VPN,而且由于原因不同,给出的提示也不尽相同.实际上,万变不离其宗, VPN是要依靠Window ...

  4. HDU 4022 Bombing(stl,map,multiset,iterater遍历)

    题目 参考了     1     2 #define _CRT_SECURE_NO_WARNINGS //用的是STL中的map 和 multiset 来做的,代码写起来比较简洁,也比较好容易理解. ...

  5. jxl.dll操作总结

    1)Jxl是一个开源的Java Excel API项目,通过Jxl,Java可以很方便的操作微软的Excel文档.除了Jxl之外,还有Apache的一个POI项目,也可以操作Excel,两者相比之下: ...

  6. POJ 1488

    #include <iostream> #include <string> using namespace std; int main() { string s; int i; ...

  7. Ado.Net要知道的东西

    什么是ADO.NET? ADO.NET就是一组类库,这组类库可以让我们通过程序的方式访问数据库,就像System.IO下的类用类操作文件一样,System.Data.这组类是用来操作数据库(不光是MS ...

  8. Java获取最后插入MySQL记录的自增ID值方法

    方法一: String sql = "INSERT INTO users (username,password,email) VALUES (?,?,?);"; PreparedS ...

  9. 传说中的WCF(2):服务协定的那些事儿

    上一篇文章中,我们抛出了N个问题:WCF到底难不难学?复杂吗?如果复杂,可以化繁为简吗? 其实,这些问题的答案全取决于你的心态,都说“态度决定一切”,这句话,不知道各位信不信,反正我是信了.首先,敢于 ...

  10. HADOOP NAMENODE对Image和edits的处理

    1.SNN CheckPoint的处理流程 配置中配置做CheckPoint的两个条件,一个是文件大小editlog大于多大就做,另一个是时间维度,多长时间做一次. (1)SNN首先检查是否需要进行c ...