با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
از یک نشان برای نمایش یک عنصر بصری کوچک برای نشان دادن وضعیت یا یک مقدار عددی در قابل ترکیب دیگر استفاده کنید. در اینجا چند سناریو رایج وجود دارد که در آن ممکن است از نشان استفاده کنید:
اعلانها : تعداد اعلانهای خوانده نشده را روی نماد برنامه یا زنگ اعلان نمایش دهید.
پیام ها : پیام های جدید یا خوانده نشده را در یک برنامه چت نشان می دهد.
بهروزرسانیهای وضعیت : وضعیت یک کار مانند "تکمیل"، "در حال انجام" یا "شکست خورده" را نشان میدهد.
مقدار سبد خرید : نمایش تعداد اقلام در سبد خرید کاربر.
محتوای جدید : محتوا یا ویژگی های جدید در دسترس کاربر را برجسته کنید.
شکل 1. نمونه های نشان
سطح API
از BadgedBox composable برای پیاده سازی نشان ها در برنامه خود استفاده کنید. در نهایت یک ظرف است. شما ظاهر آن را با این دو پارامتر اصلی کنترل می کنید:
content : محتوای قابل ترکیبی که BadgedBox حاوی آن است. به طور معمول Icon .
badge : قابل ترکیب که به عنوان نشان روی محتوا ظاهر می شود. معمولاً Badge اختصاصی قابل ساخت.
مثال اساسی
این قطعه کد یک پیاده سازی اساسی از BadgedBox را نشان می دهد:
آرگومان برای پارامتر badgeBadgedBoxBadge است. از آنجایی که Badge هیچ آرگومان خاص خود را ندارد، برنامه نشان پیش فرض را نشان می دهد که یک دایره قرمز کوچک است.
Icon به عنوان آرگومان برای پارامتر contentBadgedBox عمل می کند. این نمادی است که نشان روی آن ظاهر می شود. در این مورد، نماد ایمیل است.
اینگونه به نظر می رسد:
شکل 2. اجرای حداقل نشان.
مثال مفصل
قطعه زیر نشان می دهد که چگونه می توانید مقادیری را در نشان نشان دهید که به اقدامات کاربر پاسخ می دهند.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-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-08-27 بهوقت ساعت هماهنگ جهانی."],[],[],null,["Use a [badge](https://m3.material.io/components/badges/overview) to display a small visual element to denote status\nor a numeric value on another composable. Here are a few common scenarios where\nyou might use a badge:\n\n- **Notifications**: Display the number of unread notifications on an app icon or notification bell.\n- **Messages**: Indicate new or unread messages within a chat application.\n- **Status updates**: Show the status of a task, such as \"complete,\" \"in progress,\" or \"failed.\"\n- **Cart quantity**: Display the number of items in a user's shopping cart.\n- **New content**: Highlight new content or features available to the user.\n\n**Figure 1.** Badge examples\n\nAPI surface\n\nUse the [`BadgedBox`](/reference/kotlin/androidx/compose/material3/package-summary#BadgedBox(kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Function1)) composable to implement badges in your application. It\nis ultimately a container. You control its appearance with these two main\nparameters:\n\n- `content`: The composable content that the `BadgedBox` contains. Typically `Icon`.\n- `badge`: The composable that appears as the badge over the content. Typically the dedicated [`Badge`](/reference/kotlin/androidx/compose/material3/package-summary#Badge(androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,kotlin.Function1)) composable.\n\nBasic example\n\nThis code snippet shows a basic implementation of `BadgedBox`:\n\n\n```kotlin\n@Composable\nfun BadgeExample() {\n BadgedBox(\n badge = {\n Badge()\n }\n ) {\n Icon(\n imageVector = Icons.Filled.Mail,\n contentDescription = \"Email\"\n )\n }\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/components/Badges.kt#L62-L74\n```\n\n\u003cbr /\u003e\n\nThis example displays a badge that overlaps the provided `Icon` composable. Note\nthe following in the code:\n\n- [`BadgedBox`](/reference/kotlin/androidx/compose/material3/package-summary#BadgedBox(kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Function1)) serves as the overall container.\n- The argument for the `badge` parameter of `BadgedBox` is [`Badge`](/reference/kotlin/androidx/compose/material3/package-summary#Badge(androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,kotlin.Function1)). Because `Badge` has no arguments of its own, the app displays the default badge, which is a small red circle.\n- `Icon` serves as the argument for the `content` parameter of `BadgedBox`. It is the icon over which the badge appears. In this case, it is a mail icon.\n\nThis is how it appears:\n**Figure 2.** A minimal badge implementation. **Important:** You can pass any composable to the `BadgedBox` composable's content slot. You could therefore create a badge that displays on a `Button`, an `Image`, or any other composable.\n\nDetailed example\n\nThe following snippet demonstrates how you can display values in the badge that\nrespond to user actions.\n\n\n```kotlin\n@Composable\nfun BadgeInteractiveExample() {\n var itemCount by remember { mutableStateOf(0) }\n\n Column(\n verticalArrangement = Arrangement.spacedBy(16.dp)\n ) {\n BadgedBox(\n badge = {\n if (itemCount \u003e 0) {\n Badge(\n containerColor = Color.Red,\n contentColor = Color.White\n ) {\n Text(\"$itemCount\")\n }\n }\n }\n ) {\n Icon(\n imageVector = Icons.Filled.ShoppingCart,\n contentDescription = \"Shopping cart\",\n )\n }\n Button(onClick = { itemCount++ }) {\n Text(\"Add item\")\n }\n }\n}https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/compose/snippets/src/main/java/com/example/compose/snippets/components/Badges.kt#L79-L107\n```\n\n\u003cbr /\u003e\n\nThis example implements a shopping cart icon with a badge that displays the\nnumber of items in the user's cart.\n\n- The [`BadgedBox`](/reference/kotlin/androidx/compose/material3/package-summary#BadgedBox(kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Function1)) displays only when the item count is over 0.\n- The arguments for `containerColor` and `contentColor` control the appearance of the badge.\n- The `Text` composable for the content slot of [`Badge`](/reference/kotlin/androidx/compose/material3/package-summary#Badge(androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,kotlin.Function1)) appears within the badge. In this case, it displays the number of items in the cart.\n\nThis implementation appears as follows:\n**Figure 3.** A badge that displays the number of items in a shopping cart.\n\nAdditional resources\n\n- [Material 3 - Badges](https://m3.material.io/components/badges/overview)\n- [`BadgedBox`](/reference/kotlin/androidx/compose/material3/package-summary#BadgedBox(kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Function1))\n- [`Badge`](/reference/kotlin/androidx/compose/material3/package-summary#Badge(androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,kotlin.Function1))"]]