像我这个有强迫症的人来说,自从TabActivity抛弃之后,再使用看到一个个警告和一条条划着的横线,心里很不舒服,现在终于下定决心用Fragment来替换掉TabActivity了!我的研究成果如下:

首先是MainActivity,它需要继承FragmentActivity(这里是指:版本是3.0之前的继承
FragmentActivity,3.0版本之后的继承Activity就可以),对于FragmentActivity的声明周期我就不过多介绍了,
和Activity差不了多少,自己也能弄明白!下边是MainActivity的代码:

  1. package net.loonggg.fragment;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.support.v4.app.FragmentActivity;
  5. import android.support.v4.app.FragmentManager;
  6. import android.support.v4.app.FragmentTransaction;
  7. import android.view.Window;
  8. import android.widget.RadioButton;
  9. import android.widget.RadioGroup;
  10. import android.widget.RadioGroup.OnCheckedChangeListener;
  11. public class MainActivity extends FragmentActivity {
  12. private Fragment[] mFragments;
  13. private RadioGroup bottomRg;
  14. private FragmentManager fragmentManager;
  15. private FragmentTransaction fragmentTransaction;
  16. private RadioButton rbOne, rbTwo, rbThree, rbFour;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. requestWindowFeature(Window.FEATURE_NO_TITLE);
  21. setContentView(R.layout.activity_main);
  22. mFragments = new Fragment[3];
  23. fragmentManager = getSupportFragmentManager();
  24. mFragments[0] = fragmentManager.findFragmentById(R.id.fragement_main);
  25. mFragments[1] = fragmentManager.findFragmentById(R.id.fragement_search);
  26. mFragments[2] = fragmentManager
  27. .findFragmentById(R.id.fragement_setting);
  28. fragmentTransaction = fragmentManager.beginTransaction()
  29. .hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]);
  30. fragmentTransaction.show(mFragments[0]).commit();
  31. setFragmentIndicator();
  32. }
  33. private void setFragmentIndicator() {
  34. bottomRg = (RadioGroup) findViewById(R.id.bottomRg);
  35. rbOne = (RadioButton) findViewById(R.id.rbOne);
  36. rbTwo = (RadioButton) findViewById(R.id.rbTwo);
  37. rbThree = (RadioButton) findViewById(R.id.rbThree);
  38. bottomRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  39. @Override
  40. public void onCheckedChanged(RadioGroup group, int checkedId) {
  41. fragmentTransaction = fragmentManager.beginTransaction()
  42. .hide(mFragments[0]).hide(mFragments[1])
  43. .hide(mFragments[2]);
  44. switch (checkedId) {
  45. case R.id.rbOne:
  46. fragmentTransaction.show(mFragments[0]).commit();
  47. break;
  48. case R.id.rbTwo:
  49. fragmentTransaction.show(mFragments[1]).commit();
  50. break;
  51. case R.id.rbThree:
  52. fragmentTransaction.show(mFragments[2]).commit();
  53. break;
  54. default:
  55. break;
  56. }
  57. }
  58. });
  59. }
  60. }

下边对应的是MainActivity的布局文件activity_main.xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/activity_bg"
  6. android:orientation="vertical" >
  7. <!-- 上边主页面 -->
  8. <fragment
  9. android:id="@+id/fragement_main"
  10. android:name="net.loonggg.fragment.FragmentMain"
  11. android:layout_width="fill_parent"
  12. android:layout_height="fill_parent"
  13. android:layout_weight="10" />
  14. <fragment
  15. android:id="@+id/fragement_search"
  16. android:name="net.loonggg.fragment.FragmentSearch"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:layout_weight="10" />
  20. <fragment
  21. android:id="@+id/fragement_setting"
  22. android:name="net.loonggg.fragment.FragmentSetting"
  23. android:layout_width="fill_parent"
  24. android:layout_height="fill_parent"
  25. android:layout_weight="10" />
  26. <!-- 底部菜单页面 -->
  27. <RadioGroup
  28. android:id="@+id/bottomRg"
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content"
  31. android:layout_weight="0.5"
  32. android:background="@drawable/tab_footer_bg"
  33. android:orientation="horizontal" >
  34. <RadioButton
  35. android:id="@+id/rbOne"
  36. style="@style/rg_btn_style"
  37. android:checked="true"
  38. android:drawableTop="@drawable/rb_one_btn_selector"
  39. android:text="首页" />
  40. <RadioButton
  41. android:id="@+id/rbTwo"
  42. style="@style/rg_btn_style"
  43. android:drawableTop="@drawable/rb_two_btn_selector"
  44. android:text="搜索" />
  45. <RadioButton
  46. android:id="@+id/rbThree"
  47. style="@style/rg_btn_style"
  48. android:drawableTop="@drawable/rb_three_btn_selector"
  49. android:text="设置" />
  50. </RadioGroup>
  51. </LinearLayout>

