简体   繁体   中英

Changing the default size of the notification (Android)

So I wanted to create a special notification , and that's why I have used the remote views and not the default kind of notifications. I know that with that kind I'm able to use expanded notification or something... The problem is that with this kind of notifications, I don't know if I'm able to change the size...

This is the creation of my notification:

RemoteViews contentView = new RemoteViews(packageName, R.layout.new_event_notification);
contentView.setTextViewText(R.id.eventName,eventToDisplay.getName());
contentView.setTextViewText(R.id.eventDayOfTheWeekTxt, dateTextToDisplay.getDateTextToDisplay(context,eventToDisplay.getEventDate()));            contentView.setTextViewText(R.id.eventTimeTxt,context.getString(R.string.at_time) + " " + eventToDisplay.getEventTime().toString());

contentView.setOnClickPendingIntent(R.id.attendButton, acceptPendingIntent);
contentView.setOnClickPendingIntent(R.id.declineButton, declinePendingIntent);
//Building the notification
NotificationCompat.Builder eventsNotification = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.notification_template_icon_bg)
    .setContentText("Incoming notifications")
    .setContent(contentView);

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//Sending out the notification
notificationManager.notify(notificationID, eventsNotification.build());

The notification's layout XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    android:layout_marginTop="-4dip">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Event Name"
        android:tag="event"
        android:id="@+id/eventName"
        android:layout_gravity="left"
        android:textColor="@android:color/black"
        android:textSize="21sp" 
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/eventDayOfTheWeekTxt"
        android:layout_alignEnd="@+id/eventDayOfTheWeekTxt" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Going"
        android:id="@+id/attendButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="-5dip"/>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Not Going"
        android:id="@+id/declineButton"
        android:layout_alignTop="@+id/attendButton"
        android:layout_toRightOf="@+id/attendButton"
        android:layout_toEndOf="@+id/attendButton"
        android:layout_marginLeft="-7dip"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="EventDayOfWeek"
        android:id="@+id/eventDayOfTheWeekTxt"
        android:textColor="#000000"
        android:singleLine="true"
        android:layout_below="@+id/eventName"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="EventTime"
        android:id="@+id/eventTimeTxt"
        android:textColor="#000000"
        android:layout_below="@+id/eventDayOfTheWeekTxt"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button2"
        android:layout_below="@+id/attendButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

After it is created, it looks like this(the top one) :

What I need you to look at, is the rest of the notifications in the picture, look how bigger they are than my notification.

Is there any way that I'll be able to change my notification size to a wanted size?

Or

at least to the size of the other notifications in the picture?

Just to be clear: I'm not talking about expanded size of the notification , but the default size.

If you are looking to provide space between text views (or buttons) then apply marginTop, marginBottom & other margins as per your requirement. You can play around with different values till you get desired result

You can't change the height your notification gets. The height of a notification is set by the system and you need to fit your contents into that space. If you need more space, you'll have to set a "big" content view (setCustomBigContentView) and try to get it to be expanded (use PRIORITY_HIGH, but this is no guarantee).

I don't know what kind of phone you have, but it seems to be rendering your layout larger than my phone does. The same layout code on my phone fits just fine.

通知适合我的手机

I tried to force the buttons larger and at 70dp the layout just forced the lower button to be smaller so that it would fit.

70dp 按钮高度

However, I have seen a LinearLayout with items that get lost because they extend past the bottom of the notification area, which seems more like what you are seeing on your phone.

Maybe you can explicitly set your font size/button height to ensure everything fits?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM