notification 修改打开之后的图片,最好的办法是使用contentView然后调用布局,然而有时候却要面临不能使用布局的情况也就是你不能使用R.layout。xx,这个样子你就不能动态修改icon的图标了。
换一个思路就是调用系统的布局 获得系统显示图片的View 然后重新设置他的图片就可以了
private boolean recurseGroup(ViewGroup gp)
{
final int count = gp.getChildCount();
for (int i = 0; i < count; ++i)
{
if (gp.getChildAt(i) instanceof ImageView)
{
Bitmap mIcon=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
notification.contentView.setImageViewBitmap(((ImageView) gp.getChildAt(i)).getId(), mIcon);
mNotification.notify(ID, notification);
return true;
}else if (gp.getChildAt(i) instanceof ViewGroup)
return recurseGroup((ViewGroup) gp.getChildAt(i));
}
return false;
}
这里是方法 注意我这里为了测试 mIcon还是调用了系统图标,而在我的项目中是不能使用资源文件的,这里尽是测试。
调用
notification.setLatestEventInfo(this, "消息", "Hello Android", pi);
LinearLayout group = new LinearLayout(this);
ViewGroup event = (ViewGroup) notification.contentView.apply(this, group);
recurseGroup(event);
本文介绍了一种在不使用自定义布局的情况下更改Android Notification打开后显示图片的方法。通过递归查找Notification contentView中的ImageView,并替换其内容来实现。这种方法适用于无法直接引用资源文件的场景。
2490

被折叠的 条评论
为什么被折叠?



