在Android中自定义通知区域

在Android中自定义通知区域,android,android-notifications,android-notification-bar,android-remoteview,Android,Android Notifications,Android Notification Bar,Android Remoteview,我正在使用remoteview创建自定义通知。有没有办法自定义通知区域的高度。。。???我不想使用BigView因为我需要将它用于小于11的API 桑普肖是: 这是我的密码: public class Main extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我正在使用
remoteview
创建自定义通知。有没有办法自定义通知区域的高度。。。???我不想使用
BigView
因为我需要将它用于小于11的API

桑普肖是:

这是我的密码:

public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notificationmain);

        Button bnotify = (Button) findViewById(R.id.notification);
        Button bcustomnotify = (Button) findViewById(R.id.customnotification);

        // Click For Default Notification       
        bnotify.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Notification();
            }
        });

        // Click For Custom Notification
        bcustomnotify.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                CustomNotification();
            }
        });

    }

    // Default Notification
    public void Notification() {
        String strtitle = getString(R.string.notificationtitle);
        String strtext = getString(R.string.notificationtext);
        Intent intent = new Intent(this, NotificationView.class);
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logosmall)
                .setTicker(getString(R.string.notificationticker))
                .setContentTitle(getString(R.string.notificationtitle))
                .setContentText(getString(R.string.notificationtext))
                .addAction(R.drawable.ic_launcher, "Action Button", pIntent)
                .setContentIntent(pIntent)
                .setAutoCancel(true);

        // Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationmanager.notify(0, builder.build());
    }


    // Custom Notification

    public void CustomNotification() {
        // Using RemoteViews to bind custom layouts into Notification
        RemoteViews remoteViews = new RemoteViews(getPackageName(),
                R.layout.customnotification);

        String strtitle = getString(R.string.customnotificationtitle);
        String strtext = getString(R.string.customnotificationtext);

        Intent intent = new Intent(this, NotificationView.class);
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logosmall)
                .setTicker(getString(R.string.customnotificationticker))
                .setAutoCancel(true)
                .setContentIntent(pIntent)
                .setContent(remoteViews);

        remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
        remoteViews.setImageViewResource(R.id.imagenotiright,R.drawable.androidhappy);

        remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
        remoteViews.setTextViewText(R.id.text,getString(R.string.customnotificationtext));

        Calendar cal = Calendar.getInstance();
        cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");

        remoteViews.setTextViewText(R.id.time,sdf.format(cal.getTime()) );
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationmanager.notify(1, builder.build());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Android support library v4似乎有一个支持此类功能的通知生成器:

NotificationCompat.InboxStyle似乎是4.1版本之前的设备的BigView等价物

这使您可以创建一个更大的通知,最多包含五行文本,这可能适合您的需要

 Notification noti = new Notification.Builder()
 .setContentTitle("5 New mails from " + sender.toString())
 .setContentText(subject)
 .setSmallIcon(R.drawable.new_mail)
 .setLargeIcon(aBitmap)
 .setStyle(new Notification.InboxStyle()
     .addLine(str1)
     .addLine(str2)
     .setSummaryText("+3 more"))
 .setContentTitle("")
 .build();

这里有一个简短的教程:

通知显示区由android系统控制
请参阅link

您可以通过修改布局
customnotification
来控制高度,并使用较小尺寸的图像。我想增加高度…自定义通知布局的可用高度取决于通知视图。普通视图布局限制为
64 dp
,扩展视图布局限制为
256 dp
@JoelFernandes,我在
customnotification
布局中将图像帧大小增加到80 dp,但是高度还是一样的…我在我的默认通知代码中加入了这些行…但它不起作用…经过多次尝试,我终于在您的帮助下找到了它…真棒,很高兴听到它!