菜单项的图标从布局

时间:2018-01-11 01:12:33

标签: android xamarin.android

我有一个菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto">
    <group
        android:id="@+id/nav_items">
        <item
            android:id="@+id/nav_home"
            android:title="Home" />
        <item
            android:id="@+id/nav_second_home"
            android:title="Second host home" />
    </group>
</menu>

我想使用我的icon.xml作为项目的图标:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:text="icon1"
    android:textColor="@color/black" />
</FrameLayout>

我正在尝试将icon.xml方法中的菜单项图标设置为OnViewCreated,如下所示:

public override void OnViewCreated(View view, Android.OS.Bundle savedInstanceState)
{
    base.OnViewCreated(view, savedInstanceState);

    var navigationView = view.FindViewById<NavigationView>(Resource.Id.navigation_view);
    navigationView.SetNavigationItemSelectedListener(this);
    navigationView.Menu.FindItem(Resource.Id.nav_home).SetChecked(true);

    var icon = LayoutInflater.Inflate(Resource.Layout.icon, null, false);
    icon.Layout(icon.Left, icon.Top, icon.Right, icon.Bottom);
    icon.FindViewById<TextView>(Resource.Id.textview).Text = "icon2";
    icon.DrawingCacheEnabled = true;
    var iconDrawable = new  BitmapDrawable(icon.GetDrawingCache(true));
    icon.DrawingCacheEnabled = false;
    navigationView.Menu.GetItem(0).SetIcon(iconDrawable);
}

但它不起作用。它需要图标所在的空间,但它是空白的。知道为什么吗?我在Xamarin.Android中编写这个应用程序,但它与原生Android相同,所以在任何一个答案中都会受到赞赏!

2 个答案:

答案 0 :(得分:0)

您可以尝试这样做:

<item
    android:id="@+id/menu_basket_spinner"
    android:title="title"
    app:actionLayout="@layout/your_layout"/>

答案 1 :(得分:0)

对于带图标的菜单项,首先创建一个JAVA文件(IconizedMenu.java),

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="http://vsss.co.in/assets/css/main.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="col-sm-4 col-md-3 wow fadeInUp animated animated" style="visibility: visible; animation-name: fadeInUp; margin-bottom: 15px;">
    <div class="products">
        <div class="product">
            <div class="product-image image_slider">
                <div class="image">
                    <a href="http://vsss.co.in/index.php/Detail/view/84"><img src="http://vsss.co.in/assets/images/No_image.png" alt="" class="slide_active" data-number="0" style="height: 100%; width: auto; max-width: 100%; max-height: 251px; display: inline;">
                        <div style="position: absolute;bottom:97px; right:0px">
                            <button class="btn btn-primary icon transparent shopping_cart" data-item="84" data-toggle="dropdown" type="button"> <i class="fa fa-shopping-cart" style="font-size: 22px"></i> </button>
                        </div>
                    </a>
                </div>
            </div>
            <div class="product-info text-left">
                <h3 class="name"><a href="http://vsss.co.in/index.php/Detail/view/84">2048 TWINKLE POUCH</a></h3>
                <h3 class="price"><i class="fa fa-inr" aria-hidden="true"></i>0/-</h3> </div>
            <div class="product-cart text-left">
                <div class="row">
                    <div class="col-md-12 Addtocart">
                        <div style="width:40%;float: left;">
                            <select>
                                <option value="11" data-unit_value="1">Pc</option>
                            </select>
                        </div>
                        <div style="width:40%;float: left;">
                            <input type="text" placeholder="Quantity"> </div>
                        <div style="width:20%;float: left;">
                            <button class="btn btn-primary icon transparent" type="button"> OK </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

只需将此文件用作

即可
/**
 * Created by PatelD on 04-10-2017.
 */

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.view.SupportMenuInflater;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.view.menu.MenuPopupHelper;
import android.support.v7.view.menu.MenuPresenter;
import android.support.v7.view.menu.SubMenuBuilder;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

public class IconizedMenu implements MenuBuilder.Callback, MenuPresenter.Callback {
    private Context mContext;
    private MenuBuilder mMenu;
    private View mAnchor;
    private MenuPopupHelper mPopup;
    private OnMenuItemClickListener mMenuItemClickListener;
    private OnDismissListener mDismissListener;

