自定义通知带有两个操作按钮

时间:2015-04-20 06:13:10

标签: android android-notifications

我正在使用以下代码生成带有两个操作按钮和一些细节的通知。请帮助我,为什么我无法在通知中看到操作按钮?

public static void sendNotification(Context mContext,int mode,String title,String message){     尝试     {         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>

1 个答案:

答案 0 :(得分:1)

我的应用程序中出现了类似的错误。问题是RemoteViews不支持所有类型的布局或视图。

在您的情况下,我认为问题是您的custom_notification.xml中的<View/>部分。 尝试使用支持的布局元素删除或替换它。您可以在此处找到它们:http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout