简体   繁体   中英

draw text on item menu actionbar

I want to draw an text on an item icon in action bar. my current actionbar is this:

在此处输入图片说明

I want to be like this:

在此处输入图片说明

i tried this code:

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.main, menu);
    shoppingCardTopMenu = menu.findItem(R.id.top_menu_shopping_card);

    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    Bitmap bmp = Bitmap.createBitmap(30, 30, conf);

    Paint paint = new Paint();
    Canvas canvas = new Canvas(bmp);
    canvas.drawPaint(paint);
    paint.setColor(Color.BLACK);
    paint.setTextSize(16);
    canvas.drawText("test", 0, 0, paint);

    shoppingCardTopMenu.getIcon().draw(canvas);

    return true;
}

but not working. is there any solution?

I do not know It will work for you or not but try this way,because I have button,add custom item..

  <item
    android:id="@+id/badge"
    android:actionLayout="@layout/feed_update_count"
    android:icon="@drawable/shape_notification"
    android:showAsAction="always">
 </item>

Button

     <Button xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/notif_count"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:minWidth="32dp"
      android:minHeight="32dp"
      android:background="@drawable/yourthings"
      android:text="0"
      android:textSize="16sp"
      android:textColor="@android:color/white"
      android:gravity="center"
      android:padding="2dp"
      android:singleLine="true">    
     </Button>

Youractivity

static Button notifCount;
static int mNotifCount = 0;    

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

View count = menu.findItem(R.id.badge).getActionView();
notifCount = (Button) count.findViewById(R.id.notif_count);
notifCount.setText(String.valueOf(mNotifCount));
return super.onCreateOptionsMenu(menu);
}

private void setNotifCount(int count){
mNotifCount = count;
invalidateOptionsMenu();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM