TopAppBar และไอคอนการดำเนินการ
เพิ่มแถบชื่อพร้อมการดำเนินการ
TopAppBar และไอคอนการดำเนินการ เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Title Bar
The strip across the top with your screen name and actions is the TopAppBar. It gives users a stable anchor that says where they are. 🧭
A Minimal TopAppBar
At its simplest, a TopAppBar just needs a title. Drop it into the Scaffold topBar slot and you have a real Material header.
TopAppBar(
title = { Text("Inbox") }
)Title Is a Composable
Notice the title takes a Composable lambda, not a String. That means you can put any content there, though usually a single Text fits best.
The navigationIcon Slot
The leading navigationIcon slot holds your back or menu button. Wrap an Icon in an IconButton so it is tappable and sized correctly.
navigationIcon = {
IconButton(onClick = { }) {
Icon(Icons.Default.Menu, "Menu")
}
}The actions Slot
Trailing buttons go in the actions slot. It is a RowScope, so each IconButton you add lines up neatly on the right edge.
actions = {
IconButton(onClick = { }) {
Icon(Icons.Default.Search, "Search")
}
}Always Pass contentDescription
Every Icon in a bar needs a contentDescription. Screen readers announce it, so a Search icon should say Search, not stay silent.
Bar Variants
Material 3 offers CenterAlignedTopAppBar, MediumTopAppBar, and LargeTopAppBar. They differ only in title size and alignment, so pick by feel.
Coloring the Bar
Use TopAppBarDefaults.topAppBarColors to recolor the container and title. Lean on theme colors so the bar stays consistent in light and dark.
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primary
)Collapsing Bars
Pass a scrollBehavior and connect it to your content's nested scroll to make large bars shrink as the user scrolls down.
Do Not Overcrowd
Keep actions to two or three icons. Move the rest behind an overflow menu so the bar stays readable on small phones.
Wire It to Scaffold
The bar lives in Scaffold's topBar slot, so the content area shrinks automatically and your screen body never hides behind it.
Scaffold(topBar = { TopAppBar(title = { Text("Home") }) }) { p -> }Quick Check
You add a search IconButton to a TopAppBar. Where does it belong?
Recap
A TopAppBar holds a title, a leading navigationIcon, and trailing actions. Describe every icon, keep it tidy, and place it in the topBar slot. ✨
คำถามที่พบบ่อย
บทเรียน “TopAppBar และไอคอนการดำเนินการ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “TopAppBar และไอคอนการดำเนินการ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “TopAppBar และไอคอนการดำเนินการ”
เพิ่มแถบชื่อพร้อมการดำเนินการ คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “TopAppBar และไอคอนการดำเนินการ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม
ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- Scaffold: โครงร่างหน้าจอ
- TopAppBar และไอคอนการดำเนินการ
- แถบด้านล่างและปุ่มการดำเนินการลอย
- Snackbar และ SnackbarHostState