DecoratedCustomViewStyle 要高吗?

DecoratedCustomViewStyle to high height?

我已经在 https://developer.android.com/training/notify-user/custom-notification

我用过

.setStyle(new NotificationCompat.DecoratedCustomViewStyle())

设置通知的原始样式+header。

结果如下:

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/notification_small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/notification_image"
        android:layout_toStartOf="@+id/notification_image">

        <LinearLayout
            android:id="@+id/linearNotificationWrap"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/notification_cat_icon"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center" />

            <TextView
                android:id="@+id/notification_title"
                style="@style/TextAppearance.Compat.Notification.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:ellipsize="end"
                android:maxLines="1"
                android:text="title" />
        </LinearLayout>

        <TextView
            android:id="@+id/notification_body"
            style="@style/TextAppearance.Compat.Notification.Info"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/linearNotificationWrap"
            android:ellipsize="marquee"
            android:maxLines="2"
            android:text="body" />

    </RelativeLayout>

    <ImageView
        android:id="@+id/notification_image"
        android:layout_width="@android:dimen/app_icon_size"
        android:layout_height="@android:dimen/app_icon_size"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_gravity="center" />
</RelativeLayout>

无论我在 android:layout_height 中使用什么,视图都不会改变。如果我删除 DecoratedCustomViewStyle,无用的高度就消失了,但我想设置标准的通知样式。

显示通知的代码:

private void sendNotification(String title, String message, int id, String uri, String channelId) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("intentUrl",uri);
        intent.setAction(Long.toString(System.currentTimeMillis()));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
        notificationLayout.setTextViewText(R.id.notification_title, title);
        notificationLayout.setTextViewText(R.id.notification_body, message);
        notificationLayout.setImageViewResource(R.id.notification_cat_icon,R.drawable.icon_push);
        notificationLayout.setImageViewResource(R.id.notification_image,R.drawable.icon_push);


        RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
        notificationLayoutExpanded.setTextViewText(R.id.notification_title, title);
        notificationLayoutExpanded.setTextViewText(R.id.notification_body, message);
        notificationLayoutExpanded.setImageViewResource(R.id.notification_cat_icon,R.drawable.icon_push);
        notificationLayoutExpanded.setImageViewResource(R.id.notification_image,R.drawable.icon_push);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.icon_push)
                .setAutoCancel(true)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(notificationLayout)
                .setCustomBigContentView(notificationLayoutExpanded);



        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(id, notificationBuilder.build());
    }

有人有类似的效果或知道发生了什么事吗? 谢谢

问题解决如下: 确保没有子元素的高度为 match_parent。 问题示例中的固定部分是:

<TextView
            android:id="@+id/notification_body"
            style="@style/TextAppearance.Compat.Notification.Info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/linearNotificationWrap"
            android:ellipsize="marquee"
            android:maxLines="2"
            android:text="body" />

带有 android:id="@+id/notification_body" 的文本视图:将 android:layout_height="match_parent" 更改为 android:layout_height="wrap_content"