Android 中ActionBar+fragment实现页面导航的实例

为保证android2.0以上均能运行,使用support.v7库下的actionbar及fragment
继承自AppCompatActivity(ActionBarActivity已过时)使用getSupportActionBar()得到ActionBar,
ActionBar.Tab,这里Tab必须设置监听,在监听中实现Fragment的切换。
这里重点提一下,Theme主题一定要适配,因为我使用的是AppCompatActivity所以,
android:theme="@style/Theme.AppCompat.Light"
如果不用AppCompatActivity一定要注意使用相应的主题适配,否则会getActionBar/getSupportActionbar的时候拿不到东西,空指针报错
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<FrameLayout
android:id="@+id/context"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
package com.example.yasin.actionbarusing;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionBar.newTab().setText("Tab1");
tab1.setTabListener(new MyTabListener(new Fragment1()));
ActionBar.Tab tab2 = actionBar.newTab().setText("Tab2");
tab2.setTabListener(new MyTabListener(new Fragment2()));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
}
class MyTabListener implements ActionBar.TabListener{
private Fragment fragment;
public MyTabListener (Fragment fragment){
this.fragment=fragment;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
ft.replace(R.id.context,fragment);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
//ft.remove(fragment);
}
}
}
<?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">
<TextView
android:id="@+id/tv1"
android:text="fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
package com.example.yasin.actionbarusing;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Yasin on 2016/1/3.
*/
public class Fragment1 extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1,container,false);
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yasin.actionbarusing" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
效果图:
如有疑问请留言或者到本站社区交流讨论,大家共同进步,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
# Android
# 中ActionBar+fragment的应用
# 页面导航的实现
# Android中Activity和Fragment传递数据的两种方式
# Android中Fragment相互切换间不被回收的实现方法
# Android开发中如何解决Fragment +Viewpager滑动页面重复加载的问题
# Android使用TabLayout+Fragment实现顶部选项卡
# Android使用TabLayou+fragment+viewpager实现滑动切换页面效果
# android 中viewpager+fragment仿微信底部TAG完美渐变
# 的是
# 如有
# 希望能
# 报错
# 谢谢大家
# 则会
# 均能
# 共同进步
# 疑问请
# 因为我
# 为保证
# context
# id
# paddingBottom
# FrameLayout
# gt
# MainActivity
# paddingLeft
# dimen
# match_parent
相关文章:
如何选择高效便捷的WAP商城建站系统?
香港服务器建站指南:外贸独立站搭建与跨境电商配置流程
建站之家VIP精选网站模板与SEO优化教程整合指南
建站之星IIS配置教程:代码生成技巧与站点搭建指南
C#如何使用XPathNavigator高效查询XML
广州建站公司哪家好?十大优质服务商推荐
宝塔建站助手安装配置与建站模板使用全流程解析
建站VPS配置与SEO优化指南:关键词排名提升策略
相册网站制作软件,图片上的网址怎么复制?
,柠檬视频怎样兑换vip?
如何在云主机上快速搭建多站点网站?
广州顶尖建站服务:企业官网建设与SEO优化一体化方案
个人摄影网站制作流程,摄影爱好者都去什么网站?
如何在Windows 2008云服务器安全搭建网站?
如何正确选择百度移动适配建站域名?
淘宝制作网站有哪些,淘宝网官网主页?
如何通过NAT技术实现内网高效建站?
如何通过虚拟机搭建网站?详细步骤解析
开源网站制作软件,开源网站什么意思?
如何确保FTP站点访问权限与数据传输安全?
如何在IIS中新建站点并配置端口与IP地址?
再谈Python中的字符串与字符编码(推荐)
攀枝花网站建设,攀枝花营业执照网上怎么年审?
实现点击下箭头变上箭头来回切换的两种方法【推荐】
较简单的网站制作软件有哪些,手机版网页制作用什么软件?
小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化
如何构建满足综合性能需求的优质建站方案?
黑客入侵网站服务器的常见手法有哪些?
深圳网站制作费用多少钱,读秀,深圳文献港这样的网站很多只提供网上试读,但有些人只要提供试读的文章就能全篇下载,这个是怎么弄的?
视频网站制作教程,怎么样制作优酷网的小视频?
西安专业网站制作公司有哪些,陕西省建行官方网站?
如何在IIS管理器中快速创建并配置网站?
如何在阿里云购买域名并搭建网站?
创业网站制作流程,创业网站可靠吗?
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?
如何处理“XML格式不正确”错误 常见XML well-formed问题解决方法
网站插件制作软件免费下载,网页视频怎么下到本地插件?
青岛网站设计制作公司,查询青岛招聘信息的网站有哪些?
网站制作软件有哪些,制图软件有哪些?
c++ stringstream用法详解_c++字符串与数字转换利器
ui设计制作网站有哪些,手机UI设计网址吗?
昆明网站制作哪家好,昆明公租房申请网上登录入口?
如何在万网自助建站中设置域名及备案?
简易网站制作视频教程,使用记事本编写一个简单的网页html文件?
如何用美橙互联一键搭建多站合一网站?
清除minerd进程的简单方法
安徽网站建设与外贸建站服务专业定制方案
免费公司网站制作软件,如何申请免费主页空间做自己的网站?
官网自助建站平台指南:在线制作、快速建站与模板选择全解析
php能控制zigbee模块吗_php通过串口与cc2530 zigbee通信【介绍】
*请认真填写需求信息,我们会在24小时内与您取得联系。