int*_*thi 12 android menu android-menu android-actionbar
我在操作栏中有一个菜单项.与菜单项图像一起,我需要显示一些与之相关的数字,这些数字会经常变化.我没有使用Action bar sherlock.我不想用它.除此之外,一切正常.在显示的图像中,白色图标颜色图标是我的.我需要动态生成带有红色背景的数字.我怎么能在Android中做到这一点?
这是示例图像:
更新:
我在menu.xml中有这个菜单项.这应该像通知菜单项一样,显示通知计数的数量.我将菜单图标设置为,
menuItem.setIcon(image);
Run Code Online (Sandbox Code Playgroud)
现在,在菜单项的顶部,我需要放置一个文本视图,其中包含通知总数.
是否可以使用viewbadger实现此功能? Github网址
经过对SO的几乎所有资源的大量尝试后,我转向了博客; 成功地.我想分享一下对我有用的东西(Api> = 13); 来源.
让我们从甜蜜的代码开始,使用它的方式:
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;
}
Run Code Online (Sandbox Code Playgroud)
的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>
Run Code Online (Sandbox Code Playgroud)
这个班级方便了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;
}
}
Run Code Online (Sandbox Code Playgroud)
这个类有助于设置数字.我建议实施更多的方法来设置徽章作为日期等:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
而mui importante drawable(如布局)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>
Run Code Online (Sandbox Code Playgroud)
祝你好运!
归档时间: |
|
查看次数: |
25987 次 |
最近记录: |