自Android3.0之后,ActionBar的操作模式使得用户体验强了好多,但官方并没有提供低版本的解决办法,如果你关注Android开源项目,那对ActionBarSherlock肯定不陌生了,是的,老外JakeWharton的这个开源项目就很好的解决了在低版本操作系统中使用ActionBar及其他一些具有ICS风格的界面设计,而且很多其他开源框架也是在此基础上进行编写,如SlidingMenu,这个后面再说。
首先,去github将工程项目下载,其中包括一个Library Project是基础包,供其他项目引入,另外包含几个示例项目,供我们学习研究其使用方法,由于github上很多项目都是Maven+Android配置的,所以导入时候可能会出现一些小问题,所以我把已经能正常导入Eclipse的项目源码上传供大家学习使用,至于Maven+Android的模式一直没抽出时间搞明白,今后有时间一定研究研究,也希望熟悉这种配置方式的大牛指点一二,至于ActionBarSherlock的详细使用,示例工程中写的很清晰,此处不加赘述,大家自己学习研究。
/**下面是简单粗糙的记录的示例工程中的一些知识点,仅供自己日后查看整理使用,可略过。*/
ABS_AstroboyMasterConsole
- 依赖注入Roboguice 继承RoboSherlockActivity父类
- Reboguice日志输出类Ln.(E文不好就不翻译了
)
- Debug and verbose logging are automatically disabled for release builds.
- Your app name, file and line of the log message, time stamp, thread, and other useful information is automatically logged for you. (Some of this information is disabled for release builds to improve performance).
- Performance of disabled logging is faster than Log due to the use of the varargs. Since your most expensive logging will often be debug or verbose logging, this can lead to a minor performance win.
- You can override where the logs are written to and the format of the logging.
- 依赖注入用法:引入包,采用注释的方法@InjectView @Inject 等等
ABS_DemosSampleList
- ActionItems 继承SherlockActivity
/**setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT)
* SHOW_AS_ACTION_IF_ROOM 表示如果Actionbar有空间就会显示在上面没有则显示为普通菜单
*SHOW_AS_ACTION_WITH_TEXT 表示在ActionBar上的话,即使有文字和图标,只显示显示文字
*/- setTheme 设置主题 在ABS_library中已经定义好
public static final int Theme_Sherlock=0x7f0c0052;
public static final int Theme_Sherlock_Dialog=0x7f0c0057;
public static final int Theme_Sherlock_Light=0x7f0c0053;
public static final int Theme_Sherlock_Light_DarkActionBar=0x7f0c0054;
public static final int Theme_Sherlock_Light_Dialog=0x7f0c0058;
public static final int Theme_Sherlock_Light_NoActionBar=0x7f0c0056;
public static final int Theme_Sherlock_NoActionBar=0x7f0c0055;
public static final int Theme_Styled=0x7f0c0059;
- ActionModes 继承SherlockActivity
- ActionMode :Represents a contextual mode of the user interface
mMode = startActionMode(new AnActionModeOfEpicProportions());
//启动顶部操作交互界面
private final class AnActionModeOfEpicProportions implements ActionMode.Callback {
//实现其中的onCreateActionMode方法
- ActionMode :Represents a contextual mode of the user interface
- ActionProviders 菜单选项操作提供者 需要在xml中配置
- menu.add("Search")
.setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.ic_search)
.setActionView(R.layout.collapsible_edittext)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
- menu.add("Search")
- CustomNavigation 顶部导航
- getSupportActionBar().setCustomView(customNav); //自定义布局
getSupportActionBar().setDisplayShowCustomEnabled(true);
- getSupportActionBar().setCustomView(customNav); //自定义布局
- IndeterminateProgress 无限进度加载
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); //初始时转动加载
setSupportProgressBarIndeterminateVisibility(true); //手动设置
- ListNavigation 在ActionBar上添加list操作列表
采用ArrayAdapter<T>
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this); -
OverLay
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); -
Preference
继承SherlockPreferenceActivity
addPreferencesFromResource(R.xml.preferences); -
Progress 进度条
requestWindowFeature(Window.FEATURE_PROGRESS); -
SearchViews ActionBar搜索框
有专门的SearchView 初始化并添加到ActionBar的menu中
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search for countries…");
menu.add("Search")
.setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); -
SplitXXX
android:uiOptions="splitActionBarWhenNarrow" -
StaticAttachment
在普通Activity的基础上添加ActionBar