【译】ActionBar 基础2
来源:互联网 发布:淘宝店怎么找供应商 编辑:程序博客网 时间:2024/05/22 13:43
在上一篇文章中我们用ActionBar实现了一个基本的动作,这篇文章我们将看看如何通过ActionBar来允许用户在我们的应用程序中导航。
ActionBar支持基于tab(选项卡)和基于下拉两种基本类型的导航。在Play Movies 应用程序中可以发现基于tab导航的例子。
在这个例子中tabs被用于在电影(Films)和个人视频(Personal Videos)中进行选择。要实现tab导航需要我们创建一些Fragment,这些Fragment作为tabs的内容面板。每一个都包含一个简单的布局res/layout/frag1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/frag1"/> </RelativeLayout>
和一个简单的inflates这个布局的类Fragment1.java:
public class Fragment1 extends Fragment{ @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) { return inflater.inflate( R.layout.frag1, container, false ); }}
另一个class(类)和layout(布局)几乎是一样的除了为text分配不同的string资源和inflates frag2布局替代frag1。对于一个真实的应用程序这些会明显的复杂很多,但我故意使fragments保持简单,这样可以让我们把注意力放在导航上。
接下来我们要创建一个 android.app.ActionBar.TabListener实例作为MainActivity的内部类:
private class MyTabListener implements ActionBar.TabListener{ private Fragment mFragment; private final Activity mActivity; private final String mFragName; public MyTabListener( Activity activity, String fragName ) { mActivity = activity; mFragName = fragName; } @Override public void onTabReselected( Tab tab, FragmentTransaction ft ) { // TODO Auto-generated method stub } @Override public void onTabSelected( Tab tab, FragmentTransaction ft ) { mFragment = Fragment.instantiate( mActivity, mFragName ); ft.add( android.R.id.content, mFragment ); } @Override public void onTabUnselected( Tab tab, FragmentTransaction ft ) { ft.remove( mFragment ); mFragment = null; }}它负责创建和添加一个fragment到主要内容区域android.R.id.content。
@Overridepublic void onCreate( Bundle savedInstanceState ){ super.onCreate( savedInstanceState ); ActionBar ab = getActionBar(); ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS ); Tab tab = ab.newTab() .setText( R.string.frag1 ) .setTabListener( new MyTabListener( this, Fragment1.class.getName() ) ); ab.addTab( tab ); tab = ab.newTab() .setText( R.string.frag2 ) .setTabListener( new MyTabListener( this, Fragment2.class.getName() ) ); ab.addTab( tab );}
最后我们需要在MainActivity的onCreate 方法中实现这个。
这里设置了所需的导航模式,然后创建了两个包含显示文本和相应fragment的类名的tab。需要指明的是我们并没有像往常一样在onCreate()中执行setContentView(),因为我们通过在TabListner中用android.R.id.content将fragments直接添加到了内容区域。如果我们想做一些更复杂的事,我们可以执行setContentView()来添加自定义的layout,但我们必须改变我们的TabListener将fragments添加到包含在自定义layout中的空容器来替代。要是我们运行目前为止我们创建的将会得到在两个fragment之间导航的tabs。
有一点需要指名,由于没有足够的空间tab被分到了第二行,在大的设备上或切换到横屏模式就会有足够的空间,ActionBar会自动适应将用一行显示所有内容。
在ICS版本(Android 4.0)或更新版本,将可以通过用uiOptions="splitActionBarWhenNarrow" 参数或在manifest的elements中控制这个。
下一篇文章我们将看看基于下拉的导航,关于本文的代码资源可以在 here获得。
- 【译】ActionBar 基础2
- 【译】ActionBar 基础1
- 【译】ActionBar 基础3
- 【译】ActionBar 基础4
- Andorid ActionBar基础学习
- ActionBar详解之一(ActionBar基础)
- ActionBar(一)之ActionBar基础
- actionbar 2
- 【Android 应用开发】 ActionBar 基础
- <Android 基础(六)> ActionBar
- ActionBar神奇魅力2
- Android ActionBar<2>
- ActionBar入门(2)
- android ActionBar(2)
- ActionBar基础学习之头部返回菜单
- ActionBar
- ActionBar
- ActionBar
- Common Lisp 学习书目
- 编写程序,输入某一时刻的时间,加一秒后输出新的时间,时间以时、分、秒表示(利用结构类型)
- 黑马程序员-------JAVA图文复习八
- 1.窗体与界面设计
- APUE 之 随书源码的编译与使用, 基本环境搭建
- 【译】ActionBar 基础2
- 深入理解按位异或运算符
- 黑马程序员-------JAVA图文复习九
- log4net小结
- 如何关闭Cocos2d-x引擎 初始的警告
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- php 获取页面中的指定内容类
- 2013.06.14刘汝佳白书例题3-3-竖式计算
- 个人简介