Android应用开发中Action bar编写的入门教程

从Android 3.0开始除了我们重点讲解的Fragment外,Action Bar也是一个重要的内容,Action Bar主要是用于代替传统的标题栏,对于Android平板设备来说屏幕更大它的标题使用Action Bar来设计可以展示更多丰富的内容,方便操控。

Action Bar主要功能包含:

1. 显示选项菜单
2. 提供标签页的切换方式的导航功能,可以切换多个fragment.
3. 提供下拉的导航条目.
4. 提供交互式活动视图代替选项条目
5. 使用程序的图标作为返回Home主屏或向上的导航操作。

提示在你的程序中应用ActionBar需要注意几点,SDK和最终运行的固件必须是Android 3.0即honeycomb,在androidmanifest.xml文件中的uses-sdk元素中加入android:minSdkVersion 或android:targetSdkVersion,类似

< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="eoe.android.cwj"
android:versionCode="1"
android:versionName="1.0">
< uses-sdk android:minSdkVersion="honeycomb" />
< application ... > 

< /application>
< /manifest>

如果需要隐藏Action Bar可以在你的Activity的属性中设置主题风格为NoTitleBar在你的manifest文件中,下面的代码在3.0以前是隐藏标题,而在3.0以后就是隐藏ActionBar了,代码为:

< activity android:theme="@android:style/Theme.NoTitleBar">

一、添加活动条目 Action Items

  对于活动条目大家可以在下图看到Android 3.0的标题右部分可以变成工具栏,下面的Save和Delete就是两个Action Items活动条目。

  下面是一个menu的layout布局文件代码

< ?xml version="1.0" encoding="utf-8"?>
< menu xmlns:android="http://schemas.android.com/apk/res/android">
< item android:id="@+id/menu_add"
android:icon="@drawable/ic_menu_save"
android:title="@string/menu_save"
android:showAsAction="ifRoom|withText" />
< /menu>

而其他代码类似Activity中的Menu,比如

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// 当Action Bar的图标被单击时执行下面的Intent
Intent intent = new Intent(this, Android123.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}

对于ActionBar的创建,可以在你的Activity中重写onStart方法:

@Override
protected void onStart() {
super.onStart();
ActionBar actionBar = this.getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
}

调用getActionBar方式在你的Activity的onCreate中时需要注意必须在调用了setContentView之后。

二、添加活动视图 Action View

对于ActionView,我们可以在menu的布局文件使用中来自定义searchview布局,如下:

< item android:id="@+id/menu_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionLayout="@layout/searchview" />

也可以直接指定Android系统中的SearchView控件,那么这时menu"_search的代码要这样写:

< item android:id="@+id/menu_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />

大家注意上面的两种方法中一个属性是actionLayout制定一个layout xml布局文件,一个是actionViewClass指定一个类,最终调用可以在Activity中响应onCreateOptionsMenu方法映射这个menu布局即可.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
return super.onCreateOptionsMenu(menu);
}

三、添加标签 Tabs

在ActionBar中实现标签页可以实现android.app.ActionBar.TabListener ,重写onTabSelected、onTabUnselected和onTabReselected方法来关联Fragment。代码如下:

private class MyTabListener implements ActionBar.TabListener {
 private TabContentFragment mFragment;
 public TabListener(TabContentFragment fragment) {
 mFragment = fragment;
 } @Override
 public void onTabSelected(Tab tab, FragmentTransaction ft) {
 ft.add(R.id.fragment_content, mFragment, null);
 }
 @Override
 public void onTabUnselected(Tab tab, FragmentTransaction ft) {
 ft.remove(mFragment);
 }
 @Override
 public void onTabReselected(Tab tab, FragmentTransaction ft) {
 } 

}

接下来我们创建ActionBar在Activity中,代码如下;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ActionBar actionBar = getActionBar(); //提示getActionBar方法一定在setContentView后面
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
Fragment artistsFragment = new ArtistsFragment();
actionBar.addTab(actionBar.newTab().setText(R.string.tab_artists).setTabListener(new TabListener(artistsFragment)));
Fragment albumsFragment = new AlbumsFragment();
actionBar.addTab(actionBar.newTab().setText(R.string.tab_albums).setTabListener(new TabListener(albumsFragment)));
}

四、添加下拉导航 Drop-down Navigation:

创建一个SpinnerAdapter提供下拉选项,和Tab方式不同的是Drop-down只需要修改下setNavigationMode的模式,将ActionBar.NAVIGATION_MODE_TABS改为ActionBar.NAVIGATION_MODE_LIST,最终改进后的代码为

ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);

上面我们通过setListNavigationCallbacks方法绑定一个SpinnerAdapter控件,具体的OnNavigationListener代码示例为;

