【Android开发笔记】底部菜单栏 FragmentTabHost
公司项目,需求本来是按照谷歌官方指南写的,菜单栏设计成在导航栏下方
结果呢,审评时,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的更多相关文章
- 【转】Android开发笔记(序)写在前面的目录
原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...
- Android开发笔记(一百三十四)协调布局CoordinatorLayout
协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...
- [置顶] Android开发笔记(成长轨迹)
分类: 开发学习笔记2013-06-21 09:44 26043人阅读 评论(5) 收藏 Android开发笔记 1.控制台输出:called unimplemented OpenGL ES API ...
- Android开发笔记:打包数据库
对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...
- Android开发笔记--hello world 和目录结构
原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...
- [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明
接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...
- [APP] Android 开发笔记 002-命令行创建默认项目结构说明
接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.
- Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计
Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...
- 【转】Android开发笔记——圆角和边框们
原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...
- 《ArcGIS Runtime SDK for Android开发笔记》
开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...
随机推荐
- 17.[CVE-2017-12615]Tomcat任意文件上传漏洞
[CVE-2017-12615] Tomcat任意文件上传漏洞 首先先贴出wooyun上的一个案例:http://wooyun.jozxing.cc/static/bugs/wooyun-2015-0 ...
- android build system resource links
总体结构,参见这里:http://www.jayway.com/2012/10/24/a-practical-approach-to-the-aosp-build-system/ 一般应用的Andro ...
- 3、jquery_动态创建元素
动态创建元素:$('<b>javier</b>') $('#Button1').append($('<b>javier</b>')) 等价于 $($( ...
- 微信小程序iPhone X空白兼容
开局一张图…… 看看这空白的地方多丑 ~ 接下来就是见证奇迹的时刻(上代码) //app.js App({ onLaunch: function (ops) { if (ops.scene == 10 ...
- 谈谈Java异常处理这件事儿
此文已由作者谢蕾授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言 我们对于"异常处理"这个词并不陌生,众多框架和库在异常处理方面都提供了便利,但是对于 ...
- java IO流部分知识点
IO流部分 IO流常用的有:字符流.字节流.缓冲流.序列化流.RandomAccessFile类等 1.字节流 FileInputStream/FileOutputStream BufferedInp ...
- WampServer的安装
首先安装好Microsoft Visual C++ 然后再安装WampServer 安装过程很简单 错误解决 运行后为黄色图标(成功运行应该为绿色图标) 解决办法: 1.80端口是否被占用 你的80端 ...
- 架构师分享 Docker 新手入门完全指南
来源:架构师小秘圈 ID:seexmq Docker 最初 dotCloud 公司内部的一个业余项目 Docker 基于 Go 语言 Docker 项目的目标是实现轻量级的操作系统虚拟化解决方案 Do ...
- PAT甲级——1097 Deduplication on a Linked List (链表)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...
- BZOJ 3796 Mushroom追妹纸 哈希+二分(+KMP)
先把两个串能匹配模式串的位置找出来,然后标记为$1$(标记在开头或末尾都行),然后对标记数组求一个前缀和,这样可以快速查到区间内是否有完整的一个模式串. 然后二分子串(答案)的长度,每次把长度为$md ...