با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
برای اینکه اعلانهایتان در نسخههای مختلف اندروید به بهترین شکل ظاهر شوند، از الگوی استاندارد اعلانها برای ساخت اعلانهای خود استفاده کنید. اگر میخواهید محتوای بیشتری در اعلان خود ارائه دهید، از یکی از الگوهای اعلان قابل ارتقا استفاده کنید.
با این حال، اگر الگوهای سیستم نیازهای شما را برآورده نمی کند، می توانید از طرح بندی خود برای اعلان استفاده کنید.
طرح بندی سفارشی برای منطقه محتوا ایجاد کنید
اگر به یک طرح بندی سفارشی نیاز دارید، می توانید NotificationCompat.DecoratedCustomViewStyle را در اعلان خود اعمال کنید. این API به شما امکان میدهد یک طرحبندی سفارشی برای ناحیه محتوایی که معمولاً توسط محتوای عنوان و متن اشغال میشود، ارائه دهید، در حالی که همچنان از تزئینات سیستم برای نماد اعلان، مهر زمانی، متن فرعی و دکمههای عمل استفاده میکنید.
این API به طور مشابه با الگوهای اعلان قابل ارتقا با ایجاد طرح اولیه اعلان به شرح زیر عمل می کند:
پس از آماده شدن طرحبندیها، میتوانید مانند مثال زیر از آنها استفاده کنید:
کاتلین
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)
جاوا
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() را صدا نکنید .
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]