android的Notification折叠

android 的Notification 看起来简单,但是api老是改。用的时候确实很麻烦。这里写一个折叠的Demo。
布局文件:activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="elfstton.cheerchip.com.notifitydemo.MainActivity">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Collapsed"
        android:onClick="collapsedNotify" />
</RelativeLayout>

点击事件,注意api的判断。

  public void collapsedNotify(View view) {
        Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
        Notification.Builder builder=new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));

        //通过RemoteViews创建自定义视图
        RemoteViews  remoteViews=new RemoteViews(getPackageName(),R.layout.mynotification);
        remoteViews.setTextViewText(R.id.textView,"可折叠式");

        Notification notification= null;
        //大于16
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            notification = builder.build();
        }else{
            //小于16
            notification=builder.getNotification();
        }
        notification.contentView=remoteViews;
        //通过RemoteViews创建自定义的Notification视图
        RemoteViews expandedView=new RemoteViews(getPackageName(),R.layout.notification_expanded);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            notification.bigContentView=expandedView;
        }

        NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(0,notification);

    }

源码下载: https://github.com/AndriodWorld/androidnotification

AndroidNotification类中提供了`BigContentView`属性,可以实现通知不折叠。默认情况下,当通知内容过长时,系统会将通知折叠显示,只显示一部分内容。但是使用`BigContentView`属性后,可以实现通知的完全展开,显示所有的内容。 要使用`BigContentView`,首先需要创建一个RemoteViews对象,用于自定义通知的布局。然后,将该布局设置给`NotificationCompat.Builder`对象的`setCustomBigContentView()`方法。这样,系统在展示通知时,就会使用自定义布局,实现通知的不折叠。 以下是一个简单的示例代码: ```java // 创建自定义的BigContentView布局 RemoteViews bigContentView = new RemoteViews(getPackageName(), R.layout.notification_big); // 设置自定义布局到NotificationCompat.Builder对象 NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("通知标题") .setContentText("通知内容") .setCustomBigContentView(bigContentView); // 发送通知 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notificationId, builder.build()); ``` 在上面的示例中,`notification_big`是一个自定义的布局文件,表示大尺寸通知的内容。你可以在该布局中设计显示所有的通知内容,包括标题、文字、图标等。然后,将该布局设置给`NotificationCompat.Builder`对象,即可实现通知的不折叠显示。 需要注意的是,某些设备可能无法显示大尺寸通知,而是将其折叠显示。这是由于设备厂商对通知的样式和布局进行了定制。因此,在使用`BigContentView`时,需要考虑设备和系统的兼容性,并进行适当的测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值