2016-01-26 50 views
13

我試圖爲自己找到答案,但無法找到它。如何在工具欄上添加徽章MenuItem圖標

我需要做在工具欄上的菜單項的圖標徽章,像這樣:

enter image description here

我怎樣才能讓這個?

+1

有很多的例子已經在這裏就如何使徽章。你到底有什麼問題? –

回答

2

我認爲這是可能的:

<ImageView 
     android:id="@+id/counterBackground" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/unread_background" /> <!-- your icon --> 

    <TextView 
     android:id="@+id/count" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="1" 
     android:textSize="8sp" 
     android:layout_centerInParent="true" 
     android:textColor="#FFFFFF" /> 

,然後用它爲圖標,background。還有,你需要刪除/禁用默認的圖標了。

你可能想看一看:

Remove App Icon from Navigation Drawer ActionBar Does not work

Remove the navigation drawer icon and use the app icon to open

https://stackoverflow.com/a/29160904/4409113

此外,在[android-sdk]/platforms/android-22/data/res應該有那個圖標,你只需要找到和使用它爲你的目的(例如,添加imageview並添加它像background

請看:https://stackoverflow.com/a/34999691/4409113

13

這裏是一步一步的功能:

添加menu.xml文件

<?xml version="1.0" encoding="utf-8"?> 
    <menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 

     <item 
       android:id="@+id/actionNotifications" 
       android:icon="@drawable/ic_info_outline_white_24dp" 
       android:menuCategory="secondary" 
       android:orderInCategory="1" 
       android:title="Cart" 
       app:actionLayout="@layout/notification_layout" 
       app:showAsAction="always" /> 
    </menu> 

然後加入notification_layout.xml,這種佈局將用作通知圖標佈局

 <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     style="@android:style/Widget.ActionButton" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" 
     android:background="@android:color/transparent" 
     android:clickable="true" 
     android:gravity="center" 
     android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/hotlist_bell" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:contentDescription="Notification Icon" 
     android:gravity="center" 
     android:src="@drawable/ic_info_outline_white_24dp" /> 

    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/txtCount" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="@dimen/x5dp" 
     android:layout_marginLeft="@dimen/x10dp" 
     android:layout_marginRight="0dp" 
     android:background="@drawable/pointer_" 
     android:gravity="center" 
     android:minWidth="17sp" 
     android:text="0" 
     android:textColor="#ffffffff" 
     android:textSize="12sp" /> 
    </RelativeLayout> 

現在裏面活動

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); 

    final View notificaitons = menu.findItem(R.id.actionNotifications).getActionView(); 

    txtViewCount = (TextView) notificaitons.findViewById(R.id.txtCount); 
    updateHotCount(count++); 
    txtViewCount.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      updateHotCount(count++); 
     } 
    }); 
    notificaitons.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
    // TODO 
     } 
    }); 


    return true; 
} 

你可以把下面的函數(從計算器所)的活動中來更新計數器:

public void updateHotCount(final int new_hot_number) { 
    count = new_hot_number; 
    if (count < 0) return; 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      if (count == 0) 
       txtViewCount.setVisibility(View.GONE); 
      else { 
       txtViewCount.setVisibility(View.VISIBLE); 
       txtViewCount.setText(Integer.toString(count)); 
       // supportInvalidateOptionsMenu(); 
      } 
     } 
    }); 
}  
+0

你只是想說在你的下面的代碼: '如果(count == 0) txtViewCount.setVisibility(View.GONE);' –

+0

是如果計數== 0然後View.Gone。 –

+1

那麼,如何將菜單項對齊到工具欄左側? (如上圖) – Mucahit

相關問題