安卓默认通知边距

4

你好。我创建了一个完全自定义的通知视图。但是在不同设备上,左侧(开始)边距不同。

<?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"
    >

在 values/dimens.xml 中,notificationContentInsert 的值为16dp。

例如:

  • 在我的 OnePlus 6(1080 x 2280)上,所有通知(包括系统通知和其他应用程序如 Gmail)的起始边距都为16dp,看起来很好。

  • 但是,在三星 Tab A(9.7 英寸,1024x768)上,16dp 看起来比其他通知更小。

我也尝试使用 sdk 属性 android:dimen/notification_content_margin_start,但该属性是私有的,我得到了编译异常AAPT: error: resource android:dimen/notification_content_margin_start is private.

是否有办法获取设备指定的通知内容填充?谢谢。


你解决了这个问题吗?我也需要做同样的事情。 - Furkan Yurdakul
很抱歉,@FurkanYurdakul不行。 - Yuriy Aizenberg
3个回答

4

在 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() 方法,请告诉我。


2
Resource.getIdentifier()和remoteViews.setViewPadding()在除了小米设备以外的其他设备上都能正常工作。我不明白为什么? - Dinh Quang Tuan

2

我知道这是一个老问题,但解决方案很简单。与其使用边距,我们会在父视图中使用内边距,然后在代码中获取该值并设置内边距。为了防止代码出现故障,我们将从默认值开始。

<?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函数,我们必须使用padding。祝愉快,Lionscribe。

2
似乎在虚拟设备上工作正常,但在真正的小米手机上,边距太大了。 - Dinh Quang Tuan

1
不需要为布局设置任何边距和填充。使用以下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版本来说,这似乎是一个不错的选择。 - rahulrvp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接