Android 修改通知栏一条信息的显示高度

   感谢这位csdn的前辈的提示: 

            该功能需要4.1以后的版本才支持,用Notification.build类来创建一个新notificaiton,如果仅仅显示网易那样的样式,可以定义一个Notification.BigTextView的style,然后调用build类的setSytle方法,就能够实现了,但是如果需要设置更复杂的样式,就不能够使用setSytle,需要用bigContentView,bigContentView = remoteView这样就能够显示更大的通知栏了,你需要导入android-support-v4.jar包,才能够使用Notification.Builder,参考了下面的文章:http://stackoverflow.com/questions/21237495/create-custom-big-notifications,还是stackoverflow网站比较牛,能够真的解决问题


    这个链接网页如下:http://stackoverflow.com/questions/21237495/create-custom-big-notifications



I wanted to create a Notification including some controls. Since text and controls are small with default notification size (64dp), i wanted have it larger than default size.
It is possible to create larger notifications, and I think it is possible to have a custom layout, too, but I don't know how.

To be more specific, the following screenshot shows the notification from spotify (image take fromhere): Spotify notification

As you can see, the size is bigger than default. Further, it has some kind of ImageButtons without text - if you use Notification.Builder.addAction(), you may provide an icon but also need to provide a CharSequence as a description - if you leave the description empty, there will still be space reserved for the text and if you pass null, it will crash.

Can anybody tell me how to create a big notification with a custom layout?

Thanks

share improve this question
 
 
possible duplicate of Create custom notification, android –  phemt.latd  Jan 20 '14 at 15:19
 
@phemt.latd The answers given in that question deal with custom notification default size or default notification big size. I could not manage to have a custom notification big size with the given answers. –  MalaKa  Jan 20 '14 at 15:46 

2 Answers

up vote 20 down vote accepted

So after excessive google usage, I found this tutorial explaining how to use custom big layouts. The trick is not to use setStyle() but manually set the bigContentView field of the Notification after building it. Seems a bit hacky, but this is what I finally came up with:

notification_layout_big.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp" <!-- This is where I manually define the height -->
    android:orientation="horizontal" >

    <!-- some more elements.. --> 
</LinearLayout>

Building Notification in code:

Notification foregroundNote;

RemoteViews bigView = new RemoteViews(getApplicationContext().getPackageName(),
    R.layout.notification_layout_big);

// bigView.setOnClickPendingIntent() etc..

Notification.Builder mNotifyBuilder = new Notification.Builder(this);
foregroundNote = mNotifyBuilder.setContentTitle("some string")
        .setContentText("Slide down on note to expand")
        .setSmallIcon(R.drawable.ic_stat_notify_white)
        .setLargeIcon(bigIcon)
        .build();

foregroundNote.bigContentView = bigView;

// now show notification..
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyManager.notify(1, foregroundNote);

Edit
As noted by chx101, this only works for API >= 16. I did not mention it in this answer, yet it was mentioned in the given linked tutorial above:

Expanded notifications were first introduced in Android 4.1 JellyBean [API 16].

share improve this answer
 
 
thank you.but if another notification come ,this notification's view back to the default view ,can you say why?–  MHP  Jan 2 '15 at 6:40 
 
This is because Android OS wants to ensure all Notifications stay visible. You can increase the priority of the Notification using the setPriority-method. But you can never be sure, that your Notification is enlarged. Additionally, the user can always enlarge the Notification manually by sliding down on the Notification. –  MalaKa  Jan 5 '15 at 22:56 
 
Doens't work for API level 8. Field requires API level 16 (current min is 8): android.app.Notification#bigContentView –  chx101  Oct 27 '15 at 0:17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值