2015-11-19 4 views
-2

Я хочу поместить значок в свой пункт меню, но значок не отображается, я попробовал app:showAsAction="always", но значок отображается на панели инструментов. Но я хочу показать значок с текстом. enter image description hereЗначок меню не отображается в Android

И вот мой код: -

<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.

Любой может мне помочь. Для решения этой ошибки.Спасибо заранее!

ответ

0

Добавить этот код в PopupMenu.

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

Нет Не работает –

+0

Можете ли вы выслать мне код Java? –

+0

см. Мой отредактированный вопрос. –