这里为了大家方便,展示一下项目的布局图:

再下边是要设计的首页界面,它是继承的Fragment,具体看代码:

  1. package net.loonggg.fragment;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. public class FragmentMain extends Fragment {
  9. private TextView tv;
  10. @Override
  11. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  12. Bundle savedInstanceState) {
  13. return inflater.inflate(R.layout.fragment_main, container, false);
  14. }
  15. @Override
  16. public void onActivityCreated(Bundle savedInstanceState) {
  17. super.onActivityCreated(savedInstanceState);
  18. tv = (TextView) getView().findViewById(R.id.titleTv);
  19. tv.setText("首页");
  20. }
  21. }

接着是对应的布局文件代码fragment_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <include
  7. android:id="@+id/one_title"
  8. layout="@layout/title_bar" />
  9. <TextView
  10. android:layout_width="fill_parent"
  11. android:layout_height="fill_parent"
  12. android:gravity="center"
  13. android:text="这是首页"
  14. android:textColor="#000000" />
  15. </LinearLayout>

再接着是:搜索界面的代码:

  1. package net.loonggg.fragment;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. public class FragmentSearch extends Fragment {
  9. private TextView tv;
  10. @Override
  11. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  12. Bundle savedInstanceState) {
  13. return inflater.inflate(R.layout.fragment_search, container, false);
  14. }
  15. @Override
  16. public void onActivityCreated(Bundle savedInstanceState) {
  17. super.onActivityCreated(savedInstanceState);
  18. tv = (TextView) getView().findViewById(R.id.titleTv);
  19. tv.setText("搜索");
  20. }
  21. @Override
  22. public void onPause() {
  23. super.onPause();
  24. }
  25. }

如上是对应的布局文件的代码fragment_search.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6. <include layout="@layout/title_bar" />
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:gravity="center"
  11. android:text="这是搜索界面"
  12. android:textColor="#000000" />
  13. </LinearLayout>

紧跟着是:设置界面的代码:

  1. package net.loonggg.fragment;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. public class FragmentSetting extends Fragment {
  9. private TextView tv;
  10. @Override
  11. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  12. Bundle savedInstanceState) {
  13. return inflater.inflate(R.layout.fragment_setting, container, false);
  14. }
  15. @Override
  16. public void onActivityCreated(Bundle savedInstanceState) {
  17. super.onActivityCreated(savedInstanceState);
  18. tv = (TextView) getView().findViewById(R.id.titleTv);
  19. tv.setText("设置");
  20. }
  21. }

当然一样,下边对应的是设置界面的布局文件代码fragment_setting.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6. <include layout="@layout/title_bar" />
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:gravity="center"
  11. android:text="这是设置页面"
  12. android:textColor="#000000" />
  13. </LinearLayout>

最后是我用的title_bar.xml文件,这个文件是嵌入到各个界面中的那个顶部的标题的布局文件,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:background="@drawable/title_bg"
  6. android:gravity="center"
  7. android:orientation="vertical" >
  8. <TextView
  9. android:id="@+id/titleTv"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_gravity="center"
  13. android:gravity="center"
  14. android:textColor="#ffffff"
  15. android:textSize="20sp" />
  16. </LinearLayout>

