[英]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 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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.