首页 > 代码库 > android ActionBar的使用
android ActionBar的使用
Action Bar主要功能包含:
1. 显示选项菜单
2. 提供标签页的切换方式的导航功能,可以切换多个fragment.
3. 提供下拉的导航条目.
4. 提供交互式活动视图代替选项条目
5. 使用程序的图标作为返回Home主屏或向上的导航操作。
首先说下,使用OverFlow的时候需要在onCreate()函数中调用如下方法:
private void forceShowOverflowMenu() { try { ViewConfiguration config = ViewConfiguration.get(this); Field menuKeyField = ViewConfiguration.class .getDeclaredField("sHasPermanentMenuKey"); if (menuKeyField != null) { menuKeyField.setAccessible(true); menuKeyField.setBoolean(config, false); } } catch (Exception e) { e.printStackTrace(); } }
注:此处未使用这种实现方式,本应用中使用的PopupMen
以下是自己开发的项目所使用到的ActionBar:
public class ShopOrderActivity extends ActionBarActivity implements OnTouchListener, OnMenuItemClickListener{ private Toast mToast; private Context context; /** AlertDialog中输入反馈框 */ private EditText et_FeedBack; private PopupMenu popupMenu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shop_order); //设置Home图标区域 // requestWindowFeature(Window.FEATURE_LEFT_ICON); // setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, resId); this.context = this; mToast = Toast.makeText(this, "", Toast.LENGTH_LONG); initActionBar(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.actionbar_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.actionbar_overflow) { if (popupMenu == null) { popupMenu = new PopupMenu(this, findViewById(R.id.actionbar_overflow)); popupMenu.inflate(R.menu.actionbar_pop); popupMenu.setOnMenuItemClickListener(this); } popupMenu.show(); return true; } else if (<pre name="code" class="java">public class ShopOrderActivity extends ActionBarActivity implements OnTouchListener, OnMenuItemClickListener{ private Toast mToast; private Context context; /** AlertDialog中输入反馈框 */ private EditText et_FeedBack; private PopupMenu popupMenu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shop_order); //设置Home图标区域 // requestWindowFeature(Window.FEATURE_LEFT_ICON); // setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, resId); this.context = this; mToast = Toast.makeText(this, "", Toast.LENGTH_LONG); initActionBar(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.actionbar_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.actionbar_overflow) { if (popupMenu == null) { popupMenu = new PopupMenu(this, findViewById(R.id.actionbar_overflow)); popupMenu.inflate(R.menu.actionbar_pop); popupMenu.setOnMenuItemClickListener(this); } popupMenu.show(); return true; } else if (id == android.R.id.home) { finish(); } else if (id == R.id.new_order_bar) { showTip("显示红点"); } return super.onOptionsItemSelected(item); } @Override public boolean onMenuItemClick(MenuItem arg0) { Intent intent; switch (arg0.getItemId()) { case R.id.actionbar_settings: intent = new Intent(this, SettingsActivity.class); startActivity(intent); break; case R.id.historyOrder: intent = new Intent(this, HistoryOrderActivity.class); startActivity(intent); break; case R.id.logout: AlertDialog.Builder builder = new AlertDialog.Builder( ShopOrderActivity.this); builder.setTitle("确定要退出吗?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showTip("退出登录!"); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); break; } return false; } /** * 初始化ActionBar */ private void initActionBar() { ActionBar actionBar = getSupportActionBar(); actionBar.setTitle("抢单"); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true); // actionBar.setIcon(R.drawable.back_menu); // Drawable background = (Drawable) getResources() // .getDrawable(R.drawable.top_bg); // getActionBar().setBackgroundDrawable(background); setTitleColor(this.getResources().getColor(R.color.green));// 没反应 } /** * 显示Toast * * @param str */ public void showTip(final String str) { runOnUiThread(new Runnable() { @Override public void run() { mToast.setText(str); mToast.show(); } }); }
}
id == android.R.id.home当操作左上角icon图标的时候实现的功能是返回,配置清单需要设置:
<activity
android:name="com.shop.order.ShopOrderActivity"
android:launchMode="singleTop"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
</activity>
actionbar_menu的代码:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/new_order_bar" android:title="@string/new_order" android:icon="@drawable/ic_launcher" app:showAsAction="ifRoom|withText"/> <item android:id="@+id/actionbar_overflow" android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_dark" android:title="@string/pop" app:showAsAction="ifRoom|withText"/> </menu>
注意:app:showAsAction="ifRoom|withText"如果写成android:showAsAction="ifRoom|withText"则不会在actionbar显示,当操作手机右下角的menu键时才会显示
actionbar_pop的代码:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/actionbar_settings" android:title="@string/settings"/> <item android:id="@+id/historyOrder" android:title="@string/historyorder"/> <item android:id="@+id/logout" android:title="@string/logout"/> </menu>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。