有时系统的MenuItem布局无法满足我们自己系统或者应用的需求,怎样办呢?
下面我以我的经验分享给大家,首先你的需求是否可以需要重新布局,重新布局后,有时还无法达到自己的效果,就要重新编写了,其实有时候,我们只需要一点点偏移就可以了,不用那么麻烦.
下面是具体实现的代码,以Delete为例
private MenuItem mDeleteItem;
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
/// M: Optimize select all performance, restore actionmode status. @{
mListAdapter.clearstate();
// M: add for ALPS01988446, dismiss search view.
mSearchItem.collapseActionView();
if (mIsNeedRestoreAdapterState) {
for (int i = 0; i < mListSelectedThreads.length; i++) {
mListAdapter.setSelectedState(mListSelectedThreads[i]);
}
mIsNeedRestoreAdapterState = false;
} else {
Log.d(TAG, "onCreateActionMode: no need to restore adapter state");
}
/// @}
mSelectedThreadIds = new HashSet<Long>();
inflater.inflate(R.menu.conversation_multi_select_menu_with_selectall, menu);
final Menu m=menu;
mDeleteItem = menu.findItem(R.id.delete);
mDeleteItem.getActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用delete的方法,具体实现点击效果
m.performIdentifierAction(mDeleteItem.getItemId(), 0);
}
});
这里是点击的方法实现
@Override
public boolean onActionItemClicked(final ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
if (mSelectedThreadIds.size() > 0) {
Log.v(TAG, "ConversationList->ModeCallback: delete");
if (mDeleteAlertDialog != null && mDeleteAlertDialog.isShowing()) {
MmsLog.d(TAG, "no need to show delete dialog");
} else {
confirmDeleteThreads(mSelectedThreadIds, mQueryHandler);
}
} else {
item.setEnabled(false);
}
break;
在xml中使用layout添加自己想要的布局
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/delete"
android:title="@string/delete"
android:actionLayout="@layout/zzz_menu_actionlayout"
android:showAsAction="always" />
</menu>
具体的布局zzz_menu_actionlayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="10dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="@drawable/ic_menu_trash_holo_dark"
android:background="?android:attr/selectableItemBackground"
/>
</LinearLayout>
这样就可以实现menuItem的偏移问题