首页 > 解决方案 > 如何解决在android自定义布局通知中不显示通知文本的问题?

问题描述

我想为通知创建自定义布局。通知的展开和折叠有 2 种布局。我的问题是,当我折叠通知时,没有在第二个文本视图中显示通知文本。请告诉我如何解决它。谢谢。

这是我的布局:notification_small.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="match_parent"
    android:layoutDirection="ltr"
    android:padding="5dp"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:orientation="vertical">
            <TextView
                android:id="@+id/notification_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:lines="1"
                android:ellipsize="end"
                style="@style/TextAppearance.Compat.Notification.Title" />
            <TextView
                android:id="@+id/notification_body"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:lines="1"
                android:ellipsize="end"
                style="@style/TextAppearance.Compat.Notification.Line2" />
        </LinearLayout>
        <ImageView
            android:id="@+id/notification_image"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:src="@drawable/default_image"/>
    </LinearLayout>

</LinearLayout>

这是我的代码的一部分:

 NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "dapp_channel_id" + Id)
                        .setGroup(groupKey)
                        .setContentTitle(Title)
                        .setContentText(Body)
                        .setSmallIcon(R.drawable.ic_stat_name)
                        .setLargeIcon(bm)
                        .setSound(notificationSoundURI)
                        .setAutoCancel(true)
                        .setGroupSummary(false)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(Body))
                        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                        .setCustomContentView(notificationLayout)
                        .setCustomBigContentView(notificationLayoutExpanded)
                        .setContentIntent(singleNotificationPendingIntent);

这是输出:

在通知折叠中: 在通知折叠中

在通知中展开: 在通知中 展开

标签: androidnotifications

解决方案


最后我解决了我的问题,我希望它对有同样问题的人有用。这个问题是因为在主 LinearLayout 中设置了填充,我改变了我的布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/notification_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/notification_image"
        android:gravity="right"
        android:lines="1"
        android:ellipsize="end"
        style="@style/TextAppearance.Compat.Notification.Title" />
    <TextView
        android:id="@+id/notification_body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/notification_image"
        android:layout_below="@id/notification_title"
        android:gravity="right"
        android:lines="1"
        android:ellipsize="end"
        android:textSize="13sp"
        style="@style/TextAppearance.Compat.Notification.Title" />
    <ImageView
        android:id="@+id/notification_image"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:maxHeight="40dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:adjustViewBounds="true"
        android:src="@drawable/default_image"/>
</RelativeLayout>

推荐阅读