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

结果呢,审评时,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同理。

 

最新文章

  1. PHP开发笔记:二维数组根据某一项来进行排序
  2. BIOS开启虚拟化
  3. ssh 协议执行repo sync 报错:Permission denied (publickey)
  4. Linux下守护进程初探
  5. Netty开发UDP协议
  6. 在线富文本编辑器FckEditor配置(.Net Framework 3.5)
  7. ARM的启动和中断向量表
  8. [转载]解决zabbix在configure时候遇到的问题(Ubuntu)
  9. 牛 JQuery视频笔记
  10. 大数据量场景下storm自定义分组与Hbase预分区完美结合大幅度节省内存空间
  11. ubuntu上安装apache2+mysql+php5-fpm(PHP5 - FastCGI Process Manager)
  12. CSS后代选择器“空格”和“&gt;”的使用辨析
  13. Vue (二) --- Vue对象提供的属性功能
  14. 特殊字符url编码以后再解码后出现错误(&amp;not , &amp;cent, &amp;curren, &amp;pound)
  15. python split()函数的用法
  16. BBWebImage 设计思路
  17. [android] 优酷环形菜单-相对布局练习
  18. Unity Shader _Time 的单位
  19. Label Propagation Algorithm LPA 标签传播算法解析及matlab代码实现
  20. 固态盘经常性蓝屏处理方法(WIN7/8)

热门文章

  1. HDU 5971 Wrestling Match (二分图)
  2. Sharepoint2013商务智能学习笔记之Secure Store Service服务配置(二)
  3. C# 中的EventHandler实例详解-转
  4. cinder介绍及使用lvm本地存储
  5. 使用 PHPMailer 发送邮件出现诡异bug,间歇性发送失败
  6. 洛谷P1282 多米诺骨牌
  7. Mac安装vue
  8. springBoot2.0 配置shiro实现权限管理
  9. Phpstorm Git 操作
  10. 从HTML form submit 到 django response是怎么完成的