2016-01-26 44 views
13

Tôi đã cố gắng tìm câu trả lời cho chính mình nhưng không thể tìm thấy câu trả lời đó.Cách thêm huy hiệu trên Thanh công cụ MenuItem Biểu tượng

tôi cần làm huy hiệu vào biểu tượng MenuItem trong Thanh công cụ, như thế này:

enter image description here

Làm thế nào tôi có thể làm điều này?

+1

Có rất nhiều ví dụ ở đây đã về cách làm phù hiệu. Bạn đang gặp phải vấn đề gì? –

Trả lời

2

Tôi nghĩ rằng có thể có:

<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" /> 

Và sau đó sử dụng nó cho biểu tượng đó như background .also, bạn cần phải loại bỏ/vô hiệu hóa các biểu tượng mặc định quá.

Bạn có thể muốn có một cái nhìn:

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

Ngoài ra, trong [android-sdk]/platforms/android-22/data/res nên có biểu tượng đó, bạn chỉ cần thấy rằng, sử dụng cho mục đích của bạn (ví dụ: thêm số lần xem hình ảnh đó và thêm hình ảnh đó như background)

Hãy xem:https://stackoverflow.com/a/34999691/4409113

13

Dưới đây là từng chức năng bước:

thêm 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> 

Sau đó thêm notification_layout.xml, bố cục này sẽ được sử dụng như thông báo biểu tượng bố cục

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

hiện đang hoạt động

@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; 
} 

Bạn có thể đặt chức năng sau (lấy từ stackoverflow) bên trong hoạt động để cập nhật truy cập:

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

Bạn chỉ muốn nói bằng mã thấp hơn: 'if (count == 0) txtViewCount.setVisibility (View.GONE);' –

+0

Có nếu đếm == 0 thì View.Gone. –

+1

Vì vậy, làm cách nào tôi có thể căn chỉnh mục menu ở bên trái của thanh công cụ? (Như hình trên) – Mucahit

Các vấn đề liên quan