用Fragment实现如新浪微博一样的底部菜单的切换
像我这个有强迫症的人来说,自从TabActivity抛弃之后,再使用看到一个个警告和一条条划着的横线,心里很不舒服,现在终于下定决心用Fragment来替换掉TabActivity了!我的研究成果如下:
首先是MainActivity,它需要继承FragmentActivity(这里是指:版本是3.0之前的继承
FragmentActivity,3.0版本之后的继承Activity就可以),对于FragmentActivity的声明周期我就不过多介绍了,
和Activity差不了多少,自己也能弄明白!下边是MainActivity的代码:
- package net.loonggg.fragment;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.view.Window;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- public class MainActivity extends FragmentActivity {
- private Fragment[] mFragments;
- private RadioGroup bottomRg;
- private FragmentManager fragmentManager;
- private FragmentTransaction fragmentTransaction;
- private RadioButton rbOne, rbTwo, rbThree, rbFour;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- mFragments = new Fragment[3];
- fragmentManager = getSupportFragmentManager();
- mFragments[0] = fragmentManager.findFragmentById(R.id.fragement_main);
- mFragments[1] = fragmentManager.findFragmentById(R.id.fragement_search);
- mFragments[2] = fragmentManager
- .findFragmentById(R.id.fragement_setting);
- fragmentTransaction = fragmentManager.beginTransaction()
- .hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]);
- fragmentTransaction.show(mFragments[0]).commit();
- setFragmentIndicator();
- }
- private void setFragmentIndicator() {
- bottomRg = (RadioGroup) findViewById(R.id.bottomRg);
- rbOne = (RadioButton) findViewById(R.id.rbOne);
- rbTwo = (RadioButton) findViewById(R.id.rbTwo);
- rbThree = (RadioButton) findViewById(R.id.rbThree);
- bottomRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- fragmentTransaction = fragmentManager.beginTransaction()
- .hide(mFragments[0]).hide(mFragments[1])
- .hide(mFragments[2]);
- switch (checkedId) {
- case R.id.rbOne:
- fragmentTransaction.show(mFragments[0]).commit();
- break;
- case R.id.rbTwo:
- fragmentTransaction.show(mFragments[1]).commit();
- break;
- case R.id.rbThree:
- fragmentTransaction.show(mFragments[2]).commit();
- break;
- default:
- break;
- }
- }
- });
- }
- }
下边对应的是MainActivity的布局文件activity_main.xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/activity_bg"
- android:orientation="vertical" >
- <!-- 上边主页面 -->
- <fragment
- android:id="@+id/fragement_main"
- android:name="net.loonggg.fragment.FragmentMain"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="10" />
- <fragment
- android:id="@+id/fragement_search"
- android:name="net.loonggg.fragment.FragmentSearch"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="10" />
- <fragment
- android:id="@+id/fragement_setting"
- android:name="net.loonggg.fragment.FragmentSetting"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="10" />
- <!-- 底部菜单页面 -->
- <RadioGroup
- android:id="@+id/bottomRg"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.5"
- android:background="@drawable/tab_footer_bg"
- android:orientation="horizontal" >
- <RadioButton
- android:id="@+id/rbOne"
- style="@style/rg_btn_style"
- android:checked="true"
- android:drawableTop="@drawable/rb_one_btn_selector"
- android:text="首页" />
- <RadioButton
- android:id="@+id/rbTwo"
- style="@style/rg_btn_style"
- android:drawableTop="@drawable/rb_two_btn_selector"
- android:text="搜索" />
- <RadioButton
- android:id="@+id/rbThree"
- style="@style/rg_btn_style"
- android:drawableTop="@drawable/rb_three_btn_selector"
- android:text="设置" />
- </RadioGroup>
- </LinearLayout>
这里为了大家方便,展示一下项目的布局图:
再下边是要设计的首页界面,它是继承的Fragment,具体看代码:
- package net.loonggg.fragment;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class FragmentMain extends Fragment {
- private TextView tv;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_main, container, false);
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- tv = (TextView) getView().findViewById(R.id.titleTv);
- tv.setText("首页");
- }
- }
接着是对应的布局文件代码fragment_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <include
- android:id="@+id/one_title"
- layout="@layout/title_bar" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:text="这是首页"
- android:textColor="#000000" />
- </LinearLayout>
再接着是:搜索界面的代码:
- package net.loonggg.fragment;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class FragmentSearch extends Fragment {
- private TextView tv;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_search, container, false);
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- tv = (TextView) getView().findViewById(R.id.titleTv);
- tv.setText("搜索");
- }
- @Override
- public void onPause() {
- super.onPause();
- }
- }
如上是对应的布局文件的代码fragment_search.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <include layout="@layout/title_bar" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:text="这是搜索界面"
- android:textColor="#000000" />
- </LinearLayout>
紧跟着是:设置界面的代码:
- package net.loonggg.fragment;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class FragmentSetting extends Fragment {
- private TextView tv;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- return inflater.inflate(R.layout.fragment_setting, container, false);
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- tv = (TextView) getView().findViewById(R.id.titleTv);
- tv.setText("设置");
- }
- }
当然一样,下边对应的是设置界面的布局文件代码fragment_setting.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <include layout="@layout/title_bar" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:text="这是设置页面"
- android:textColor="#000000" />
- </LinearLayout>
最后是我用的title_bar.xml文件,这个文件是嵌入到各个界面中的那个顶部的标题的布局文件,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/title_bg"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/titleTv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:gravity="center"
- android:textColor="#ffffff"
- android:textSize="20sp" />
- </LinearLayout>
到这里就基本完成了!!!你会了吗?
用Fragment实现如新浪微博一样的底部菜单的切换的更多相关文章
- Android自己定义TabActivity(实现仿新浪微博底部菜单更新UI)
现在Android上非常多应用都採用底部菜单控制更新的UI这样的框架,比如新浪微博 点击底部菜单的选项能够更新界面.底部菜单能够使用TabHost来实现,只是用过TabHost的人都知道自己定义Tab ...
- 转-Fragment+FragmentTabHost组件(实现新浪微博底部菜单)
http://www.cnblogs.com/lichenwei/p/3985121.html 记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果, ...
- 安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)
记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果,但作为学习,还是需要来了解下这个新引入类FragmentTabHost 之前2篇文章的链接: 安 ...
- [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单
Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...
- Xamarin.Android 利用Fragment实现底部菜单
效果图: 第一步:添加引用 引用 Crosslight.Xamarin.Android.Support.v7.AppCompat 这个包. 第二步:绘制Main和Fragment界面 fg_home. ...
- Android底部菜单的实现
前言:以前制作菜单使用TabHost,但是android 3.0以上就被废弃了,google已经不建议使这个类了.ActionBar也是菜单,不过在头部,算是导航了 ===本文就介绍怎么制作底部菜单= ...
- Android自定义控件----RadioGroup实现APP首页底部Tab的切换
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- 转-TabHost组件(一)(实现底部菜单导航)
http://www.cnblogs.com/lichenwei/p/3974009.html 什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用wind ...
- Android应用主界面底部菜单实现
介绍 现在绝大多数主流的应用主界面,都会包含一个底部菜单,就拿腾讯的QQ与微信来说,看起来是这样的 <---我是底部菜单 原理 在很久以前,可以通过TabActivity实现相关功能,自从Fr ...
随机推荐
- 分布式爬虫-Kafka监控
分布式爬虫-Kafka监控 1.介绍
- "COM Surrogate 已停止工作"解决方案(windows7 64位及32位)
根据图示步骤,将以下文件添加至“数据执行保护”的例外列表中. 64位:C:Windows\SysWOW64\dllhost.exe 32位:C:\Windows\System32\dllhost.ex ...
- MPMoviePlayerViewController和MPMoviePlayerController的使用
ios播放视频文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController.前者是一个view,后者是个Controller.差别就是MPM ...
- http知识补充
在我的职业生涯中,没怎么重视过这http四个字,想当然的觉得不就是个网页请求嘛就没怎么当回事,而且很多http相关的长篇大论一听就困,真心是弄不下去,但是就是这种观念导致我后期的工作中不断的挖坑,不断 ...
- 0x40二分法
二分模板一共有两个,分别适用于不同情况.算法思路:假设目标值在闭区间[l, r]中, 每次将区间长度缩小一半,当l = r时,我们就找到了目标值. 版本1 在单调递增序列a中查找>=x的数中最小 ...
- 软件架构中的SOA架构有哪些特点?
面向服务的架构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来.构建在各种各样的系统中的服务可以以一种统一和通用的方式进行交互. SOA是一 ...
- MAC下secureCRT无法保存密码的解决方法
在mac下新安装了secureCRT,取代系统自带的终端工具,主要是为了方便链接服务器.mac下面的secureCRT默认保存不上密码, 我们选择了保存密码后,下次登录还是提示密码错误,需要重新认证输 ...
- IOError: [Errno 22] invalid mode ('rb') or filename: 'C
dataset = scipy.io.loadmat('F:\test_data.mat') 报错 IOError: [Errno ] invalid mode ('rb') or filename: ...
- 洛谷P1762 偶数(找规律)
题目描述 给定一个正整数n,请输出杨辉三角形前n行的偶数个数对1000003取模后的结果. 输入输出格式 输入格式: 一个数 输出格式: 结果 输入输出样例 输入样例#1: 复制 6 输出样例#1: ...
- 【复杂度分析】loj#6043. 「雅礼集训 2017 Day7」蛐蛐国的修墙方案
感觉有点假 题目大意 数据范围:$n<=100$ 题目分析 由于题目给出的是 置换,所以相当于只需枚举每个环的两个状态. 主要是复杂度分析这里: 一元环:不存在 二元环:特判保平安 三元环:不存 ...