到这里就基本完成了!!!你会了吗?

用Fragment实现如新浪微博一样的底部菜单的切换的更多相关文章

  1. Android自己定义TabActivity(实现仿新浪微博底部菜单更新UI)

    现在Android上非常多应用都採用底部菜单控制更新的UI这样的框架,比如新浪微博 点击底部菜单的选项能够更新界面.底部菜单能够使用TabHost来实现,只是用过TabHost的人都知道自己定义Tab ...

  2. 转-Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

    http://www.cnblogs.com/lichenwei/p/3985121.html 记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果, ...

  3. 安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

    记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果,但作为学习,还是需要来了解下这个新引入类FragmentTabHost 之前2篇文章的链接: 安 ...

  4. [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单

    Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...

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

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

  6. Android底部菜单的实现

    前言:以前制作菜单使用TabHost,但是android 3.0以上就被废弃了,google已经不建议使这个类了.ActionBar也是菜单,不过在头部,算是导航了 ===本文就介绍怎么制作底部菜单= ...

  7. Android自定义控件----RadioGroup实现APP首页底部Tab的切换

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  8. 转-TabHost组件(一)(实现底部菜单导航)

    http://www.cnblogs.com/lichenwei/p/3974009.html 什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用wind ...

  9. Android应用主界面底部菜单实现

    介绍 现在绝大多数主流的应用主界面,都会包含一个底部菜单,就拿腾讯的QQ与微信来说,看起来是这样的  <---我是底部菜单 原理 在很久以前,可以通过TabActivity实现相关功能,自从Fr ...

随机推荐

  1. 笨办法学Python(十六)

    习题 16: 读写文件 如果你做了上一个练习的加分习题,你应该已经了解了各种文件相关的命令(方法/函数).你应该记住的命令如下: close – 关闭文件.跟你编辑器的 文件->保存.. 一个意 ...

  2. May 2 2017 Week 18 Tuesday

    The beauty of the journey is found in the scenery along the way. 旅行之美在于沿途所见的风景. Several years ago, I ...

  3. iOS逆向命令集

    越狱命令行 破壳: 10.10.215.119 ssh root@10.10.215.119 ssh root@10.10.213.176 CCBMobileBank Fuqianlade-iPhon ...

  4. 【[TJOI2018]异或】

    写板子了,可持久化\(Trie\)的板子了 其实和主席树写法类似,还是存好左右儿子之后存好权值 之后差分去查询就好了 这道题第一问我们直接\(dfs\)序转化成区间 第二问搞成\(x,y,lca(x, ...

  5. 2017.10.24 Java 详解 JVM 工作原理和流程

    JVM工作原理和特点主要是指操作系统装入JVM是通过jdk中Java.exe来完成,通过下面4步来完成JVM环境. 1.创建JVM装载环境和配置 2.装载JVM.dll 3.初始化JVM.dll并挂界 ...

  6. C#操作Word,写数据,插入图片

    本篇介绍的是如何在C#中往word里面写入数据. 如何在线的操作文档:  c#在线操作文档 关于Aspose.Word控件的介绍,请戳→ 介绍 首先需要去下载这个dll文件,然后引用到你的项目当中.地 ...

  7. 前端JavaScript之DOM事件操作

    DOM:文档对象模型,操作网页上的元素的API.比如让盒子移动.变色.轮播图等. 1.DOM(Document Object Moduel):文档对象模型 定义了访问和操作HTML文档的标准法,把HT ...

  8. TCP心跳的意义

    摘自:https://blog.csdn.net/bjrxyz/article/details/71076442 TCP新手误区–心跳的意义 背景 最近面试了很多的学生,发现很多TCP的新手对于TCP ...

  9. java面试题:已知一个数组[2,4,6,2,1,5],将该数组进行排序(降序,不能用工具类进行排序),创建两条线程交替输出排序后的数组,线程名自定义

    package com.swift; import java.util.Arrays; import java.util.Comparator; public class ArrayThread_Te ...

  10. iOS新浪微博OAuth2.0认证代码

    #import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...