有两个操作按钮自定义通知 [英] Custom notification with two action button
问题描述
我使用以下code,产生了两个操作按钮和一些细节的通知。请帮我,为什么我不能够看到通知操作按钮?
公共静态无效sendNotification时(上下文mContext,诠释方式,标题字符串,字符串消息){
尝试
{
RemoteViews remoteViews =新的RemoteViews(mContext.getPackageName(),R.layout.custom_notification);
//设置通知标题
字符串strtitle =Instevent;
//设置文本的通知
字符串strText的=事件创造; //打开NotificationView类的通知点击
意向意图=新意图(mContext,MainActivity.class);
//将数据发送NotificationView类
intent.putExtra(称号,strtitle);
intent.putExtra(文字,strText的);
//打开NotificationView.java活动
的PendingIntent pIntent = PendingIntent.getActivity(mContext,0,意向,PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder建设者=新NotificationCompat.Builder(mContext)
//设置图标
.setSmallIcon(R.drawable.ic_address_book)
//设置北京时间留言
.setTicker(北京时间信息)
//关闭通知
.setAutoCancel(真)
//设置的PendingIntent到通知
.setContentIntent(pIntent)
//设置RemoteViews到通知
.setContent(remoteViews); //找到并设置图像到customnotificationtext.xml
// ImageViews
remoteViews.setImageViewResource(R.id.nfImage,R.drawable.ic_action_accept);
remoteViews.setTextViewText(R.id.nfEventName,自定义通知);
remoteViews.setTextViewText(R.id.nfEventTime,这是一个自定义布局);
remoteViews.setTextViewText(R.id.nfEventAddress,这是一个自定义布局); //创建通知管理器
NotificationManager notificationmanager =(NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//建立与通知管理通知
notificationmanager.notify(0,builder.build());
}
赶上(例外五)
{
e.printStackTrace();
Log.print(通知====>>中,e.getMessage());
}
}
custom_notification.xml
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res/com.instevent
机器人:layout_width =match_parent
机器人:layout_height =match_parent> < RelativeLayout的
机器人:ID =@ + ID / rlMainNotification
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/ bg_rounded_white
机器人:填充=10dp> < ImageView的
机器人:ID =@ + ID / nfImage
机器人:layout_width =50dp
机器人:layout_height =50dp
机器人:scaleType =fitXY
机器人:SRC =@绘制/ ic_group/> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =5DP
机器人:layout_marginStart =5DP
机器人:layout_toEndOf =@ + ID / nfImage
机器人:layout_toRightOf =@ + ID / nfImage
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / nfEventName
风格=@风格/ BlackTextN
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:paddingLeft =5DP
机器人:paddingRight =5DP
机器人:单线=真
机器人:文本=重要会议
机器人:TEXTSIZE =16SP/> <的TextView
机器人:ID =@ + ID / nfEventTime
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =5DP
机器人:paddingLeft =7DP
机器人:paddingRight =5DP
机器人:单线=真
机器人:文字=4:15 - 5:15 PM
机器人:文字颜色=#8b8b8b
机器人:TEXTSTYLE =正常/> <的TextView
机器人:ID =@ + ID / nfEventAddress
风格=@风格/ GrayColorN
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =2DP
机器人:paddingLeft =7DP
机器人:paddingRight =5DP
机器人:单线=真
机器人:文本=大会议室
机器人:文字颜色=#8b8b8b
机器人:TEXTSTYLE =正常/> <查看
机器人:layout_width =FILL_PARENT
机器人:layout_height =1DP
机器人:layout_marginLeft =6DP
机器人:layout_marginStart =6DP
机器人:layout_marginTop =5DP
机器人:背景=#dcdcdc/> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
机器人:weightSum =2> <的TextView
机器人:ID =@ + ID / nfDecline
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:drawableLeft =@绘制/ ic_action_cancel
机器人:drawablePadding =10dp
机器人:drawableStart =@绘制/ ic_action_cancel
机器人:重力=center_vertical
机器人:文字=@字符串/下降
机器人:文字颜色=#676767/> <的TextView
机器人:ID =@ + ID / nfAccept
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:drawableLeft =@绘制/ ic_action_accept
机器人:drawablePadding =10dp
机器人:drawableStart =@绘制/ ic_action_accept
机器人:重力=center_vertical
机器人:文字=@字符串/接受
机器人:文字颜色=#676767/>
< / LinearLayout中>
< / LinearLayout中>
< / RelativeLayout的>< / RelativeLayout的>
我在我的应用程序也有类似的错误。问题是,RemoteViews不支持每一种布局或视图。
在你的情况,我认为这个问题是<查看/>
在custom_notification.xml一部分。
尝试删除或支持的布局元素替换它。你可以在这里找到他们:的http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
I am using following code to generate notifications with two action button and some details. Please help me, why i am not able to see action button in notification?
public static void sendNotification(Context mContext, int mode, String title, String message) { try { RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification);
// Set Notification Title
String strtitle = "Instevent";
// Set Notification Text
String strtext = "Event has created";
// Open NotificationView Class on Notification Click
Intent intent = new Intent(mContext, MainActivity.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
// Set Icon
.setSmallIcon(R.drawable.ic_address_book)
// Set Ticker Message
.setTicker("Ticker message")
// Dismiss Notification
.setAutoCancel(true)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Set RemoteViews into Notification
.setContent(remoteViews);
// Locate and set the Image into customnotificationtext.xml
// ImageViews
remoteViews.setImageViewResource(R.id.nfImage, R.drawable.ic_action_accept);
remoteViews.setTextViewText(R.id.nfEventName, "Custom notification");
remoteViews.setTextViewText(R.id.nfEventTime, "This is a custom layout");
remoteViews.setTextViewText(R.id.nfEventAddress, "This is a custom layout");
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
catch (Exception e)
{
e.printStackTrace();
Log.print("Notification====>>", e.getMessage());
}
}
custom_notification.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.instevent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/rlMainNotification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_rounded_white"
android:padding="10dp" >
<ImageView
android:id="@+id/nfImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:src="@drawable/ic_group" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_toEndOf="@+id/nfImage"
android:layout_toRightOf="@+id/nfImage"
android:orientation="vertical" >
<TextView
android:id="@+id/nfEventName"
style="@style/BlackTextN"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true"
android:text="The Big Meeting"
android:textSize="16sp" />
<TextView
android:id="@+id/nfEventTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="7dp"
android:paddingRight="5dp"
android:singleLine="true"
android:text="4:15 - 5:15 PM"
android:textColor="#8b8b8b"
android:textStyle="normal" />
<TextView
android:id="@+id/nfEventAddress"
style="@style/GrayColorN"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:paddingLeft="7dp"
android:paddingRight="5dp"
android:singleLine="true"
android:text="The Big Conference Room"
android:textColor="#8b8b8b"
android:textStyle="normal" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="5dp"
android:background="#dcdcdc" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="@+id/nfDecline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/ic_action_cancel"
android:drawablePadding="10dp"
android:drawableStart="@drawable/ic_action_cancel"
android:gravity="center_vertical"
android:text="@string/decline"
android:textColor="#676767" />
<TextView
android:id="@+id/nfAccept"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/ic_action_accept"
android:drawablePadding="10dp"
android:drawableStart="@drawable/ic_action_accept"
android:gravity="center_vertical"
android:text="@string/accept"
android:textColor="#676767" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I had a similar error in my App. The problem was that RemoteViews does not support every kind of layout or view.
In your case, I think the problem is the <View/>
part in your custom_notification.xml.
Try removing or replacing it with a supported layout element. You can find them here: http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
这篇关于有两个操作按钮自定义通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!