<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_pic"
android:title="严振杰的博客"
app:showAsAction="always" />
</menu>
public class BadgeActionProvider extends ActionProvider {
public BadgeActionProvider(Context context) {
super(context);
}
@Override
public View onCreateActionView() {
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?actionBarItemBackground">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="center" />
<TextView
android:id="@+id/tv_badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="2dp"
android:background="@drawable/circle_red"
android:gravity="center"
android:text="0"
android:textColor="@android:color/white"
android:textSize="12sp" />
</RelativeLayout>
public class BadgeActionProvider extends ActionProvider {
private ImageView mIvIcon;
private TextView mTvBadge;
// 用来记录是哪个View的点击,这样外部可以用一个Listener接受多个menu的点击。
private int clickWhat;
private OnClickListener onClickListener;
public BadgeActionProvider(Context context) {
super(context);
}
@Override
public View onCreateActionView() {
int size = getContext().getResources().getDimensionPixelSize(
android.support.design.R.dimen.abc_action_bar_default_height_material);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(size, size);
View view = LayoutInflater.from(getContext())
.inflate(R.layout.menu_badge_provider, null, false);
view.setLayoutParams(layoutParams);
mIvIcon = (ImageView) view.findViewById(R.id.iv_icon);
mTvBadge = (TextView) view.findViewById(R.id.tv_badge);
view.setOnClickListener(onViewClickListener);
return view;
}
// 点击处理。
private View.OnClickListener onViewClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClickListener != null)
onClickListener.onClick(clickWhat);
}
};
// 外部设置监听。
public void setOnClickListener(int what, OnClickListener onClickListener) {
this.clickWhat = what;
this.onClickListener = onClickListener;
}
public interface OnClickListener {
void onClick(int what);
}
}
// 设置图标。
public void setIcon(@DrawableRes int icon) {
mIvIcon.setImageResource(icon);
}
// 设置显示的数字。
public void setBadge(int i) {
mTvBadge.setText(Integer.toString(i));
}
// 设置文字。
public void setTextInt(@StringRes int i) {
mTvBadge.setText(i);
}
// 设置显示的文字。
public void setText(CharSequence i) {
mTvBadge.setText(i);
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_pic"
android:title="严振杰的博客"
app:actionProviderClass="com.yanzhenjie.BadgeActionProvider"
app:showAsAction="always" />
</menu>
private BadgeActionProvider mActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.menu_delete);
mActionProvider = (BadgeActionProvider) MenuItemCompat.getActionProvider(menuItem);
mActionProvider.setOnClickListener(0, onClickListener);// 设置点击监听。
return true;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mActionProvider.setIcon(R.mipmap.ic_action_delete_small);
mActionProvider.setIcon(R.mipmap.ic_action_picture);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有