【问题标题】:Android: Extra spacing in collapsed notificationAndroid:折叠通知中的额外间距
【发布时间】:2019-12-27 10:01:37
【问题描述】:

我正在使用折叠和展开视图制作持久通知。但是我折叠的视图在底部占用了额外的空间。这不是我想出来的布局问题。

最初,我为展开视图和折叠视图创建了单独的布局,但我意识到通知标题需要默认设计,因此我删除了折叠视图布局。

mBuilder.setSmallIcon(R.drawable.ic_fix_icon)
                .setAutoCancel(false)
                .setOngoing(true)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setCustomBigContentView(remoteExpandedViews)
                .setPriority(Notification.PRIORITY_MAX)
                .setChannelId(NOTIFICATION_CHANNEL_ID);

请在下面找到我用于扩展通知视图的布局文件。我不为折叠视图使用任何布局。我想要扩展通知中的标题、描述和两个按钮。从两个按钮点击,我直接打开活动。

删除ExpandedViews.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="0dp"
android:padding="0dp"
android:background="@android:color/transparent"
android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/custome_expanded_notification_relativelayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/custome_expanded_notification_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/TextAppearance.Compat.Notification.Info"
            android:text="Today's task"
            android:fontFamily="@font/robotomedium"
            android:ellipsize="end"
            android:textColor="@color/gray"
            android:textSize="12sp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:layout_alignParentLeft="true"/>

        <ImageView
            android:id="@+id/custome_expanded_notification_next_icon"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/ic_next_icon"
            android:tint="@color/black"
            android:visibility="gone"
            android:padding="10dp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"/>


        <TextView
            android:id="@+id/custome_expanded_notification_task"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="No task for today !"
            android:ellipsize="end"
            android:textSize="14sp"
            android:layout_marginTop="2dp"
            android:textColor="@color/black"
            android:layout_marginLeft="5dp"
            android:layout_toLeftOf="@+id/custome_expanded_notification_next_icon"
            style="@style/TextAppearance.Compat.Notification.Title"
            android:fontFamily="@font/robotomedium"
            android:layout_below="@+id/custome_expanded_notification_title"/>


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/custome_expanded_notification_relativelayout1"
        android:padding="0dp"
        android:layout_marginLeft="5dp">

        <TextView
            android:id="@+id/custome_expanded_notification_add_task"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Task"
            android:padding="0dp"
            android:textStyle="bold"
            android:textAllCaps="true"
            android:textColor="@color/textColorTitle"
            android:fontFamily="@font/robotobold"
            android:textSize="11dp"
            android:layout_marginTop="10dp"
            android:gravity="center"/>

        <TextView
            android:id="@+id/custome_expanded_notification_setting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Setting"
            android:textStyle="bold"
            android:padding="0dp"
            android:textAllCaps="true"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/custome_expanded_notification_add_task"
            android:textColor="@color/textColorTitle"
            android:fontFamily="@font/robotobold"
            android:textSize="11dp"
            android:layout_marginTop="10dp"
            android:gravity="center"/>

    </RelativeLayout>

</RelativeLayout>

我想要像 any.do 一样的折叠视图。

【问题讨论】:

  • 使用 uiautomatorviewer 检查 UI 层次结构并查看它为您的通知膨胀的附加视图。
  • 您好,能否请您同时粘贴您的ic_fix_iconremoteExpandedViews
  • @ravi 请检查。

标签: android android-layout push-notification notifications


【解决方案1】:

用这个,肯定有用。

NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setImportance(NotificationManager.IMPORTANCE_UNSPECIFIED);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

【讨论】:

  • 我们得到一个错误 java.lang.IllegalArgumentException: Invalid important level
【解决方案2】:

我看到你的通知小图标是 77*77 像素。根据通知小图标的android指南,你应该不超过24 * 24像素。

还要考虑减少自定义布局的嵌套。 :)

你能改变大小并使用它吗?如果这对您不起作用,请告诉我。

编辑:

使用 Android Studio 中的图像资源:

【讨论】:

  • 我只使用 24X24 图标,它仍然显示额外的间距。
  • 您是否使用了 Android Studio 中的添加图片资源并将图标类型设置为通知?
  • 是的。我做的完全一样。
  • 最后一件事,你没有在你的Builder 上调用setCustomContentView(RemoteViews remoteViews)。这是故意的吗?通常对于自定义大内容视图,也会使用自定义内容视图。您不想要任何最小化的通知视图。如果您不想要任何最小化视图,请尝试在您的构建器上添加 setCustomContentView(null),或者添加自定义布局并将其传递给相同的视图。
猜你喜欢
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2012-07-28
  • 2019-11-24
  • 1970-01-01
相关资源
最近更新 更多