    /**
     * Callback interface used to notify the application that the menu has closed.
     */
    public interface OnDismissListener {
        /**
         * Called when the associated menu has been dismissed.
         *
         * @param menu The PopupMenu that was dismissed.
         */
        public void onDismiss(IconizedMenu menu);
    }

    /**
     * Construct a new PopupMenu.
     *
     * @param context Context for the PopupMenu.
     * @param anchor  Anchor view for this popup. The popup will appear below the anchor if there
     *                is room, or above it if there is not.
     */
    @SuppressLint("RestrictedApi")
    public IconizedMenu(Context context, View anchor) {
        mContext = context;
        mMenu = new MenuBuilder(context);
        mMenu.setCallback(this);
        mAnchor = anchor;
        mPopup = new MenuPopupHelper(context, mMenu, anchor);
        mMenu.setCallback(this);
        mPopup.setForceShowIcon(true);
    }

    /**
     * @return the {@link android.view.Menu} associated with this popup. Populate the returned Menu with
     * items before calling {@link #show()}.
     * @see #show()
     * @see #getMenuInflater()
     */
    public Menu getMenu() {
        return mMenu;
    }

    /**
     * @return a {@link android.view.MenuInflater} that can be used to inflate menu items from XML into the
     * menu returned by {@link #getMenu()}.
     * @see #getMenu()
     */
    @SuppressLint("RestrictedApi")
    public MenuInflater getMenuInflater() {
        return new SupportMenuInflater(mContext);
    }

    /**
     * Inflate a menu resource into this PopupMenu. This is equivalent to calling
     * popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu()).
     *
     * @param menuRes Menu resource to inflate
     */
    public void inflate(int menuRes) {
        getMenuInflater().inflate(menuRes, mMenu);
    }

    /**
     * Show the menu popup anchored to the view specified during construction.
     *
     * @see #dismiss()
     */
    @SuppressLint("RestrictedApi")
    public void show() {
        mPopup.show();
    }

    /**
     * Dismiss the menu popup.
     *
     * @see #show()
     */
    @SuppressLint("RestrictedApi")
    public void dismiss() {
        mPopup.dismiss();
    }

    /**
     * Set a listener that will be notified when the user selects an item from the menu.
     *
     * @param listener Listener to notify
     */
    public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
        mMenuItemClickListener = listener;
    }

    /**
     * Set a listener that will be notified when this menu is dismissed.
     *
     * @param listener Listener to notify
     */
    public void setOnDismissListener(OnDismissListener listener) {
        mDismissListener = listener;
    }

    /**
     * @hide
     */
    public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
        if (mMenuItemClickListener != null) {
            return mMenuItemClickListener.onMenuItemClick(item);
        }
        return false;
    }

    /**
     * @hide
     */
    public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
        if (mDismissListener != null) {
            mDismissListener.onDismiss(this);
        }
    }

    /**
     * @hide
     */
    @SuppressLint("RestrictedApi")
    public boolean onOpenSubMenu(MenuBuilder subMenu) {
        if (subMenu == null) return false;

        if (!subMenu.hasVisibleItems()) {
            return true;
        }

        // Current menu will be dismissed by the normal helper, submenu will be shown in its place.
        new MenuPopupHelper(mContext, subMenu, mAnchor).show();
        return true;
    }

    /**
     * @hide
     */
    public void onCloseSubMenu(SubMenuBuilder menu) {
    }

    /**
     * @hide
     */
    public void onMenuModeChange(MenuBuilder menu) {
    }

    /**
     * Interface responsible for receiving menu item click events if the items themselves
     * do not have individual item click listeners.
     */
    public interface OnMenuItemClickListener {
        /**
         * This method will be invoked when a menu item is clicked if the item itself did
         * not already handle the event.
         *
         * @param item {@link MenuItem} that was clicked
         * @return <code>true</code> if the event was handled, <code>false</code> otherwise.
         */
        public boolean onMenuItemClick(MenuItem item);
    }

}

pop_up.xml

IconizedMenu popup = new IconizedMenu(MainActivity.this, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.pop_up, popup.getMenu());

popup.setOnMenuItemClickListener(new IconizedMenu.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        Toast.makeText(MainActivity.this, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
        return false;
    }
});
popup.show();//showing popup menu
相关问题