Android UI开发第三十五篇——AppCompat实现Action Bar
来源:互联网 发布:windows桌面主题 编辑:程序博客网 时间:2024/05/16 19:08
每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了。Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计。如果对ActionBar不太熟悉的可以参考《
Android UI开发第二十四篇——Action Bar》。ActionBar的API被添加在Android3.0(API 级别 11)中,低版本的还是用不了,根本不能适配支持Android 2.X系列的应用。很幸运有第三方开源的actionbarsherlock支持使得Android 2.1以上的Android应用使用actionbarsherlock定义的Action Bar。这里我们不介绍actionbarsherlock怎么使用,我们介绍一种更新的官方支持的AppCompat 使得Android2.1以上的版本可以实现Action Bar。
Google I/O 2013中AppCompat实现的Action Bar效果
AppCompat在最新的API 18的Android Support Library中。使用AppCompat需要以库的形式引入到应用中,AppCompat在<sdk>/extras/android/support/v7/appcompat/
的位置,需要自行下载,或者升级SDK。
如果应用是使用actionbarsherlock实现的Action Bar,也不必刻意的改成AppCompat。因为actionbarsherlock是一个很稳定的经过很多开发者验证的开发库。
- ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time.
- If you are already using it and do not currently require any of the above then there is no need to migrate.
ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time. If you are already using it and do not currently require any of the above then there is no need to migrate.
1)导入AppCompat库
使用AppCompat第一步需要导入AppCompat库,这一步就不做详细介绍了。
2)修改android:theme
每个使用Action Bar的Activity都应该添加Android:theme
- <activity
- ...
- android:theme="@style/Theme.AppCompat"/>
<activity ... android:theme="@style/Theme.AppCompat" />
或者修改application
- <application
- android:label="@string/app_name"
- android:icon="@drawable/ic_launcher"
- android:theme="@style/Theme.AppCompat"
- android:allowBackup="true">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/Theme.AppCompat" android:allowBackup="true">
3)Activity要继承自ActionBarActivity
实现Action Bar的视图需要继承ActionBarActivity。
4)修改menu的命名空间
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:holo="http://schemas.android.com/apk/res-auto">
- <item
- android:id="@+id/action_websearch"
- android:icon="@drawable/action_search"
- android:title="@string/action_websearch"
- holo:showAsAction="never"/>
- </menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:holo="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_websearch" android:icon="@drawable/action_search" android:title="@string/action_websearch" holo:showAsAction="never"/></menu>
要特别注意的是,通过XML文件来实现Action Item,一定要自定义命名空间,而且该命名空间的后缀一定要和item中showAsAction的前缀一致,本例中为“holo”
显示Menu需要重写onCreateOptionsMenu方法:
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.main, menu);
- return super.onCreateOptionsMenu(menu);
- }
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); }
对Menu的item事件处理需要重写onOptionsItemSelected方法。
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // The action bar home/up action should open or close the drawer.
- // ActionBarDrawerToggle will take care of this.
- if (mDrawerToggle.onOptionsItemSelected(item)) {
- return true;
- }
- // Handle action buttons
- switch(item.getItemId()) {
- case R.id.action_websearch:
- // create intent to perform web search for this planet
- Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
- intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
- // catch event that there's no activity to handle intent
- if (intent.resolveActivity(getPackageManager()) !=null) {
- startActivity(intent);
- } else {
- Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
- }
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
@Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this. if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action buttons switch(item.getItemId()) { case R.id.action_websearch: // create intent to perform web search for this planet Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle()); // catch event that there's no activity to handle intent if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show(); } return true; default: return super.onOptionsItemSelected(item); } }
上面就是简单的通过Appcompat实现Action Bar,想自定义各种属性请参考官方文档。
demo下载:demo
/*** @author 张兴业* http://blog.csdn.net/xyz_lmn* iOS入门群:83702688
* android开发进阶群:241395671* 我的新浪微博:@张兴业TBOW*/
参考:
http://antonioleiva.com/actionbarcompat-how-to-use/
http://antonioleiva.com/actionbarcompat-action-views/
http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html
每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了。Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计。如果对ActionBar不太熟悉的可以参考《
Android UI开发第二十四篇——Action Bar》。ActionBar的API被添加在Android3.0(API 级别 11)中,低版本的还是用不了,根本不能适配支持Android 2.X系列的应用。很幸运有第三方开源的actionbarsherlock支持使得Android 2.1以上的Android应用使用actionbarsherlock定义的Action Bar。这里我们不介绍actionbarsherlock怎么使用,我们介绍一种更新的官方支持的AppCompat 使得Android2.1以上的版本可以实现Action Bar。
Google I/O 2013中AppCompat实现的Action Bar效果
AppCompat在最新的API 18的Android Support Library中。使用AppCompat需要以库的形式引入到应用中,AppCompat在<sdk>/extras/android/support/v7/appcompat/
的位置,需要自行下载,或者升级SDK。
如果应用是使用actionbarsherlock实现的Action Bar,也不必刻意的改成AppCompat。因为actionbarsherlock是一个很稳定的经过很多开发者验证的开发库。
- ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time.
- If you are already using it and do not currently require any of the above then there is no need to migrate.
ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time. If you are already using it and do not currently require any of the above then there is no need to migrate.
1)导入AppCompat库
使用AppCompat第一步需要导入AppCompat库,这一步就不做详细介绍了。
2)修改android:theme
每个使用Action Bar的Activity都应该添加Android:theme
- <activity
- ...
- android:theme="@style/Theme.AppCompat"/>
<activity ... android:theme="@style/Theme.AppCompat" />
或者修改application
- <application
- android:label="@string/app_name"
- android:icon="@drawable/ic_launcher"
- android:theme="@style/Theme.AppCompat"
- android:allowBackup="true">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/Theme.AppCompat" android:allowBackup="true">
3)Activity要继承自ActionBarActivity
实现Action Bar的视图需要继承ActionBarActivity。
4)修改menu的命名空间
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:holo="http://schemas.android.com/apk/res-auto">
- <item
- android:id="@+id/action_websearch"
- android:icon="@drawable/action_search"
- android:title="@string/action_websearch"
- holo:showAsAction="never"/>
- </menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:holo="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_websearch" android:icon="@drawable/action_search" android:title="@string/action_websearch" holo:showAsAction="never"/></menu>
要特别注意的是,通过XML文件来实现Action Item,一定要自定义命名空间,而且该命名空间的后缀一定要和item中showAsAction的前缀一致,本例中为“holo”
显示Menu需要重写onCreateOptionsMenu方法:
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.main, menu);
- return super.onCreateOptionsMenu(menu);
- }
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); }
对Menu的item事件处理需要重写onOptionsItemSelected方法。
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // The action bar home/up action should open or close the drawer.
- // ActionBarDrawerToggle will take care of this.
- if (mDrawerToggle.onOptionsItemSelected(item)) {
- return true;
- }
- // Handle action buttons
- switch(item.getItemId()) {
- case R.id.action_websearch:
- // create intent to perform web search for this planet
- Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
- intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
- // catch event that there's no activity to handle intent
- if (intent.resolveActivity(getPackageManager()) !=null) {
- startActivity(intent);
- } else {
- Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
- }
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
@Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this. if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action buttons switch(item.getItemId()) { case R.id.action_websearch: // create intent to perform web search for this planet Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle()); // catch event that there's no activity to handle intent if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show(); } return true; default: return super.onOptionsItemSelected(item); } }
上面就是简单的通过Appcompat实现Action Bar,想自定义各种属性请参考官方文档。
demo下载:demo
/*** @author 张兴业* http://blog.csdn.net/xyz_lmn* iOS入门群:83702688
* android开发进阶群:241395671* 我的新浪微博:@张兴业TBOW*/
参考:
http://antonioleiva.com/actionbarcompat-how-to-use/
http://antonioleiva.com/actionbarcompat-action-views/
http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html
每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了。Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计。如果对ActionBar不太熟悉的可以参考《
Android UI开发第二十四篇——Action Bar》。ActionBar的API被添加在Android3.0(API 级别 11)中,低版本的还是用不了,根本不能适配支持Android 2.X系列的应用。很幸运有第三方开源的actionbarsherlock支持使得Android 2.1以上的Android应用使用actionbarsherlock定义的Action Bar。这里我们不介绍actionbarsherlock怎么使用,我们介绍一种更新的官方支持的AppCompat 使得Android2.1以上的版本可以实现Action Bar。
Google I/O 2013中AppCompat实现的Action Bar效果
AppCompat在最新的API 18的Android Support Library中。使用AppCompat需要以库的形式引入到应用中,AppCompat在<sdk>/extras/android/support/v7/appcompat/
的位置,需要自行下载,或者升级SDK。
如果应用是使用actionbarsherlock实现的Action Bar,也不必刻意的改成AppCompat。因为actionbarsherlock是一个很稳定的经过很多开发者验证的开发库。
- ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time.
- If you are already using it and do not currently require any of the above then there is no need to migrate.
ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time. If you are already using it and do not currently require any of the above then there is no need to migrate.
1)导入AppCompat库
使用AppCompat第一步需要导入AppCompat库,这一步就不做详细介绍了。
2)修改android:theme
每个使用Action Bar的Activity都应该添加Android:theme
- <activity
- ...
- android:theme="@style/Theme.AppCompat"/>
<activity ... android:theme="@style/Theme.AppCompat" />
或者修改application
- <application
- android:label="@string/app_name"
- android:icon="@drawable/ic_launcher"
- android:theme="@style/Theme.AppCompat"
- android:allowBackup="true">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/Theme.AppCompat" android:allowBackup="true">
3)Activity要继承自ActionBarActivity
实现Action Bar的视图需要继承ActionBarActivity。
4)修改menu的命名空间
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:holo="http://schemas.android.com/apk/res-auto">
- <item
- android:id="@+id/action_websearch"
- android:icon="@drawable/action_search"
- android:title="@string/action_websearch"
- holo:showAsAction="never"/>
- </menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:holo="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_websearch" android:icon="@drawable/action_search" android:title="@string/action_websearch" holo:showAsAction="never"/></menu>
要特别注意的是,通过XML文件来实现Action Item,一定要自定义命名空间,而且该命名空间的后缀一定要和item中showAsAction的前缀一致,本例中为“holo”
显示Menu需要重写onCreateOptionsMenu方法:
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.main, menu);
- return super.onCreateOptionsMenu(menu);
- }
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); }
对Menu的item事件处理需要重写onOptionsItemSelected方法。
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // The action bar home/up action should open or close the drawer.
- // ActionBarDrawerToggle will take care of this.
- if (mDrawerToggle.onOptionsItemSelected(item)) {
- return true;
- }
- // Handle action buttons
- switch(item.getItemId()) {
- case R.id.action_websearch:
- // create intent to perform web search for this planet
- Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
- intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
- // catch event that there's no activity to handle intent
- if (intent.resolveActivity(getPackageManager()) !=null) {
- startActivity(intent);
- } else {
- Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
- }
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
@Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this. if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action buttons switch(item.getItemId()) { case R.id.action_websearch: // create intent to perform web search for this planet Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle()); // catch event that there's no activity to handle intent if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show(); } return true; default: return super.onOptionsItemSelected(item); } }
上面就是简单的通过Appcompat实现Action Bar,想自定义各种属性请参考官方文档。
demo下载:demo
参考:
http://antonioleiva.com/actionbarcompat-how-to-use/
http://antonioleiva.com/actionbarcompat-action-views/
http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html
每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了。Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计。如果对ActionBar不太熟悉的可以参考《
Android UI开发第二十四篇——Action Bar》。ActionBar的API被添加在Android3.0(API 级别 11)中,低版本的还是用不了,根本不能适配支持Android 2.X系列的应用。很幸运有第三方开源的actionbarsherlock支持使得Android 2.1以上的Android应用使用actionbarsherlock定义的Action Bar。这里我们不介绍actionbarsherlock怎么使用,我们介绍一种更新的官方支持的AppCompat 使得Android2.1以上的版本可以实现Action Bar。
Google I/O 2013中AppCompat实现的Action Bar效果
AppCompat在最新的API 18的Android Support Library中。使用AppCompat需要以库的形式引入到应用中,AppCompat在<sdk>/extras/android/support/v7/appcompat/
的位置,需要自行下载,或者升级SDK。
如果应用是使用actionbarsherlock实现的Action Bar,也不必刻意的改成AppCompat。因为actionbarsherlock是一个很稳定的经过很多开发者验证的开发库。
- ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time.
- If you are already using it and do not currently require any of the above then there is no need to migrate.
ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time. If you are already using it and do not currently require any of the above then there is no need to migrate.
1)导入AppCompat库
使用AppCompat第一步需要导入AppCompat库,这一步就不做详细介绍了。
2)修改android:theme
每个使用Action Bar的Activity都应该添加Android:theme
- <activity
- ...
- android:theme="@style/Theme.AppCompat"/>
<activity ... android:theme="@style/Theme.AppCompat" />
或者修改application
- <application
- android:label="@string/app_name"
- android:icon="@drawable/ic_launcher"
- android:theme="@style/Theme.AppCompat"
- android:allowBackup="true">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/Theme.AppCompat" android:allowBackup="true">
3)Activity要继承自ActionBarActivity
实现Action Bar的视图需要继承ActionBarActivity。
4)修改menu的命名空间
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:holo="http://schemas.android.com/apk/res-auto">
- <item
- android:id="@+id/action_websearch"
- android:icon="@drawable/action_search"
- android:title="@string/action_websearch"
- holo:showAsAction="never"/>
- </menu>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:holo="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_websearch" android:icon="@drawable/action_search" android:title="@string/action_websearch" holo:showAsAction="never"/></menu>
要特别注意的是,通过XML文件来实现Action Item,一定要自定义命名空间,而且该命名空间的后缀一定要和item中showAsAction的前缀一致,本例中为“holo”
显示Menu需要重写onCreateOptionsMenu方法:
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.main, menu);
- return super.onCreateOptionsMenu(menu);
- }
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); }
对Menu的item事件处理需要重写onOptionsItemSelected方法。
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // The action bar home/up action should open or close the drawer.
- // ActionBarDrawerToggle will take care of this.
- if (mDrawerToggle.onOptionsItemSelected(item)) {
- return true;
- }
- // Handle action buttons
- switch(item.getItemId()) {
- case R.id.action_websearch:
- // create intent to perform web search for this planet
- Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
- intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
- // catch event that there's no activity to handle intent
- if (intent.resolveActivity(getPackageManager()) !=null) {
- startActivity(intent);
- } else {
- Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
- }
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
@Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this. if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action buttons switch(item.getItemId()) { case R.id.action_websearch: // create intent to perform web search for this planet Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle()); // catch event that there's no activity to handle intent if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show(); } return true; default: return super.onOptionsItemSelected(item); } }
上面就是简单的通过Appcompat实现Action Bar,想自定义各种属性请参考官方文档。
demo下载:demo
参考:
http://antonioleiva.com/actionbarcompat-how-to-use/
http://antonioleiva.com/actionbarcompat-action-views/
http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html
- Android UI开发第三十五篇——AppCompat实现Action Bar
- Android UI开发第三十五篇——AppCompat实现Action Bar
- Android UI开发第三十五篇——AppCompat实现Action Bar
- Android UI开发——AppCompat实现Action Bar
- Android UI开发第二十五篇——分享一篇自定义的 Action Bar
- Android UI开发第二十五篇——分享一篇自定义的 Action Bar
- Android UI开发——Action Bar
- Android UI开发——Action Bar
- Android UI开发第二十四篇——Action Bar
- Android UI开发第二十四篇——Action Bar
- Android UI开发第二十四篇——Action Bar
- Android UI开发第二十四篇——Action Bar
- Android UI开发第二十四篇——Action Bar
- Android UI开发第二十四篇——Action Bar
- Android UI开发第二十四篇——Action Bar
- Android UI开发第二十四篇——Action Bar
- Android UI开发第二十四篇——Action Bar
- Android UI开发第二十四篇——Action Bar
- 如何用C++实现浏览器弹窗
- Java编程之获取计算机的MAC地址
- 上传进行验证时,出现了 Invalid Code Signing Entitlements 的错误,百度以后发现有多原因可以引发这个错误。
- AVS编码---lencod.c中函数流程以及各函数作用
- Android自定义Dialog 及位置和大小
- Android UI开发第三十五篇——AppCompat实现Action Bar
- Faunus Getting Started
- java类初始化和对象初始化
- WPF 依赖属性使用
- eclipse,myeclipse默认字符集修改(UTF-8)
- 【Unity3D】关于Assets资源目录结构管理
- ios-面试题_简答题
- XHTML入门
- Starling Feathers Controls NumericStepper