http://blog.csdn.net/crazy123456789/article/details/7495622
除了在ActionBar的左侧显示应用图标和Activity的标题,也可以在ActionBar上面显示额外的项目。这些被添加的项目被叫做“action items”。这些项目就是一些在你应用中一些功能的快捷方式。举个例子,你正在开发一块RSS阅读器,那么action items就可能是“刷新”、“删除”和“添加”。
下面的例子演示如何在ActionBar中添加项目。
1、使用上一节的项目,在MyActionBarActivity.java中的代码。
- public class MyActionBarActivityextends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //ActionBar actionBar = getActionBar();
- //actionBar.hide();
- //actionBar.show(); //---show it again---
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- CreateMenu(menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- return MenuChoice(item);
- }
- private void CreateMenu(Menu menu)
- {
- MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
- {
- mnu1.setIcon(R.drawable.ic_launcher);
- mnu1.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM);
- }
- MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
- {
- mnu2.setIcon(R.drawable.ic_launcher);
- mnu2.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM);
- }
- MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
- {
- mnu3.setIcon(R.drawable.ic_launcher);
- mnu3.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM);
- }
- MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
- {
- mnu4.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM |
- MenuItem.SHOW_AS_ACTION_WITH_TEXT);
- }
- MenuItem mnu5 = menu.add(0, 4, 4, "Item 5");
- {
- mnu5.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM);
- }
- }
- private boolean MenuChoice(MenuItem item)
- {
- switch (item.getItemId()) {
- case 0:
- Toast.makeText(this, "You clicked on Item 1",
- Toast.LENGTH_LONG).show();
- return true;
- case 1:
- Toast.makeText(this, "You clicked on Item 2",
- Toast.LENGTH_LONG).show();
- return true;
- case 2:
- Toast.makeText(this, "You clicked on Item 3",
- Toast.LENGTH_LONG).show();
- return true;
- case 3:
- Toast.makeText(this, "You clicked on Item 4",
- Toast.LENGTH_LONG).show();
- return true;
- case 4:
- Toast.makeText(this, "You clicked on Item 5",
- Toast.LENGTH_LONG).show();
- return true;
- }
- return false;
- }
- }
- public class MyActionBarActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //ActionBar actionBar = getActionBar();
- //actionBar.hide();
- //actionBar.show(); //---show it again---
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- CreateMenu(menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- return MenuChoice(item);
- }
- private void CreateMenu(Menu menu)
- {
- MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
- {
- mnu1.setIcon(R.drawable.ic_launcher);
- mnu1.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM);
- }
- MenuItem mnu2 = menu.add(0, 1, 1, "Item 2");
- {
- mnu2.setIcon(R.drawable.ic_launcher);
- mnu2.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM);
- }
- MenuItem mnu3 = menu.add(0, 2, 2, "Item 3");
- {
- mnu3.setIcon(R.drawable.ic_launcher);
- mnu3.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM);
- }
- MenuItem mnu4 = menu.add(0, 3, 3, "Item 4");
- {
- mnu4.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM |
- MenuItem.SHOW_AS_ACTION_WITH_TEXT);
- }
- MenuItem mnu5 = menu.add(0, 4, 4, "Item 5");
- {
- mnu5.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM);
- }
- }
- private boolean MenuChoice(MenuItem item)
- {
- switch (item.getItemId()) {
- case 0:
- Toast.makeText(this, "You clicked on Item 1",
- Toast.LENGTH_LONG).show();
- return true;
- case 1:
- Toast.makeText(this, "You clicked on Item 2",
- Toast.LENGTH_LONG).show();
- return true;
- case 2:
- Toast.makeText(this, "You clicked on Item 3",
- Toast.LENGTH_LONG).show();
- return true;
- case 3:
- Toast.makeText(this, "You clicked on Item 4",
- Toast.LENGTH_LONG).show();
- return true;
- case 4:
- Toast.makeText(this, "You clicked on Item 5",
- Toast.LENGTH_LONG).show();
- return true;
- }
- return false;
- }
- }
如果按菜单按钮,就会看见余下的菜单选项。“悬浮的”菜单。
3、点击菜单项,就会弹出提示。
4、按Ctrl+F11,改变模拟器的显示方向。将会看到四个选项:三个按钮和一个文本框。
http://blog.csdn.net/crazy123456789/article/details/7495590
除了fragments(碎片),在Android3和4中新增加的特性,还有ActionBar(活动栏)。ActionBar位于传统标题栏的位置,就在设备屏幕的顶部。ActionBar显示了应用的图标和Activity的标题。可选择地,ActionBar的右侧是那些action items(活动项目)。下图展示了build-in(内置的)Email应用,显示了应用的图标,Activity(活动)的标题,以及一些活动项目。下一节将详细地讨论action items(活动项目)。
下面的例子,将会介绍如何通过编写代码去隐藏和显示ActionBar。
1、创建一个工程,MyActionBar。
2、按F11在模拟器上面调试。此时,你就会看见ActionBar被放置在了屏幕的顶端(包含应用的图标和应用的名称“MyActionBar”)。如下图。
3、想要隐藏ActionBar,可以在AndroidManifest.xml中加入以下代码。
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="net.learn2develop.MyActionBar"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdkandroid:minSdkVersion="13"/>
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name">
- <activity
- android:label="@string/app_name"
- android:name=".MyActionBarActivity"
- <!-- 注意这行代码 -->
- android:theme="@android:style/Theme.Holo.NoActionBar">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="net.learn2develop.MyActionBar"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="13" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:label="@string/app_name"
- android:name=".MyActionBarActivity"
- <!-- 注意这行代码 -->
- android:theme="@android:style/Theme.Holo.NoActionBar">
- <intent-filter >
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
5、也可以通过编写代码的方式去移除ActionBar,但这要使用ActionBar类。如果想这么做的话,首先就得把AndroidManifest.xml中的android:theme属性去掉,否则,将会引起一个Exception(异常)。
6、在MyActionBar.java文件中加入如下代码。
- public class MyActionBarActivityextends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 获取ActionBar实例
- ActionBar actionBar = getActionBar();
- // 隐藏
- actionBar.hide();
- // 显示
- //actionBar.show();
- }
- }
- public class MyActionBarActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 获取ActionBar实例
- ActionBar actionBar = getActionBar();
- // 隐藏
- actionBar.hide();
- // 显示
- //actionBar.show();
- }
- }
注意:
如果在AndroidManifest.xml中定义了主题,把ActionBar给隐藏了。在程序运行时,如果执行getActionBar()方法,就会返回一个null。所以,一般情况下,总是通过编写代码的方式去控制ActionBar的显示和隐藏。
http://blog.csdn.net/crazy123456789/article/details/7495639
菜单选项是没有文字的。如果想要同时显示图标和文字,可以一起使用“|”和MenuItem.SHOW_AS_ACTION_WITH_TEXT常量。
- MenuItem mnu1 = menu.add(0,0, 0,"Item 1");
- {
- mnu1.setIcon(R.drawable.ic_launcher);
- mnu1.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM |
- MenuItem.SHOW_AS_ACTION_WITH_TEXT);
- }
- MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");
- {
- mnu1.setIcon(R.drawable.ic_launcher);
- mnu1.setShowAsAction(
- MenuItem.SHOW_AS_ACTION_IF_ROOM |
- MenuItem.SHOW_AS_ACTION_WITH_TEXT);
- }
除了可以点击Action项目,也可以点击ActionBar上面的应用图标。当应用图标被点击的时候,会调用onOptionsItemSelected()方法。如果想识别应用图标被点击了,可以使用adnroid.R.id.home常量。
- private boolean MenuChoice(MenuItem item)
- {
- switch (item.getItemId()) {
- case android.R.id.home:
- Toast.makeText(this,
- "You clicked on the Application icon",
- Toast.LENGTH_LONG).show();
- return true;
- case 0:
- Toast.makeText(this, "You clicked on Item 1",
- Toast.LENGTH_LONG).show();
- return true;
- case 1:
- ......
- }
- private boolean MenuChoice(MenuItem item)
- {
- switch (item.getItemId()) {
- case android.R.id.home:
- Toast.makeText(this,
- "You clicked on the Application icon",
- Toast.LENGTH_LONG).show();
- return true;
- case 0:
- Toast.makeText(this, "You clicked on Item 1",
- Toast.LENGTH_LONG).show();
- return true;
- case 1:
- ......
- }
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ActionBar actionBar = getActionBar();
- actionBar.setDisplayHomeAsUpEnabled(true);
- //actionBar.hide();
- //actionBar.show(); //---show it again---
- }
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ActionBar actionBar = getActionBar();
- actionBar.setDisplayHomeAsUpEnabled(true);
- //actionBar.hide();
- //actionBar.show(); //---show it again---
- }
一般情况下,一个应用可能包含很多activity,那么应用的图标通常被用来返回主Activity。如果想这么做,就需要一个Intent对象,并使用Intent.FLAG_ACTIVITY_CLEAR_TOP标志。
- case android.R.id.home:
- Toast.makeText(this,
- "You clicked on the Application icon",
- Toast.LENGTH_LONG).show();
- Intent i = new Intent(this, MyActionBarActivity.class);
- i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(i);
- return true;
- case android.R.id.home:
- Toast.makeText(this,
- "You clicked on the Application icon",
- Toast.LENGTH_LONG).show();
- Intent i = new Intent(this, MyActionBarActivity.class);
- i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(i);
- return true;