公司项目,需求本来是按照谷歌官方指南写的,菜单栏设计成在导航栏下方

结果呢,审评时,BOSS为了和iOS统一,改成了底部菜单栏(标准结局),我只能呵呵呵呵呵呵呵

查了查资料发现实现底部菜单栏用的是FragmentTabHost,下面记录下具体如何实现的

首先,假设我有3个菜单栏,对应3个Fragment:FragmentA、FragmentB、FragmentC

这3个Fragment将由一个Activity控制:TabHostActivity

TabHostActivity对应的xml文件:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <FrameLayout
android:id="@+id/real_tab_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/> <RadioGroup
android:id="@+id/radio_tab_bottom_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#111111"
android:orientation="horizontal"> <RadioButton
android:id="@+id/tab_patient_list"
style="@style/tab_rb_style"
android:checked="true"
android:text="@string/tab_patient_list"/> <RadioButton
android:id="@+id/tab_message"
style="@style/tab_rb_style"
android:text="@string/tab_message"/> <RadioButton
android:id="@+id/tab_settings"
style="@style/tab_rb_style"
android:text="@string/tab_settings"/> </RadioGroup> <android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
</android.support.v4.app.FragmentTabHost> </LinearLayout>

其中,id为real_tab_content的fragment存放用于显示的Fragment。

TabHostActivity:

 public class TabHostActivity extends FragmentActivity{
private FragmentTabHost mFragmentTabHost;
private RadioGroup mTabRg; private final Class[] fragments = {
FragmentA.class,
FragmentB.class,
FragmentC.class
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_menu); initView();
} private void initView() {
// 构建TabHost
mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// getSupportFragmentManager():
// return the fragmentManager for interacting with fragments associated with this activity.
// setup(Context context, FragmentManager manager, int containerId)
mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.real_tab_content); int count = fragments.length;
for (int i = 0; i < count; i++) {
// A tab has a tab indicator, content, and a tag that is used to keep track of it.
// newTabSpec(String tag):
// Get a new TabHost.TabSpec associated with this tab host.
TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(i + "").setIndicator(i + "");
mFragmentTabHost.addTab(tabSpec, fragments[i], null);
} mTabRg = (RadioGroup) findViewById(R.id.radio_tab_bottom_menu); mTabRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.tab_patient_list:
mFragmentTabHost.setCurrentTab(0);
break; case R.id.tab_message:
mFragmentTabHost.setCurrentTab(1);
break; case R.id.tab_settings:
mFragmentTabHost.setCurrentTab(2);
break; default:
break;
}
}
});
} }

FragmentA:

 public class FragmentA extends Fragment {
private View rootView; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment_settings, container, false);
}
     
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null) {
parent.removeView(rootView);
} return rootView;
}
}

FragmentB、C同理。

 

【Android开发笔记】底部菜单栏 FragmentTabHost的更多相关文章

  1. 【转】Android开发笔记(序)写在前面的目录

    原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...

  2. Android开发笔记(一百三十四)协调布局CoordinatorLayout

    协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...

  3. [置顶] Android开发笔记(成长轨迹)

    分类: 开发学习笔记2013-06-21 09:44 26043人阅读 评论(5) 收藏 Android开发笔记 1.控制台输出:called unimplemented OpenGL ES API ...

  4. Android开发笔记:打包数据库

    对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...

  5. Android开发笔记--hello world 和目录结构

    原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...

  6. [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明

    接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...

  7. [APP] Android 开发笔记 002-命令行创建默认项目结构说明

    接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.

  8. Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计

    Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...

  9. 【转】Android开发笔记——圆角和边框们

    原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...

  10. 《ArcGIS Runtime SDK for Android开发笔记》

    开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...

随机推荐

  1. python的pip 安装

    python的pip 安装 python有很多好用的包,但是需要的时候一一安装实在是麻烦,还好有pip这么好用的安装工具.所以第一步是安装pip,然后其它软件都so easy! 文章来源:https: ...

  2. cargo实现自动化部署远程jetty容器(非安全模式)

    cargo实现自动化部署应用至远程jetty容器 (非安全模式) 一.准备: WAR包:Deployer Web application for the Jetty remote containers ...

  3. bat实现监测计算机无线连接,断网自动重启无线

    @echo off :Begin ping www.baidu.com if errorlevel 1 goto Reboot if errorlevel 0 goto Continue :Conti ...

  4. [java基础]short s1 = 1; s1 = s1 + 1;有什么错?short s1 = 1; s1 += 1;有什么错?

    为什么写这篇文章是因为搜到的答案里并没有阐明s1 = s1 + 1为什么就要转换为int类型. 由一下实验可知: public class test { public static void main ...

  5. Redis源码分析-底层数据结构盘点

    前段时间翻看了Redis的源代码(C语言版本,Git地址:https://github.com/antirez/redis), 过了一遍Redis数据结构,包括SDS.ADList.dict.ints ...

  6. Hadoop WordCount单词计数原理

    计算文件中出现每个单词的频数 输入结果按照字母顺序进行排序 编写WordCount.java 包含Mapper类和Reducer类 编译WordCount.java javac -classpath ...

  7. 基于XML的AOP配置

    创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...

  8. Python实现返回指定范围内的所有素数

    # 获取a, b范围的所有素数 def func(a, b): li = [] for i in range(a, b+1): for j in range(2, i): if i % j == 0: ...

  9. bzoj4200: [Noi2015]小园丁与老司机(可行流+dp)

    传送门 这该死的码农题…… 题解在这儿->这里 //minamoto #include<iostream> #include<cstdio> #include<cs ...

  10. ie9下网页设计兼容模式

    个人实践使用:ie9下使用低版本ie兼容模式,在网站第一个页面的<head>标签后使用<meta http-equiv="X-UA-Compatible" con ...