2013-08-09 14 views
24

Eylem çubuğunda bildirim sayısının içinde bir bildirim simgesi yapmak istiyorum. ÖrneğinEylem çubuğunda bildirim sayısı olan bir simge nasıl oluşturulur?

(Google Adsence):

Action Bar Google Adsence

ben stackoverflow bu cevabı bulundu, ancak bu durumda bir sayı ile tek sayı değil bir simge olduğu için tam olarak soruma cevap vermez : Actionbar notification icon with count

+1

Bu yaklaşım diğer cevaplarla aynı olacaktır. Sadece arka plan için farklı bir resim oluşturmalısınız. Dokuz yamanın kullanılması, görüntüdeki gibi numarayı kolayca dengelemenize izin verir. – Grimmace

+1

@Grimmace haklı. Bence şu soruları arıyorsun: http://stackoverflow.com/questions/6011786/add-new-item-count-to-icon-on-button-android http://stackoverflow.com/questions/13288989/ nasıl yapılır-metin-on-a-actionbar-simge Sorununuzu çözer umuyoruz. –

cevap

29

Hemen hemen tüm kaynakları denedikten sonra, bloglara yöneldim; Başarıyla. Benim için çalışılan şeyi paylaşmak istiyorum (Api> = 13).

en ile bu kodda kullanılan yol başlayalım:

public boolean onCreateOptionsMenu(Menu menu) { 
    //inflate menu 
    getMenuInflater().inflate(R.menu.menu_my, menu); 

    // Get the notifications MenuItem and LayerDrawable (layer-list) 
    MenuItem item = menu.findItem(R.id.action_notifications); 
    LayerDrawable icon = (LayerDrawable) item.getIcon(); 

    // Update LayerDrawable's BadgeDrawable 
    Utils2.setBadgeCount(this, icon, 2); 

    return true; 
} 

menu_my.xml:

<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_notifications" 
     android:icon="@drawable/ic_menu_notifications" 
     android:title="Notifications" 
     app:showAsAction="always" /> 
</menu> 

elverişli bir BadgeDrawable yapar Bu sınıf:

public class BadgeDrawable extends Drawable { 

    private float mTextSize; 
    private Paint mBadgePaint; 
    private Paint mTextPaint; 
    private Rect mTxtRect = new Rect(); 

    private String mCount = ""; 
    private boolean mWillDraw = false; 

    public BadgeDrawable(Context context) { 
     //mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); 
     mTextSize = 12F; 

     mBadgePaint = new Paint(); 
     mBadgePaint.setColor(Color.RED); 
     mBadgePaint.setAntiAlias(true); 
     mBadgePaint.setStyle(Paint.Style.FILL); 

     mTextPaint = new Paint(); 
     mTextPaint.setColor(Color.WHITE); 
     mTextPaint.setTypeface(Typeface.DEFAULT_BOLD); 
     mTextPaint.setTextSize(mTextSize); 
     mTextPaint.setAntiAlias(true); 
     mTextPaint.setTextAlign(Paint.Align.CENTER); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     if (!mWillDraw) { 
      return; 
     } 

     Rect bounds = getBounds(); 
     float width = bounds.right - bounds.left; 
     float height = bounds.bottom - bounds.top; 

     // Position the badge in the top-right quadrant of the icon. 
     float radius = ((Math.min(width, height)/2) - 1)/2; 
     float centerX = width - radius - 1; 
     float centerY = radius + 1; 

     // Draw badge circle. 
     canvas.drawCircle(centerX, centerY, radius, mBadgePaint); 

     // Draw badge count text inside the circle. 
     mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect); 
     float textHeight = mTxtRect.bottom - mTxtRect.top; 
     float textY = centerY + (textHeight/2f); 
     canvas.drawText(mCount, centerX, textY, mTextPaint); 
    } 

    /* 
    Sets the count (i.e notifications) to display. 
    */ 
    public void setCount(int count) { 
     mCount = Integer.toString(count); 

     // Only draw a badge if there are notifications. 
     mWillDraw = count > 0; 
     invalidateSelf(); 
    } 

    @Override 
    public void setAlpha(int alpha) { 
     // do nothing 
    } 

    @Override 
    public void setColorFilter(ColorFilter cf) { 
     // do nothing 
    } 

    @Override 
    public int getOpacity() { 
     return PixelFormat.UNKNOWN; 
    } 
} 

Bu sınıf bu ayarlamaya yardımcı olur numara. res/drawable yılında

public class Utils2 { 
    public static void setBadgeCount(Context context, LayerDrawable icon, int count) { 

     BadgeDrawable badge; 

     // Reuse drawable if possible 
     Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge); 
     if (reuse != null && reuse instanceof BadgeDrawable) { 
      badge = (BadgeDrawable) reuse; 
     } else { 
      badge = new BadgeDrawable(context); 
     } 

     badge.setCount(count); 
     icon.mutate(); 
     icon.setDrawableByLayerId(R.id.ic_badge, badge); 
    } 


} 

Ve mui importante (bir düzen gibi) çizilebilir:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/ic_notification" 
     android:drawable="@drawable/ice_skate" 
     android:gravity="center" /> 

    <!-- set a place holder Drawable so android:drawable isn't null --> 
    <item 
     android:id="@+id/ic_badge" 
     android:drawable="@drawable/ice_skate" /> 
</layer-list> 

İyi şanslar!

+0

Bunu yapmanın daha iyi bir yolu olabilir. http://stackoverflow.com/questions/17696486/actionbar-notification-count-badge-like-google-has – Bikash

+0

Kodu inceledim. Bunun neden daha iyi olacağını söylemeye yardımcı olacağını düşünüyorum. Teşekkürler. – msysmilu

+0

_Source bağlantısı_ öldü veya böyle bir bağlantı yok, lütfen güncelleyin @msysmilu –

İlgili konular