ومع ذلك، إذا كانت نماذج النظام لا تلبي احتياجاتك، يمكنك استخدام نماذجك الخاصة
تخطيط الإشعار.
إنشاء تخطيط مخصص لمنطقة المحتوى
إذا كنت بحاجة إلى تنسيق مخصّص، يمكنك تطبيقه
NotificationCompat.DecoratedCustomViewStyle
إلى الإشعار. تتيح لك واجهة برمجة التطبيقات هذه توفير تنسيق مخصص للمحتوى
التي تشغلها عادةً العنوان والمحتوى النصي، مع الاستمرار في استخدام النظام
زخارف لرمز الإشعار والطابع الزمني والنص الفرعي وأزرار الإجراءات.
بعد أن تصبح التخطيطات جاهزة، يمكنك استخدامها كما هو موضّح في المثال التالي:
Kotlin
valnotificationManager=getSystemService(Context.NOTIFICATION_SERVICE)asNotificationManager// Get the layouts to use in the custom notification.valnotificationLayout=RemoteViews(packageName,R.layout.notification_small)valnotificationLayoutExpanded=RemoteViews(packageName,R.layout.notification_large)// Apply the layouts to the notification.valcustomNotification=NotificationCompat.Builder(context,CHANNEL_ID).setSmallIcon(R.drawable.notification_icon).setStyle(NotificationCompat.DecoratedCustomViewStyle()).setCustomContentView(notificationLayout).setCustomBigContentView(notificationLayoutExpanded).build()notificationManager.notify(666,customNotification)
Java
NotificationManagernotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);// Get the layouts to use in the custom notificationRemoteViewsnotificationLayout=newRemoteViews(getPackageName(),R.layout.notification_small);RemoteViewsnotificationLayoutExpanded=newRemoteViews(getPackageName(),R.layout.notification_large);// Apply the layouts to the notification.NotificationcustomNotification=newNotificationCompat.Builder(context,CHANNEL_ID).setSmallIcon(R.drawable.notification_icon).setStyle(newNotificationCompat.DecoratedCustomViewStyle()).setCustomContentView(notificationLayout).setCustomBigContentView(notificationLayoutExpanded).build();notificationManager.notify(666,customNotification);
تجدر الإشارة إلى أنّ لون خلفية الإشعار قد يختلف من جهاز إلى آخر.
والإصدارات. تطبيق أنماط مكتبة الدعم، مثل
TextAppearance_Compat_Notification للنص
TextAppearance_Compat_Notification_Title للعنوان في التنسيق المخصص،
كما هو موضح في المثال التالي. تتكيف هذه الأنماط مع اختلافات الألوان بحيث
لن ينتهي بك الأمر بنص أسود على أسود أو أبيض على أبيض.
تجنُّب ضبط صورة خلفية للعنصر RemoteViews، لأنّ النص
غير قابل للقراءة.
عند تشغيل إشعار أثناء استخدام المستخدم لأحد التطبيقات، تكون النتيجة
على غرار الشكل 1:
الشكل 1. يظهر تنسيق صغير للإشعارات
أثناء استخدام تطبيقات أخرى
يؤدي النقر على سهم التوسيع إلى توسيع الإشعار، كما هو موضح في الشكل 2:
الشكل 2. يظهر تنسيق كبير للإشعارات
أثناء استخدام تطبيقات أخرى
بعد انتهاء مهلة الإشعار، يكون الإشعار مرئيًا فقط في
شريط النظام، الذي يبدو كالشكل 3:
الشكل 3. كيف سيبدو تنسيق الإشعارات الصغير
تظهر في شريط النظام.
يؤدي النقر على سهم التوسيع إلى توسيع الإشعار، كما هو موضح في الشكل 4:
الشكل 4. يظهر تنسيق إشعارات كبير في
شريط النظام.
إنشاء تنسيق مخصّص بالكامل للإشعارات
إذا كنت لا تريد عرض الإشعار العادي في الإشعار
الرمز والعنوان، فاتبع الخطوات السابقة ولكن لا تستدعي setStyle().
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# Create a custom notification layout\n\nTo make your notifications look their best across different versions of Android,\nuse the [standard notification\ntemplate](/training/notify-user/build-notification) to build your\nnotifications. If you want to provide more content in your notification,\nconsider using one of the [expandable notification\ntemplates](/training/notify-user/expanded).\n\nHowever, if the system templates don't meet your needs, you can use your own\nlayout for the notification.\n| **Caution:** When using a custom notification layout, take special care to ensure your custom layout works with different device orientations and resolutions. Although this advice applies to all UI layouts, it's especially important for notifications because the space in the notification drawer is restricted. The height available for a custom notification layout depends on the Android version. On some versions, collapsed view layouts are limited to as little as 48 dp, heads-up view layouts are limited to as little as 88 dp, and expanded view layouts are limited to as little as 252 dp.\n\nCreate custom layout for the content area\n-----------------------------------------\n\nIf you need a custom layout, you can apply\n[`NotificationCompat.DecoratedCustomViewStyle`](/reference/androidx/core/app/NotificationCompat.DecoratedCustomViewStyle)\nto your notification. This API lets you provide a custom layout for the content\narea normally occupied by the title and text content, while still using system\ndecorations for the notification icon, timestamp, sub-text, and action buttons.\n\nThis API works similarly to the [expandable notification templates](/training/notify-user/expanded) by building on the basic notification\nlayout as follows:\n\n1. Build a [basic notification](/training/notify-user/build-notification) with [`NotificationCompat.Builder`](/reference/androidx/core/app/NotificationCompat.Builder).\n2. Call [`setStyle()`](/reference/androidx/core/app/NotificationCompat.Builder#setStyle(androidx.core.app.NotificationCompat.Style)), passing it an instance of [`NotificationCompat.DecoratedCustomViewStyle`](/reference/androidx/core/app/NotificationCompat.DecoratedCustomViewStyle).\n3. Inflate your custom layout as an instance of [`RemoteViews`](/reference/android/widget/RemoteViews).\n4. Call [`setCustomContentView()`](/reference/androidx/core/app/NotificationCompat.Builder#setCustomContentView(android.widget.RemoteViews)) to set the layout for the collapsed notification.\n5. Optionally, also call [`setCustomBigContentView()`](/reference/androidx/core/app/NotificationCompat.Builder#setCustomBigContentView(android.widget.RemoteViews)) to set a different layout for the expanded notification.\n\n| **Note:** If you're creating a customized notification for media playback controls, follow the same recommendations but use the `NotificationCompat.DecoratedMediaCustomViewStyle` class instead.\n\n### Prepare the layouts\n\nYou need a `small` and `large` layout. For this example, the `small` layout\nmight look like this: \n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"\n android:orientation=\"vertical\"\u003e\n\n \u003cTextView\n android:id=\"@+id/notification_title\"\n style=\"@style/TextAppearance.Compat.Notification.Title\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"0dp\"\n android:layout_weight=\"1\"\n android:text=\"Small notification, showing only a title\" /\u003e\n \u003c/LinearLayout\u003e\n\nAnd the `large` layout might look like this: \n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"300dp\"\n android:orientation=\"vertical\"\u003e\n\n \u003cTextView\n android:id=\"@+id/notification_title\"\n style=\"@style/TextAppearance.Compat.Notification.Title\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:layout_weight=\"1\"\n android:text=\"Large notification, showing a title and a body.\" /\u003e\n\n \u003cTextView\n android:id=\"@+id/notification_body\"\n style=\"@style/TextAppearance.Compat.Notification.Line2\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:layout_weight=\"1\"\n android:text=\"This is the body. The height is manually forced to 300dp.\" /\u003e\n \u003c/LinearLayout\u003e\n\n### Build and show the notification\n\nAfter the layouts are ready, you can use them as shown in the following example: \n\n### Kotlin\n\n```kotlin\nval notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager\n\n// Get the layouts to use in the custom notification.\nval notificationLayout = RemoteViews(packageName, R.layout.notification_small)\nval notificationLayoutExpanded = RemoteViews(packageName, R.layout.notification_large)\n\n// Apply the layouts to the notification.\nval customNotification = NotificationCompat.Builder(context, CHANNEL_ID)\n .setSmallIcon(R.drawable.notification_icon)\n .setStyle(NotificationCompat.DecoratedCustomViewStyle())\n .setCustomContentView(notificationLayout)\n .setCustomBigContentView(notificationLayoutExpanded)\n .build()\n\nnotificationManager.notify(666, customNotification)\n```\n\n### Java\n\n```java\nNotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);\n\n// Get the layouts to use in the custom notification\nRemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);\nRemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);\n\n// Apply the layouts to the notification.\nNotification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)\n .setSmallIcon(R.drawable.notification_icon)\n .setStyle(new NotificationCompat.DecoratedCustomViewStyle())\n .setCustomContentView(notificationLayout)\n .setCustomBigContentView(notificationLayoutExpanded)\n .build();\n\nnotificationManager.notify(666, customNotification);\n```\n\nBe aware that the background color for the notification can vary across devices\nand versions. Apply Support Library styles such as\n`TextAppearance_Compat_Notification` for the text and\n`TextAppearance_Compat_Notification_Title` for the title in your custom layout,\nas shown in the following example. These styles adapt to the color variations so\nyou don't end up with black-on-black or white-on-white text. \n\n```xml\n\u003cTextView\n android:layout_width=\"wrap_content\"\n android:layout_height=\"match_parent\"\n android:layout_weight=\"1\"\n android:text=\"@string/notification_title\"\n android:id=\"@+id/notification_title\"\n style=\"@style/TextAppearance.Compat.Notification.Title\" /\u003e\n```\n\nAvoid setting a background image on your `RemoteViews` object, because your text\nmight become unreadable.\n\nWhen you trigger a notification while the user is using an app, the result is\nsimilar to figure 1:\n**Figure 1.** A small notification layout appears while using other apps.\n\nTapping the expander arrow expands the notification, as shown in figure 2:\n**Figure 2.** A large notification layout appears while using other apps.\n\nAfter the notification timeout runs out, the notification is visible only in the\nsystem bar, which looks like figure 3:\n**Figure 3.** How the small notification layout appears in the system bar.\n\nTapping the expander arrow expands the notification, as shown in figure 4:\n**Figure 4.** A large notification layout appears in the system bar.\n\nCreate a fully custom notification layout\n-----------------------------------------\n\n| **Note:** Apps targeting [Android\n| 12](/about/versions/12/behavior-changes-12#custom-notifications) (API level 31) or later can't create fully custom notifications. Instead, the system applies a standard template nearly identical to the behavior of [`Notification.DecoratedCustomViewStyle`](/reference/android/app/Notification.DecoratedCustomViewStyle).\n\nIf you don't want your notification decorated with the standard notification\nicon and header, follow the preceding steps but *don't* call `setStyle()`.\n| **Caution:** We don't recommend using an undecorated notification as this doesn't match other notifications and can cause significant layout compatibility issues on devices that apply different styling to the notification area."]]