菜单图标未在Android中显示

时间:2015-11-19 10:37:36

标签: android android-menu

我想在我的菜单项中放置图标,但图标未显示我尝试了app:showAsAction="always"此图标显示在工具栏上。但我想要带文字的显示图标。enter image description here

这是我的代码: -

    <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"
    tools:context=".MainActivity">


 <item
        android:id="@+id/action_search"
        android:title="@string/action_search"
        android:orderInCategory="1"
        android:icon="@drawable/search"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_cart"
        android:title="@string/action_search"
        android:orderInCategory="2"
        android:icon="@drawable/shoppingcart"
        android:actionLayout="@layout/feed_update_count"
        app:showAsAction="ifRoom" />


    <item
        android:id="@+id/login"
        android:title="@string/login"
        android:orderInCategory="1"
        android:icon="@drawable/ic"
       />

    <item
        android:id="@+id/my_order"
        android:title="@string/my_order"
        android:orderInCategory="2"
        android:icon="@drawable/ic"/>

    <item
        android:id="@+id/wishlist"
        android:title="@string/wishlist"
        android:orderInCategory="3"
        android:icon="@drawable/ic"/>

</menu>

我的代码: -

      @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.


        LayoutInflater inflater = LayoutInflater.from(this);
        View count = inflater.inflate(R.layout.feed_update_count, null);
        notifCount = (Button) count.findViewById(R.id.notif_count);
        notifCount.setText(String.valueOf(mNotifCount));
        return super.onCreateOptionsMenu(menu);*/

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


        MenuItem menuItem = menu.findItem(R.id.action_cart);
        menuItem.setIcon(buildCounterDrawable(count, R.drawable.shoppingcart));


          return true;
    }


    private Drawable buildCounterDrawable(int count, int backgroundImageId) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.feed_update_count, null);
        view.setBackgroundResource(backgroundImageId);

        if (count == 0) {
            View counterTextPanel = view.findViewById(R.id.counterValuePanel);
            counterTextPanel.setVisibility(View.GONE);
        } else {
            TextView textView = (TextView) view.findViewById(R.id.count);
            textView.setText("" + count);
        }

        view.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);

        return new BitmapDrawable(getResources(), bitmap);
    }

    private void doIncrease() {
        count++;
        invalidateOptionsMenu();
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()){
            case R.id.action_search:
                Intent search = new Intent(MainActivity.this,SearchActivity.class);
                startActivity(search);
                return true;
            case R.id.login:
                if(session.checkLogin())
                    finish();
                else {
                        Intent intent = new Intent(MainActivity.this,MyAccount.class);
                            startActivity(intent);
                }
                return true;
            case R.id.my_order:
                alert.showAlertDialog(MainActivity.this,"Alert","This Page Under Maintenance",false);
                return true;
            case R.id.wishlist:
                if(session.checkLogin()){
                    finish();
                }
                else {
                    Intent wishlistIntent = new Intent(MainActivity.this,WishList.class);
                    startActivity(wishlistIntent);
                }

                return true;
            case android.R.id.home:
                Intent intent = new Intent(MainActivity.this,MainActivity.class);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

例如: - 我的图标显示在MyAccount的左侧。

Any One可以帮助我。解决了这个错误。提前谢谢!

1 个答案:

答案 0 :(得分:0)

在PopupMenu的代码中添加它。

yourpopmenu.setForceShowIcon(true); //ADD THIS LINE