簡體   English   中英

Android 默認通知邊距

[英]Android default notification margin

再會。 我為通知創建了完全自定義的視圖。 但是在不同的設備上左(開始)邊距不同。

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

  • 在我的具有 16dp marginStart 的 OnePlus 6 (1080 x 2280) 上,所有通知(包括系統通知和 Gmail 等其他通知)都具有相同的起始邊距並且看起來不錯。

  • 但在三星 Tab A (9.7", 1024x768) 上,16dp 看起來比其他通知小。

我也嘗試使用 sdk 屬性android:dimen/notification_content_margin_start但這個屬性是私有的,我獲得了編譯異常AAPT: error: resource android:dimen/notification_content_margin_start 是私有的。

有沒有辦法獲得設備指定的通知內容填充? 謝謝

無需為布局設置任何邊距和填充。 使用 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

我知道這是一個老問題,但解決方案很簡單。 我們將在父視圖中使用填充而不是使用邊距,然后我們將獲取數量並在代碼中設置填充。 我們將從默認值開始,以防代碼失敗。

<?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 函數。

享受吧,Lionscribe

當您的應用在 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()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM