【问题标题】:Android default notification marginAndroid 默认通知边距
【发布时间】:2019-03-01 15:32:12
【问题描述】:

美好的一天。 我为通知创建了完全自定义的视图。但在不同的设备上左(开始)边距不同。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp"
    android:orientation="vertical"
    android:layout_marginEnd="@dimen/notificationContentInsert"
    android:layout_marginStart="@dimen/notificationContentInsert"
    >

其中的 notificationContentInsert 在 values/dimens.xml 中是 16dp

F.e.

  • 在我的 OnePlus 6 (1080 x 2280) 上,边距为 16dp 开始所有通知 (包括系统通知和其他类似 gmail)有相同的开始 边距,看起来不错。

  • 但在三星 Tab A(9.7",1024x768)上,16dp 看起来比另一个小 通知。

我也尝试使用 sdk 属性 android:dimen/notification_content_margin_start 但是这个属性是私有的,我得到编译异常 AAPT:错误:资源 android:dimen/notification_content_margin_start 是私有的。

有没有办法获得设备指定的通知内容填充?谢谢

【问题讨论】:

  • 您想出解决这个问题的办法了吗?我也需要做同样的事情。
  • @FurkanYurdakul 很遗憾没有

标签: android notifications android-screen-support


【解决方案1】:

我知道这是一个老问题,但解决方案很简单。我们将在父视图中使用填充而不是使用边距,然后我们将获取数量并在代码中设置填充。我们将从默认值开始,以防代码失败。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/notify_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingTop="0dp"
    android:paddingRight="16dp"
    android:paddingBottom="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

然后在代码中我们将使用Resources.getIdentifier() 来获取值。我们用 try/catch 来包围它,所以在最坏的情况下,它使用默认值。代码如下:

RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.my_notification_content_layout);
try {
    // We are assuming start and end are same. If we want to be nitty, we will get them separately, and then check which to use for left and which for righht.
    int identifier = context.getResources().getIdentifier("notification_content_margin_start", "dimen", "android");
    if (identifier != 0) {
        int padding = context.getResources().getDimensionPixelSize(identifier);
        contentView.setViewPadding(R.id.notify_layout, padding, 0, padding, 0);
        Log.d("setViewPadding", "Setting padding to: " + padding);
    } else {
        Log.d("setViewPadding", "Failed to find padding");
    }
} catch (Exception e) {
    Log.d("setViewPadding", "Failed to set padding");
}

此代码经过测试,似乎运行良好。我们必须使用填充,因为 RemoteViews 没有 setMargins 函数。

尽情享受吧,狮王

【讨论】:

  • 在虚拟设备上似乎可以工作,但在真正的小米手机上,余量太大
【解决方案2】:

当您的应用在 Android 12 中运行时,添加静态边距或填充可能会引发问题。

如果targetSdkVersion 小于 31,您仍然可以使用完整的通知区域来自定义通知。

如果您在布局文件中添加填充并将 targetSdkVersion 更改为 31,则通知在 Android 12 设备中看起来会很奇怪。如果您正在构建一个具有通知功能的 sdk,这将变得很痛苦。我有类似的情况,并决定在以编程方式检查 targetSdkVersion 后添加填充。

ApplicationInfo info = context.getApplicationInfo();
if (info.targetSdkVersion < Build.VERSION_CODES.S) {
    int dp16 = Utils.dpToPx(16, context);
    remoteViews.setViewPadding(R.id.notification_container, dp16, dp16, dp16, dp16);
}

R.id.notification_container 是我的自定义通知布局的根视图。

值 16 对我来说看起来不错。但是,请让我知道是否有更好的方法从操作系统获取值而无需调用Resource.getIdentifier() 方法。

【讨论】:

  • Resource.getIdentifier() 和 remoteViews.setViewPadding() 在小米以外的设备上运行良好。我不明白为什么???
【解决方案3】:

无需为布局设置任何边距和内边距。使用如下 setStyle 方法将 NotificationCompat.DecoratedCustomViewStyle 应用于您的通知。此 API 允许您为通常由标题和文本内容占用的内容区域提供自定义布局,同时仍对通知图标、时间戳、子文本和操作按钮使用系统装饰。

android.support.v4.app.NotificationCompat.Builder nBuilder = new 
android.support.v4.app.NotificationCompat.Builder(parent);
nBuilder.setContentTitle("Your title")
   .setSmallIcon(R.drawable.ic_launcher)
   . // add your other settings
   .
   .setStyle(new NotificationCompat.DecoratedCustomViewStyle()); //this makes your notification similar to other system notifications

【讨论】:

  • 这种方式消除了在旧设备中使用完整空间来扩展通知的奢侈。但对于最近的 android 版本,这似乎是一个不错的选择。
猜你喜欢
  • 2021-12-09
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多