2012-11-26 26 views
12

アクションバーにメニュー項目があります。メニュー項目の画像に加えて、頻繁に変更されるいくつかの番号を表示する必要があります。私はアクションバーのsherlockを使用していません。私はそれを使用したくない。これ以外のものはすべて正常に動作します。表示されている画像では、白いアイコンの色アイコンが私のものです。私は赤色の背景色を動的に生成する必要があります。 Androidでどうすればいいですか?Androidのメニュー項目で動的値を実装する方法

enter image description here

更新:

私はmenu.xmlでこのメニュー項目を持っている。ここ

はサンプル画像です。これは、通知数を示す通知メニュー項目のように機能するはずです。

menuItem.setIcon(image); 

ここで、メニュー項目の上に、合計通知数を持つ1つのテキストビューを配置する必要があります。

ビューバッダーでこの機能を実装することは可能ですか?

は、画像の上に背景とテキストであなたのイメージをペイントカスタム Drawableを作成します。 Github url

答えて

1

は、ここにあなたが試すことができます一つのことです。サンプルについてはthis postをご覧ください。

その後MenuItem背景動的...

+0

私はこれを試します – intrepidkarthi

0

使用アクションビューとしてこのDrawableを設定します。これはデフォルトのActionBarActionBarSherlockの両方で動作します。あなたは自分の行動のビューがサブクラスである場合は、動的に別のビューを追加、(例えば、いくつかのレイアウトを膨らませることによって)独自のViewを作成します(変更の背景、変更内容をやりたいことができます。このアプローチでは

Here is an example

ViewGroupなど)。

5

SO私がブログになって上のほぼすべてのリソースをしようと、多くの後。うまくいった。私は私のために働いたものを共有したい(Api> = 13)。 source

のは甘いのコードを見てみましょう、方法は、それが使われています:

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> 

便利BadgeDrawableなります。このクラス。その外観も同様に変更することができます:

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

番号を設定するのに役立つこのクラス。私はなど、日付としてバッジを設定し、さらにthodsを実装するお勧めします。

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


} 

そしてMUIの重要項目(レイアウトなど)描画可能res/drawableに:

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

頑張ってね!

+0

ic_menu_notificationsは下部に記載したレイヤーリストファイルですか? – Lampard

+0

ソースからコードをコピーしました。それをやめてください。 – Mahori

+0

@Ravi、それは私がしたことではない。ソースリンクはもはや動作しません。私はここに投稿する前にこのコードが私のアプリでプロダクションにあったことを保証することができます。この5ブロックのコードがどのように機能するかを説明することが重要です。 – msysmilu

関連する問題