0

I want to add a badge with the number of pending events above the menu icon of the application, but as you can see in the image below I have some problems. I'm unable to make the circle opaque, I don't want to see the grey lines behind the circle and the new image is cut on the top

I want to use it on the logo icon of the action bar (the home icon), not in any other menu item of the action bar so custom action view is not an option

enter image description here

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);
    bm = bm.copy(bm.getConfig(), true);

    Paint circlePaint = new Paint();
    circlePaint.setAntiAlias(true);
    circlePaint.setColor(Color.RED);
    circlePaint.setStyle(Paint.Style.FILL);
    //circlePaint.setAlpha(255);

    Paint textPaint = new Paint(); 
    textPaint.setStyle(Style.FILL);  
    textPaint.setColor(Color.WHITE); 
    textPaint.setTextSize(23); 
    textPaint.setFakeBoldText(true);

    Canvas canvas = new Canvas(bm);
    canvas.drawCircle(bm.getWidth()/6, bm.getHeight()/5, 13, circlePaint);
    canvas.drawText(text, bm.getWidth()/7 + 5, bm.getHeight()/3, textPaint);

    return new BitmapDrawable(context.getResources(), bm);

Any help will be appreciated!

Thanks

4

3 回答 3

2

你正在寻找的东西叫做Android View badger你应该检查这个可爱的教程这个......我相信它会对你有很大帮助......享受:):)

于 2013-10-29T09:18:43.917 回答
0

我认为您应该为此使用相对布局,而不是从画布中绘制。

这是一个很好的教程如何使用相对布局

编辑:

对于操作栏

于 2013-10-29T09:07:56.043 回答
0

在 res/menu 中创建一个菜单项并将 actionLayout 设置为您的布局

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/badge"
        android:actionLayout="@layout/actionbar_badge_layout"
        android:icon="@drawable/icn_menu_posts"
        android:showAsAction="always">
    </item>
</menu>

和你的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="48dp"
    android:layout_height="fill_parent"
    android:layout_gravity="right" >

    <!-- Menu Item Image -->
    <ImageView
        android:layout_width="48dp"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:src="@drawable/bkg_actionbar_notify_off" />

    <!-- Badge Count -->    
    <TextView
        android:id="@+id/actionbar_notifcation_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="@dimen/padding_small"
        android:text="99"
        android:textColor="@color/holo_orange_dark" />

</RelativeLayout>

然后在你的活动的 onCreateOptionsMenu 做这样的事情......

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.badge, menu);

    RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
    TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
    tv.setText("12");
}

另请查看此答案

于 2013-10-29T09:18:43.137 回答