您的位置:首页 > 移动开发 > Android开发

Android ActionBar 使用总结

2016-03-08 17:53 344 查看
本文根据Android Developer和SDK example的总结:

这里所使用的都是appcompat v7 support library里面的Actionbar。

添加ActionBar

创建一个Activity继承ActionBarActivity.

给你的Activity指定“Theme.AppCompat”或者继承它的一个主题。 例如:

<activity android:theme="@style/Theme.AppCompat.Light" ... >


删除ActionBar

  可以在运行时隐藏ActionBar。例如:

ActionBar actionBar = getSupportActionBar();
actionBar.hide();


关于ActionBar的icon和logo

如果使用的是Material主题(>=API21)的时候,Navigation button(以前是“Home”)代替了之前应用icon的位置。也就是说L之后ActionBar左上角默认是Navigation button了而不是应用的icon或者logo。

通过调用getSupportActionBar().setDisplayOptions 修改DisplayOptions来改变ActionBar的显示内容,DisplayOptions常量有5个,可以结合起来使用。

DisplayOptionsFunctionsDescription
DISPLAY_USE_LOGOsetDisplayUseLogoEnabled用LOGO代替标准的icon
DISPLAY_SHOW_HOMEsetDisplayShowHomeEnabled显示Home元素,包括logo和icon
DISPLAY_HOME_AS_UPsetDisplayHomeAsUpEnabled显示Home元素并且承担向上导航的功能(可以显示返回箭头)
DISPLAY_SHOW_TITLEsetDisplayShowTitleEnabled显示Title和Subtitle
DISPLAY_SHOW_CUSTOMsetDisplayShowCustomEnabled显示用户自定义的View

Add and Handle Action Items

添加menu资源,要注意app:showAsAction 而不是 android:showAsAction,因为我们用的support library里面的ActionBar。 至于showAsAction的取值参考官网api。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<item android:id="@+id/menu_refresh"
android:title="Refresh"
android:icon="@drawable/ic_action_refresh"
app:showAsAction="ifRoom"/>   <!-- Using XML attributes from the support library -->

<item android:id="@+id/menu_settings"
android:title="Settings"
android:icon="@drawable/ic_action_settings"
app:showAsAction="never"/>
</menu>


在onCreateOptionsMenu中加载menu资源,可以通过menu.add来添加menu item,当然调用showAsAction时一定要用MenuItemCompat.setShowAsAction

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_actionbarcompat_basic, menu);

MenuItem locationItem = menu.add(Menu.NONE, R.id.menu_location, 0, "Location");
locationItem.setIcon(R.drawable.ic_action_location);
// Need to use MenuItemCompat methods to call any action item related methods
MenuItemCompat.setShowAsAction(locationItem, MenuItem.SHOW_AS_ACTION_IF_ROOM);

return true;
}


List Popup Menu

弹出菜单,PopupMenu 可以根据 构造函数里的new PopupMenu(getActivity(), view) 参数 view来设置弹出位置的锚点。

private void showPopupMenu(View view) {
final PopupAdapter adapter = (PopupAdapter) getListAdapter();

// Retrieve the clicked item from view's tag
final String item = (String) view.getTag();

// Create a PopupMenu, giving it the clicked view for an anchor
PopupMenu popup = new PopupMenu(getActivity(), view);

// Inflate our menu resource into the PopupMenu's Menu
popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

// Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_remove:
// Remove the item from the adapter
adapter.remove(item);
return true;
}
return false;
}
});

// Finally show the PopupMenu
popup.show();
}


添加一个Action Provider

使用 ShareActionProvider

要使用带有“ShareActionProvider”的share action,需要给item加一个tag指定ShareActionProvider class,如下:

<?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/menu_share"
android:title="@string/menu_share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
</menu>


在onCreateOptionsMenu中为ShareActionProvider设置Intent。

private ShareActionProvider mShareActionProvider;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);

// Set up ShareActionProvider's default share intent
MenuItem shareItem = menu.findItem(R.id.menu_share);
mShareActionProvider = (ShareActionProvider)
MenuItemCompat.getActionProvider(shareItem);
mShareActionProvider.setShareIntent(getDefaultIntent());

return super.onCreateOptionsMenu(menu);
}

/** Defines a default (dummy) share intent to initialize the action provider.
* However, as soon as the actual content to be used in the intent
* is known or changes, you must update the share intent by again calling
* mShareActionProvider.setShareIntent()
*/
private Intent getDefaultIntent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
//intent.putExtra(Intent.EXTRA_STREAM, getContentUri());  // 文件Uri
// intent.setType("text/plain");
//  intent.putExtra(Intent.EXTRA_TEXT, context.getString(contentResourceId)); // 文字
return intent;
}


split action bar

split action bar可以分离bar到屏幕的底部去显示所有的action items,前提是在小屏幕的手机上。(Android 5.0之后似乎被废弃了,官方文档上没有明确说明,但是设置了却不起作用。在设置了holo主题后才有效果@android:style/Theme.Holo.Light.DarkActionBar)

开始分离action bar的功能需要做两个事情。

Add uiOptions=”splitActionBarWhenNarrow” to each element or to the element. This attribute is understood only by API level 14 and higher (it is ignored by older versions).

To support older versions, add a element as a child of each element that declares the same value for “android.support.UI_OPTIONS”.

例如:

<manifest ...>
<activity uiOptions="splitActionBarWhenNarrow" ... >
<meta-data android:name="android.support.UI_OPTIONS"
android:value="splitActionBarWhenNarrow" />
</activity>
</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: