Android自定义通知(底部带图像的正常通知)

时间:2014-08-04 10:44:30

标签: android notifications android-custom-view android-notifications

可以在底部创建包含图像的正常通知,例如:

像这样的STH:

enter image description here

我现在就是这样创建的:

我的布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <ImageView
            android:id="@+id/icon"
            android:layout_width="67dp"
            android:layout_height="67dp"
            android:src="@drawable/notification"
            android:contentDescription="Test"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"

            android:background="#333333" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/icon"
            android:paddingBottom="10dp"
            android:textColor="#FFFFFF"
            android:text="Sample big text"
            android:textSize="16dp" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title"
            android:layout_toRightOf="@+id/icon"
            android:paddingLeft="10dp"
            android:text="small sample text"
            android:textSize="14dp" />

    </RelativeLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:src="@drawable/meow"
        android:scaleType="fitXY"/>



</LinearLayout>

和我的代码:

 public void showNotificationWithPicture(String topic, String description,int x, int y){
        Intent resultIntent = new Intent(context, MyActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(MyActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0, PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews expandedView = new RemoteViews(context.getPackageName(),
                R.layout.notification_background_layout);
        expandedView.setTextViewText(R.id.title, topic);
        expandedView.setTextViewText(R.id.desc, description);
        Bitmap result = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
                        context.getResources(), R.drawable.offer),
                x, y, true); //set image size
        expandedView.setImageViewBitmap(R.id.imageView, result);
        Notification notification = new Notification.Builder(context)
                .setSmallIcon(R.drawable.flavor_ball)
                .setLargeIcon(((BitmapDrawable)context.getResources().getDrawable(R.drawable.notification)).getBitmap())
                .setAutoCancel(false)
                .setContentIntent(resultPendingIntent)
                .setContentTitle(topic)
                .setContentText(description)
                .build();
        //notification.contentView.addView(1, expandedView);
        notification.bigContentView = expandedView;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        ((NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE))
                .notify(1, notification);

// Create the style object with BigPictureStyle subclass.

    }

但这不是我想要的,因为现在我必须创建一个布局,设置相同的字体和颜色,就像在正常的android通知中一样,这并不容易。也许有一个更简单的解决方案?我将非常感谢任何提示或任何我可以找到正常通知布局的来源。

1 个答案:

答案 0 :(得分:0)

好吧,我刚刚找到了解决方案: 通知方式:

http://developer.android.com/guide/topics/ui/notifiers/notifications.html#ApplyStyle