如何解决 android 自定义布局通知中不显示通知文本的问题?

how to fix issue not showing text of notification in android custom layout notification?

我想为通知创建自定义布局。有 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);

这是输出:

在通知折叠中:

在通知中展开:

终于解决了我的问题,希望对遇到同样问题的人有用。 这个问题是因为在主 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>