Android actionBar与Fragment结合使用Demo2
上一篇文章介绍了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的更多相关文章
- Android ActionBar详解
Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar 目录(?)[+] 第4 ...
- 详解Android ActionBar之二:ActionBar添加Tabs标签和下拉导航
本节主要讲解ActionBar如何添加Tabs标签和下拉导航. 一.添加标签 Tabs 在ActionBar中实现标签页可以实现android.app.ActionBar.TabListener ,重 ...
- Android ActionBar(转)
本文内容 关于 ActionBar 必要条件 项目结构 环境 演示一:Action Bar 显示隐藏 演示二:Action Item 显示菜单选项 演示三:Action Home 启用“返回/向上”程 ...
- Android ActionBar详解(二):ActionBar实现Tabs标签以及下拉导航
一.添加标签 Tabs 在ActionBar中实现标签页可以实现android.app.ActionBar.TabListener ,重写onTabSelected.onTabUnselected ...
- Android ActionBar应用实战,高仿微信主界面的设计
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/26365683 经过前面两篇文章的学习,我想大家对ActionBar都已经有一个相对 ...
- Android ActionBar完全解析,使用官方推荐的最佳导航栏(下) .
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/25466665 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...
- Android 原生 Android ActionBar Tab (滑动)导航
本文内容 环境 项目结构 演示一:ActionBar Tab 导航 演示二:ActionBar Tab 带滑动导航 本文演示 Tab 导航.第一个演示,是基本的 Tab 导航,第二个是带滑动的 Tab ...
- Android 原生 Android ActionBar
本文内容 关于 ActionBar 必要条件 项目结构 环境 演示一:Action Bar 显示隐藏 演示二:Action Item 显示菜单选项 演示三:Action Home 启用"返回 ...
- Android Actionbar Tab 导航模式
Android Actionbar Tab 下图中,红色矩形圈起来的就是我们 ActionBar Tab,下面我们将一步一步的实现下图中的效果. 初次尝试 package com.example.it ...
随机推荐
- BZOJ 1174 [Balkan2007]Toponyms(Trie)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1174 [题目大意] 选出一些字符串,使得字符串的最长公共前缀*字符串的总个数最大化 [ ...
- [BZOJ2716]天使玩偶
[BZOJ2716]天使玩偶 题目大意: 一个平面直角坐标系,坐标\(1\le x,y\le10^6\).\(n(n\le10^6)\)次操作,操作包含以下两种: 新增一个点\((x,y)\): 询问 ...
- BZOJ5217: [Lydsy2017省队十连测]航海舰队 FFT
被FFT的空间卡了半天 后来发现根本不用开那么大... 首先可以把包含舰艇的那个小矩形找出来 将它一行一行连接成一个串T 其中舰艇位置为1其他位置为0 将大矩形也连成串S 其中礁石为1其他为0 两个串 ...
- Shell 学习笔记之条件语句
条件语句 if # if if condition then command fi # if else if condition then command else command fi # if e ...
- Codeforces Round #360 (Div. 2) D. Remainders Game 数学
D. Remainders Game 题目连接: http://www.codeforces.com/contest/688/problem/D Description Today Pari and ...
- php 获取所有常量
有的时候想得到某个完整路径,看看都定义了哪些常量,可以这样做,即把所有的常量都打印出来,然后看看有没有自己想要的,感觉挺方便 官方给的原型: array get_defined_constants ( ...
- 机器学习(4):BP神经网络原理及其python实现
BP神经网络是深度学习的重要基础,它是深度学习的重要前行算法之一,因此理解BP神经网络原理以及实现技巧非常有必要.接下来,我们对原理和实现展开讨论. 1.原理 有空再慢慢补上,请先参考老外一篇不错的 ...
- 【原】MyBatis执行DDL:create table,drop table等等
[前言] 对MyBatis一直停留在仅仅会用的阶段,常用的场景就是通过MyBatis对表数据进行DML(insert, delete, update等)操作,从来没有想过通过MyBatis对数据库 进 ...
- MySQL Proxy 实现MySQLDB 读写分离
一.简述 MySQL Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测.分析或改变它们的通信.它使用灵活,没有限制,常见的用途包括:负载平衡,故障.查询分析 ...
- [转载] 为Visual Studio添加默认INCLUDE包含路径的方法
原文地址 你是否曾经也有过这样的问题: 用VS的时候,有时会用到一些非自带的库,例如WTL.Boost.DX等,每次需要用到时都要在项目属性里添加相应的include目录,久而久之觉得有点麻烦.是否有 ...