mOnNavigationListener = new OnNavigationListener() {
 String[] strings = getResources().getStringArray(R.array.action_list);
 @Override
 public boolean onNavigationItemSelected(int position, long itemId) {
 ListContentFragment newFragment = new ListContentFragment();
 FragmentTransaction ft = openFragmentTransaction();
 ft.replace(R.id.fragment_container, newFragment, strings[position]);
 ft.commit();
 return true;
} 

};

而其中的ListContentFragment的代码为:

public class ListContentFragment extends Fragment {
private String mText; 

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mText = getTag();
} 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setText(mText);
return text;
}
} 

五、实现切换Tabs标签;
  
Activity代码:

public class ActionBarTabs extends Activity { 

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action_bar_tabs);
} 

public void onAddTab(View v) {
final ActionBar bar = getActionBar();
final int tabCount = bar.getTabCount();
final String text = "Tab " + tabCount; 

bar.addTab(bar.newTab().setText(text)
.setTabListener(new TabListener(new TabContentFragment(text))));
} 

public void onRemoveTab(View v) {
final ActionBar bar = getActionBar();
bar.removeTabAt(bar.getTabCount() - 1);
} 

public void onToggleTabs(View v) {
final ActionBar bar = getActionBar(); 

if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 

bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
} else {
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
}
} 

public void onRemoveAllTabs(View v) {
getActionBar().removeAllTabs();
} 

private class TabListener implements ActionBar.TabListener {
private TabContentFragment mFragment;
public TabListener(TabContentFragment fragment) { 

mFragment = fragment;
} 

public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment_content, mFragment, mFragment.getText());
} 

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
} 

public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();
} 

} 

private class TabContentFragment extends Fragment {
private String mText;
public TabContentFragment(String text) {
mText = text;
} 

public String getText() {
return mText;
}
  
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false);
TextView text = (TextView) fragView.findViewById(R.id.text);
text.setText(mText);
return fragView;
}
}
}

涉及的布局文件action_bar_tabs.xml代码为:

< ?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"
android:orientation="vertical"> 

< FrameLayout android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" /> 

< LinearLayout android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical"> 

< Button android:id="@+id/btn_add_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_add_tab"
android:onClick="onAddTab" /> 

< Button android:id="@+id/btn_remove_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_remove_tab"
android:onClick="onRemoveTab" /> 

< Button android:id="@+id/btn_toggle_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_toggle_tabs"
android:onClick="onToggleTabs" /> 

< Button android:id="@+id/btn_remove_all_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_remove_all_tabs"
android:onClick="onRemoveAllTabs" />
< /LinearLayout> 

< /LinearLayout>

布局文件action_bar_tab_content.xml;

< ?xml version="1.0" encoding="utf-8"?>
< TextView xmlns:android="http://schemas.android.com/apk/res/android" 

android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
(0)

