簡體   English   中英

菜單圖標未在 Android 中顯示

[英]Menu Icon is not showing in Android

我想把圖標放在我的菜單項中,但圖標沒有顯示我試過app:showAsAction="always"這個但圖標顯示在工具app:showAsAction="always"但我想顯示帶有文本的圖標。 在此處輸入圖片說明

這是我的代碼:-

    <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 的左側。

任何人都可以幫助我。解決了這個錯誤。提前致謝!

將此添加到 PopupMenu 的代碼中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM