android工具栏设为底层,android-将带有计数器的按钮添加到工具栏中

这应该是实现这一目标的方法

>创建一个BadgeDrawable类,该类将绘制通知数量以及一个醒目的红色背景圆圈(如屏幕截图所示).

>以XML定义LayerDrawable,以将其设置为MenuItem的android:icon. LayerDrawable的底层是MenuItem的图标(通常是.png),顶层是BadgeDrawable.

>模拟查询活动中通知的计数,更新BadgeDrawable的计数,然后调用Activity#invalidateOptionsMenu()以强制刷新ActionBar菜单.

输出将是

OiZFb.png

BadgeDrawable.java

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

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;

}

}

对于/res/drawable/ic_menu_notifications.xml

android:id="@+id/ic_notification"

android:drawable="@drawable/ic_action_email"

android:gravity="center" />

android:id="@+id/ic_badge"

android:drawable="@drawable/ic_action_email" />

/res/menu/menu_home.xml

android:title="Notifications"

android:icon="@drawable/ic_menu_notifications"

android:showAsAction="always"/>

现在在HomeActivity.java中

public class HomeActivity extends Activity {

private int mNotificationsCount = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_home);

// Run a task to fetch the notifications count

new FetchCountTask().execute();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.home, menu);

// Get the notifications MenuItem and

// its LayerDrawable (layer-list)

MenuItem item = menu.findItem(R.id.action_notifications);

LayerDrawable icon = (LayerDrawable) item.getIcon();

// Update LayerDrawable's BadgeDrawable

Utils.setBadgeCount(this, icon, mNotificationsCount);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

if (item.getItemId() == R.id.action_notifications) {

// TODO: display unread notifications.

return true;

}

return super.onOptionsItemSelected(item);

}

/*

Updates the count of notifications in the ActionBar.

*/

private void updateNotificationsBadge(int count) {

mNotificationsCount = count;

// force the ActionBar to relayout its MenuItems.

// onCreateOptionsMenu(Menu) will be called again.

invalidateOptionsMenu();

}

/*

Sample AsyncTask to fetch the notifications count

*/

class FetchCountTask extends AsyncTask {

@Override

protected Integer doInBackground(Void... params) {

// example count. This is where you'd

// query your data store for the actual count.

return 5;

}

@Override

public void onPostExecute(Integer count) {

updateNotificationsBadge(count);

}

}

}

实用程序类Utils.java

public class Utils {

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值