Android中Action Bar的使用
内容概要
示例演示和基本介绍
启用Action Bar
在Action Bar上添加按钮
自定义Action Bar样式
自动隐藏Action Bar
Action Provider的使用
ActionBarSherlock的使用
示例演示和基本介绍
如果使用ActionBar则分为两种情况
1、Action Bar on devices BEFORE Android 3.0(API 11)
1)、ActionBarSherlock开源库
2)、ActionBarCompat library from the Android support library v7
2、Action Bar on devices AFTER Android 3.0(API 11)
在android3.0之前要使用Action Bar有如上两种方式,第一种使用ActionBarSherlock开源库(是android兼容开发包的一个扩展)当使用该开源库是如果应用程序运行在Android3.0以上的设备则默认使用原生的Action Bar否则使用该开源库提供的Action Bar。第二种方式就是使用google官方提供的support v7这个开源库。
在android3.0之后的设备上可以使用原生的Action Bar
启用Action Bar
1、Action Bar on devices AFTER Android 3.0(API 11)
从Android 3.0开始Action bar被包含在了所使用所使用了Theme.Holo这个主题的Activity中(或Theme.Holo的子类)Theme.Holo是默认的主题,当minSdkVersion和targetSdkVersion都在11或以上时。
2、Action Bar on devices BEFORE Android 3.0(API 11)
必须是2.1以上的设备,需要在应用中包含Support Library(android-support-v7-appcompat)操作步骤:
1)、将该android-sdk-windows\extras\android\support\v7\appcompat库导入并拷贝到工作空间中最后引入到你的项目中
2)、将activity继承自ActivityBarActivity
3)、将AndroidManifest.xml文件中的主题更改为
android:theme=”@style/Theme.AppCompat.Light.DarkActionBar”
在Action Bar上添加按钮
1、For Android 3.0 and higher only
添加按钮也分为两个部分,第一个部分在xml指定这些按钮(在menu中加入不同的item项)
属性介绍
1)、android:showAsAction=“always“
一直显示在Action Bar上
2)、android:showAsAction=“ifRoom“
如果Action Bar空间足够,则显示
3)、android:showAsAction=“never“
不显示在Action Bar中,折叠在OverFlow里
4)、android:showAsAction=“withText“
菜单项和它的图标,菜单文本一起显示
2、For Android 2.1 and higher
1)、在menu.xml中加入自定义命名空间
xmlns:yourapp=”http://schemas.android.com/apk/res-auto”
2)、在showAsAction属性前指定命名空间
yourapp:showAsAction="ifRoom"
Action Bar上的按钮添加点击事件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// 给按钮添加点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(this, "Action_Search", 0).show();
break;
case R.id.action_settings:
Toast.makeText(this, "Action_Settings", 0).show();
break;
}
return super.onOptionsItemSelected(item);
}
给ActionBar左上角图标添加向上或返回按钮(查看google的官方文档)具体添加什么样的按钮根据需要设定。
//启用左上角的向上按钮
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//getActionBar().setDisplayHomeAsUpEnabled(true);//3.0以上
注:parentActivityName属性为3.0以上使用,3.0以下要用“meta-data”
自定义Action Bar样式
使用android提供好的主题
Theme.Holo for “dark” theme Theme.Holo.Light for a “light” themeFor example
以上是在3.0以上使用Android自带的主题,如果是android 2.1以上的设备使用如下方式:
For example
Customize the Background(自定义Action Bar的背景颜色)
For Android 3.0 and higher only
1、在themes.xml中新建自定义Style,使其继承已有的Action Bar Style(如:Theme.Holo)
2、覆写其actionBarStyle属性
3、actionBarStyle属性值指向另一个已被覆写了background属性的Style
4、指定该background的属性值
themes.xml
在AndroidManifest.xml中引用自定义主题
android:theme="@style/CustomActionBarTheme"
For Android 2.1 and higher
themes.xml
最后同样在清单文件中引入自定义主题
Customize the Color 参考官方文档
Customize the Tab Indicator
ActionBar actionBar = getActionBar(); // for < 3.0 getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);//设置导航方式,ActionBar.NAVIGATION_MODE_LIST
ActionBar.TabListener tabLiatener = new ActionBar.TabListener() {
@Override
// 未选中时
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
// 选中时
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
"TabSelected" + tab.getPosition(), 0).show();
}
@Override
// 重复选中时
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-g