相关推荐

  • 灵活使用Android中ActionBar和ViewPager切换页面

    本文实例讲述了Android使用ActionBar和ViewPager切换页面,分享给大家供大家参考.具体如下: 运行效果截图如下: 项目布局如下: 具体代码如下: MainActivity.java代码 import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.

  • Android自定义ActionBar实例

    本文实例讲述了Android自定义ActionBar的实现方法.分享给大家供大家参考.具体实现方法如下: Android 3.0及以上已经有了ActionBar的API,可以通过引入support package在3.0以下的平台引用这些API,但这儿呢,完全自定义一个ActionBar,不用引入额外jar包,参照的是开源的UI组件GreeenDroid,项目主页:https://github.com/cyrilmottier/GreenDroid .提取出关于ActionBar的相关文件,你可

  • android中开启actionbar的两种方法

    两种方法,第一种是静态开启方法 把application 或者 activity的主题设置为Theme.Holo即可 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.baidu.homer" android:versi

  • Android中ActionBar以及menu的代码设置样式

    menu部分xml代码 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:title="搜索1" android:orderI

  • Android应用开发中Action bar编写的入门教程

    从Android 3.0开始除了我们重点讲解的Fragment外,Action Bar也是一个重要的内容,Action Bar主要是用于代替传统的标题栏,对于Android平板设备来说屏幕更大它的标题使用Action Bar来设计可以展示更多丰富的内容,方便操控. Action Bar主要功能包含: 1. 显示选项菜单 2. 提供标签页的切换方式的导航功能,可以切换多个fragment. 3. 提供下拉的导航条目. 4. 提供交互式活动视图代替选项条目 5. 使用程序的图标作为返回Home主屏或

  • Android应用开发中自定义ViewGroup视图容器的教程

    一.概述 在写代码之前,我必须得问几个问题: 1.ViewGroup的职责是啥? ViewGroup相当于一个放置View的容器,并且我们在写布局xml的时候,会告诉容器(凡是以layout为开头的属性,都是为用于告诉容器的),我们的宽度(layout_width).高度(layout_height).对齐方式(layout_gravity)等:当然还有margin等:于是乎,ViewGroup的职能为:给childView计算出建议的宽和高和测量模式 :决定childView的位置:为什么只是

  • Android应用开发中控制反转IoC设计模式使用教程

    1.概述 首先我们来吹吹牛,什么叫IoC,控制反转(Inversion of Control,英文缩写为IoC),什么意思呢? 就是你一个类里面需要用到很多个成员变量,传统的写法,你要用这些成员变量,那么你就new 出来用呗~~ IoC的原则是:NO,我们不要new,这样耦合度太高:你配置个xml文件,里面标明哪个类,里面用了哪些成员变量,等待加载这个类的时候,我帮你注入(new)进去: 这样做有什么好处呢?  回答这个问题,刚好可以回答另一个问题,很多人问,项目分层开发是吧,分为控制层.业务层

  • Android应用开发中Fragment间通信的实现教程

    为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让fragment的自包含和模块化.一旦你定义了这些可重用的Fragment,你可以通过Activity将它们关联起来并通过程序的逻辑代码将他们连接起来以实现整体组合的UI. 你会经常想要一个fragment与另一个fragment通信,例如基于用户事件改变fragment中的内容.所有fragment质检单额通信都是通过Activity关联起来的.两个fragment之间不应该直接进行通

  • Android应用开发中单元测试分析

    本文主要和大家分享如何在Android应用开发过程中如何进行单元测试,个人在做项目的过程中,觉得单元测试很有必要,以保证我们编写程序的正确性.下面我们先大概了解下单元测试,以及单元测试的作用.        单元测试(又称为模块测试)是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作.程序单元是应用的最小可测试部件.在过程化编程中,一个单元就是单个程序.函数.过程等:对于面向对象编程,最小单元就是方法,包括基类(超类).抽象类.或者派生类(子类)中的方法.单元测试是由程序员自己来完成

  • Android App开发中创建Fragment组件的教程

    你可以认为Fragment作为Activity的一个模块部分,有它自己的生命周期,获取它自己的事件,并且你可以在Activity运行的时候添加或者移除它(有点像你可以在不同的Activity中重用的一个"子Activity").这节课程讲述如何使用Support Library继承Fragment类,所以你的应用程序仍然是兼容运行的系统版本低于Android1.6的设备. 注意:如果你决定你的应用要求的最低的API级别是11或者更高,你不需要使用Support Library,反而能使

  • Android 游戏开发中绘制游戏触摸轨迹的曲线图

    本篇文章主要来讲解怎样绘制游戏触摸轨迹的曲线图. 我们在onTouchEvent方法中,可以获取到触摸屏幕时手指触摸点的x.y坐标,如何用这些点形成一条无规则轨迹并把这条无规则轨迹曲线显示在屏幕上就是本篇文章的主旨内容. Android Path类 Android提供了一个Path类 , 顾名思义这个类可以设置曲线路径轨迹.任何无规则的曲线实际上都是由若干条线段组成,而线段的定义为两点之间最短的一条线.path类就 可以记录这两点之间的轨迹,那么若干个Path 就是我们须要绘制的无规则曲线. 下

  • Android编程开发中ListView的常见用法分析

    本文实例讲述了Android编程开发中ListView的常见用法.分享给大家供大家参考,具体如下: 一.ListView的使用步骤 ListView的使用通常有以下三个要素: (1)ListView中每个条目的布局; (2)填充进入ListView中的内容; (3)将内容与页面进行整合的Adapter. 因此,使用ListView也通常有以下三个步骤: (1)创建ListView条目的布局文件(或使用Android SDK提供的布局); (2)创建填充进入ListView中的内容,如字符串.图片

  • Android编程开发中的正则匹配操作示例

    本文实例讲述了Android编程开发中的正则匹配操作.分享给大家供大家参考,具体如下: 在Android开发中,可能也会遇到一下输入框的合法性验证,这时候最常用的就应该是正则表达式去做一些匹配了,下面就常用的正则匹配做一下介绍 1. 手机号码的验证 根据实际开发于2009年9月7日最新统计: 中国电信发布中国3G号码段:中国联通185,186;中国移动188,187;中国电信189,180共6个号段. 移动:134.135.136.137.138.139.150.151.157(TD).158.

  • Android程序开发中单选按钮(RadioGroup)的使用详解

    在还没给大家介绍单选按钮(RadioGroup)的使用,先给大家展示下效果图吧: 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_heig

随机推荐