android ActionBar的使用
Posted yfceshi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
分隔操作栏
当应用程序在Android4.0(API级别14)或以上的版本号执行,那么另一种叫做“分隔操作栏”的额外模式对action bar有效。当你启用分隔操作栏模式时。在屏幕的底部会显示一个独立的横条,用于显示Activity在窄屏幕社保上执行时的全部操作项。
要启用分离式操作栏。仅仅需简单的在<application>或<activity>元素中加入uiOptions=”splitActionBarWhenNarrow”属性设置就能够了。
下图中左边是未设置分离式操作栏。右边图是设置了分离式操作栏:
以上是关于android ActionBar的使用的主要内容,如果未能解决你的问题,请参考以下文章
ClassCastException片段android使用ActionBar
如何更改 Android 中各个片段的 ActionBar 颜色?
如何隐藏默认片段 actionBar 在 android & kotlin 中创建我们自己的 actionBar