Android 如何完美解决MenuItem偏移问题

 有时系统的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的偏移问题







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值