全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

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 


相关文章: 做企业网站制作流程,企业网站制作基本流程有哪些?  建站之星安装后如何配置SEO及设计样式?  建站之星图片链接生成指南:自助建站与智能设计教程  胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?  免费公司网站制作软件,如何申请免费主页空间做自己的网站?  如何在建站之星绑定自定义域名?  武汉网站制作费用多少,在武汉武昌,建面100平方左右的房子,想装暖气片,费用大概是多少啊?  香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化  html制作网站的步骤有哪些,iapp如何添加网页?  简历在线制作网站免费版,如何创建个人简历?  广州建站公司哪家好?十大优质服务商推荐  如何在阿里云ECS服务器部署织梦CMS网站?  建站之星导航配置指南:自助建站与SEO优化全解析  如何通过wdcp面板快速创建网站?  购物网站制作公司有哪些,哪个购物网站比较好?  C++如何编写函数模板?(泛型编程入门)  广州网站建站公司选择指南:建站流程与SEO优化关键词解析  天河区网站制作公司,广州天河区如何办理身份证?需要什么资料有预约的网站吗?  网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?  微信网站制作公司有哪些,民生银行办理公司开户怎么在微信网页上查询进度?  西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?  C++如何使用std::optional?(处理可选值)  建站VPS能否同时实现高效与安全翻墙?  已有域名和空间如何快速搭建网站?  北京营销型网站制作公司,可以用python做一个营销推广网站吗?  深入理解Android中的xmlns:tools属性  简单实现Android验证码  ui设计制作网站有哪些,手机UI设计网址吗?  PHP 500报错的快速解决方法  Android使用GridView实现日历的简单功能  安云自助建站系统如何快速提升SEO排名?  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  建站VPS选购需注意哪些关键参数?  建站10G流量真的够用吗?如何应对访问高峰?  如何选择香港主机高效搭建外贸独立站?  建站之星如何配置系统实现高效建站?  国美网站制作流程,国美电器蒸汽鍋怎么用官方网站?  免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?  专业网站制作服务公司,有哪些网站可以免费发布招聘信息?  阿里云网站搭建费用解析:服务器价格与建站成本优化指南  微网站制作教程,我微信里的网站怎么才能复制到浏览器里?  济南网站建设制作公司,室内设计网站一般都有哪些功能?  如何用wdcp快速搭建高效网站?  网站图片在线制作软件,怎么在图片上做链接?  北京的网站制作公司有哪些,哪个视频网站最好?  微信小程序 五星评分(包括半颗星评分)实例代码  建站之星如何优化SEO以实现高效排名?  如何在Windows服务器上快速搭建网站?  交易网站制作流程,我想开通一个网站,注册一个交易网址,需要那些手续?  如何用PHP快速搭建CMS系统? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。