初识ActionBar
1.JAVA代码
1 package com.bwf.a13_actionbar; 2 3 import android.os.Bundle; 4 import android.app.ActionBar; 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.graphics.Color; 8 import android.graphics.drawable.ColorDrawable; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.Window; 12 import android.widget.Toast; 13 /** 14 * 1. 显示选项菜单 15 2. 提供标签页的切换方式的导航功能,可以切换多个fragment. 16 3. 提供下拉的导航条目. 17 4. 提供交互式活动视图代替选项条目 18 5. 使用程序的图标作为返回Home主屏或向上的导航操作。 19 * */ 20 public class MainActivity extends Activity { 21 ActionBar actionBar; 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 // rangActionBar浮于窗口上 26 //getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 27 setContentView(R.layout.activity_main); 28 29 // 获取ActionBar 30 actionBar = getActionBar(); 31 // 设置启用HOME图标,使之可点击:4.0以前默认是可点的,4.0以后默认不可点,如果显示了返回图标则也可点击 32 //actionBar.setHomeButtonEnabled(true); 33 // 设置HOME图标左边出现返回图标:默认是不显示的 34 actionBar.setDisplayHomeAsUpEnabled(true); 35 // 启用或者禁用标题:默认是启用的 36 //actionBar.setDisplayShowTitleEnabled(false); 37 // 左上角图标显示或者不显示:默认是显示 38 //actionBar.setDisplayShowHomeEnabled(false); 39 40 // 给ActionBar设置背景颜色 41 //actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#330000ff"))); 42 //actionBar.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 43 } 44 45 /** 46 * 通过布局文件转换菜单项 47 * */ 48 @Override 49 public boolean onCreateOptionsMenu(Menu menu) { 50 // Inflate the menu; this adds items to the action bar if it is present. 51 getMenuInflater().inflate(R.menu.main, menu); 52 return true; 53 } 54 55 /** 56 * 监听选项的点击事件 57 * */ 58 @Override 59 public boolean onOptionsItemSelected(MenuItem item) { 60 // TODO Auto-generated method stub 61 String text = ""; 62 switch (item.getItemId()) { 63 // 监听Home图标 64 case android.R.id.home: 65 text = "返回Home页面"; 66 Intent home = new Intent(Intent.ACTION_MAIN); 67 home.addCategory(Intent.CATEGORY_HOME); 68 startActivity(home); 69 break; 70 case R.id.action_add: 71 text = "添加联系人"; 72 break; 73 case R.id.action_delete: 74 text = "删除联系人"; 75 break; 76 case R.id.action_update: 77 text = "更新联系人"; 78 break; 79 case R.id.action_search: 80 text = "查询联系人"; 81 break; 82 83 default: 84 break; 85 } 86 87 Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show(); 88 return super.onOptionsItemSelected(item); 89 } 90 91 @Override 92 protected void onDestroy() { 93 Toast.makeText(getApplicationContext(), "onDestory", Toast.LENGTH_LONG).show(); 94 super.onDestroy(); 95 } 96 }
2. menu中的menu.xml
1 <menu xmlns:android="http://schemas.android.com/apk/res/android" > 2 <!-- 常用属性 3 android:orderInCategory 属性主要指定Item的排列顺序,越小优先级越高,不指定就按照定义的顺序排列 4 android:showAsAction 属性主要指定Item的显示方式,比如是显示在ActionBar还是在折叠的Menu里等 5 android:icon 属性主要为Item设置一个图标 6 android:title 属性主要为Item设置标题 7 --> 8 <item 9 android:id="@+id/action_update" 10 android:orderInCategory="40" 11 android:showAsAction="ifRoom" 12 android:icon="@drawable/address_book_32" 13 android:title="更新"/> 14 <item 15 android:id="@+id/action_delete" 16 android:orderInCategory="30" 17 android:showAsAction="never" 18 android:icon="@drawable/address_book_close_32" 19 android:title="删除"/> 20 <item 21 android:id="@+id/action_add" 22 android:orderInCategory="20" 23 android:showAsAction="ifRoom" 24 android:icon="@drawable/address_book_add_32" 25 android:title="添加"/> 26 <item 27 android:id="@+id/action_search" 28 android:orderInCategory="10" 29 android:showAsAction="always" 30 android:icon="@drawable/address_book_search_32" 31 android:title="查询"/> 32 33 </menu>
浙公网安备 33010602011771号