android - 如何解决在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);
这是输出:
解决方案
最后我解决了我的问题,我希望它对有同样问题的人有用。这个问题是因为在主 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>
推荐阅读
- sql - 如何使用 SSIS 在 SQL Server 中创建分隔符
- continuous-integration - 有没有一种方法可以在任何与语言无关的 CI 上运行测试?
- triggers - 使用插入触发器之前从 opp 获取输入值的 Apex 代码
- c# - 索引超出了数组的范围,不可重现
- java - 如何分析 spring-boot 应用程序内存消耗?
- java - 如何通过异步 servlet 向外部服务器发送常规查询?
- javascript - 为什么“this”关键字在 EventListener 的匿名函数中不起作用?
- python - 模拟耦合常微分方程
- c# - 防止 .Net Core 5 控制台应用程序在 Linux 作为服务上运行时结束的推荐方法
- javascript - JavaScript 子数组迭代