上一篇文章介绍了ActionBar的使用,这里介绍ActionBar的还有一种用法。达到的效果和曾经的GroupActivity或TabHost是一样的,可作为导航来使用。

实现效果图:

源码:

布局文件:activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@android:color/white"> </RelativeLayout>

f1.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:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="欢迎收看体育新闻..."
android:textSize="20sp"
android:textColor="@android:color/holo_blue_dark"/> <RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>

f2.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"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="欢迎收看娱乐新闻..."
android:textColor="@android:color/holo_green_dark"
android:textSize="20sp" /> </LinearLayout>

f3.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"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="欢迎收看军事新闻..."
android:textColor="@android:color/holo_orange_dark"
android:textSize="20sp" /> </LinearLayout>

代码文件:

MainActivity:

package com.fragmentdemo10_actionbar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle; public class MainActivity extends Activity {
private ActionBar actionBar;
/**
* 设置三个整型常量。分别为0,1,2;分别相应 :运动新闻、娱乐新闻、军事新闻。
*/
private final int SPORTS = 0;
private final int ENTERTAINMENT = 1;
private final int MILITARY = 2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); actionBar = getActionBar();
// 设置ActionBar的导航模式
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
/**
* 加入三个tab,分别为:体育新闻,娱乐新闻,军事新闻。 */
actionBar.addTab(actionBar.newTab().setText("体育新闻")
.setIcon(R.drawable.ic_launcher)
.setTabListener(new MyTabListener()).setTag(SPORTS));
actionBar.addTab(actionBar.newTab().setText("娱乐新闻")
.setIcon(R.drawable.ic_launcher)
.setTabListener(new MyTabListener()).setTag(ENTERTAINMENT));
actionBar.addTab(actionBar.newTab().setText("军事新闻")
.setIcon(R.drawable.ic_launcher)
.setTabListener(new MyTabListener()).setTag(MILITARY));
} class MyTabListener implements TabListener { @Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (Integer.parseInt(tab.getTag().toString())) {
/**
* 相应体育新闻
*/
case SPORTS:
ft.replace(R.id.main, new FragementA());
break;
/**
* 相应娱乐新闻
*/
case ENTERTAINMENT:
ft.replace(R.id.main, new FragementB());
break;
/**
* 相应军事新闻
*/
case MILITARY:
ft.replace(R.id.main, new FragementC());
break;
default:
break;
}
} @Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) { } @Override
public void onTabReselected(Tab tab, FragmentTransaction ft) { } }
}

FragmentA(Tab体育新闻相应的Fragment):

package com.fragmentdemo10_actionbar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Tab体育新闻相应的Fragment
*
*/
public class FragementA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.f1, null);
return view;
}
}

FragmentB(Tab娱乐新闻相应的Fragment):

package com.fragmentdemo10_actionbar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* TAB娱乐新闻相应的Fragment
*
*/
public class FragementB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.f2, null);
return view;
}
}

FragmentC(Tab军事新闻相应的Fragment):

package com.fragmentdemo10_actionbar;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* TAB军事新闻相应的Fragment
*
*/
public class FragementC extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.f3, null);
return view;
}
}

源码下载:

点击下载源代码

Android actionBar与Fragment结合使用Demo2的更多相关文章

  1. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

  2. 详解Android ActionBar之二:ActionBar添加Tabs标签和下拉导航

    本节主要讲解ActionBar如何添加Tabs标签和下拉导航. 一.添加标签 Tabs 在ActionBar中实现标签页可以实现android.app.ActionBar.TabListener ,重 ...

  3. Android ActionBar(转)

    本文内容 关于 ActionBar 必要条件 项目结构 环境 演示一:Action Bar 显示隐藏 演示二:Action Item 显示菜单选项 演示三:Action Home 启用“返回/向上”程 ...

  4. Android ActionBar详解(二):ActionBar实现Tabs标签以及下拉导航

    一.添加标签 Tabs   在ActionBar中实现标签页可以实现android.app.ActionBar.TabListener ,重写onTabSelected.onTabUnselected ...

  5. Android ActionBar应用实战,高仿微信主界面的设计

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/26365683 经过前面两篇文章的学习,我想大家对ActionBar都已经有一个相对 ...

  6. Android ActionBar完全解析,使用官方推荐的最佳导航栏(下) .

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/25466665 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...

  7. Android 原生 Android ActionBar Tab (滑动)导航

    本文内容 环境 项目结构 演示一:ActionBar Tab 导航 演示二:ActionBar Tab 带滑动导航 本文演示 Tab 导航.第一个演示,是基本的 Tab 导航,第二个是带滑动的 Tab ...

  8. Android 原生 Android ActionBar

    本文内容 关于 ActionBar 必要条件 项目结构 环境 演示一:Action Bar 显示隐藏 演示二:Action Item 显示菜单选项 演示三:Action Home 启用"返回 ...

  9. Android Actionbar Tab 导航模式

    Android Actionbar Tab 下图中,红色矩形圈起来的就是我们 ActionBar Tab,下面我们将一步一步的实现下图中的效果. 初次尝试 package com.example.it ...

随机推荐

  1. [xsy3466]见面会

    题意:有$n$个区间,把它们划分成若干段,如果一段$k$个区间的交长度$\geq2$,那么会产生$\binom k2$的贡献,最大化贡献 对每个$i$用单调栈预处理出$l_i$表示最小的$j$使得$j ...

  2. bzoj 2466 异或方程组

    对于每个灯,我们用一个变量表示其决策,xu=0表示不选,xu=1表示选.因为每个灯最后必须都亮,所以每个等都对应一个异或方程. 解这个异或方程组,有几种情况: 1.存在唯一解(得到的上三角系数矩阵的主 ...

  3. CentOS 6.9配置EPEL源

    简介: EPEL是一个由特别兴趣小组创建.维护并管理的,针对 红帽企业版 Linux(RHEL)及其衍生发行版(比如 CentOS.Scientific Linux.Oracle Enterprise ...

  4. perf 工具介绍2

    [root@localhost ~]# cat test1.c void longa() { int i,j; ; i < ; i++) j=i; //am I silly or crazy? ...

  5. 用Qemu模拟vexpress-a9 (一) --- 搭建Linux kernel调试环境

    参考: http://blog.csdn.net/linyt/article/details/42504975 环境介绍: Win7 64 + Vmware 11 + ubuntu14.04 32 u ...

  6. waitdialogform z

    namespace DevExpress.Utils { using DevExpress.LookAndFeel; using DevExpress.Skins; using DevExpress. ...

  7. 命令行编译工具NMAKE

    简介 大家已经习惯于微软提供的功能强大的IDE,已经很少考虑手动编连项目了,所谓技多不压身,有空的时候还是随我一块了解一下命令行编译. C/C++/VC++程序员或有Unix/Linux编程经验应该很 ...

  8. CSDN积分规则具体解释--【叶子】

    前记:在CSDN的社区支持板块,常常看到有人提问,为什么有积分却不能下载,此类问题层出不穷,而论坛的各种积分制度说明又非常分散,不便于寻找,为了方便新注冊用户高速了解论坛的积分规则,也为了降低社区支持 ...

  9. [翻译] Haneke(处理图片缓存问题)

    Haneke https://github.com/hpique/Haneke A lightweight zero-config image cache for iOS. 轻量级0配置图片缓存. H ...

  10. [runtime] MAObjCRuntime

    MAObjCRuntime 源码地址:(引入头文件MARTNSObject.h即可,非arc环境下) http://pan.baidu.com/s/1eQ6776U https://github.co ...