0Pricing
Android Academy · บทเรียน

พื้นฐาน Material Design

ใช้งาน Material Components ได้แก่ MaterialButton, TextInputLayout, MaterialCardView, FloatingActionButton, Snackbar, Chip, BottomSheetDialog และ Dynamic Color

พื้นฐาน Material Design เป็นบทเรียน Android Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 5 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Android Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Android Academy มีบทเรียนทั้งหมด 5 บทเรียน

Material Design คืออะไร

Material Design คือระบบการออกแบบของ Google สำหรับสร้างแอป Android ที่สวยงามและมีรูปแบบสอดคล้องกัน โดยมีสิ่งต่อไปนี้:

  • คอมโพเนนต์ UI สำเร็จรูปที่หลากหลาย
  • กฎเกี่ยวกับระยะห่าง รูปแบบตัวอักษร และสีที่สอดคล้องกัน
  • คอมโพเนนต์ที่เข้าถึงได้และปรับตามหน้าจอ
  • ภาพเคลื่อนไหวและการเปลี่ยนฉากในตัว

ไลบรารี Material Components for Android (MDC) นำ Material 3 มาสู่ Jetpack

การเพิ่ม Material Components

เพิ่ม dependency และตั้งธีมของแอปให้เป็นธีม Material:

// app/build.gradle:
dependencies {
    implementation 'com.google.android.material:material:1.12.0'
}

// res/values/themes.xml:
<style name="Theme.MyApp" parent="Theme.Material3.DayNight.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorSecondary">@color/secondary</item>
    <!-- ... -->
</style>

MaterialButton

แทนที่ Button แบบพื้นฐานด้วย MaterialButton เพื่อใช้มุมโค้ง เอฟเฟกต์ระลอกคลื่น และการรองรับไอคอน:

<!-- Filled (default) -->
<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    app:icon="@drawable/ic_save" />

<!-- Outlined style -->
<com.google.android.material.button.MaterialButton
    style="@style/Widget.Material3.Button.OutlinedButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cancel" />

<!-- Text button -->
<com.google.android.material.button.MaterialButton
    style="@style/Widget.Material3.Button.TextButton"
    android:text="Learn more" />

TextInputLayout และ TextInputEditText

TextInputLayout ครอบ EditText เพื่อเพิ่มป้ายกำกับแบบลอย ข้อความแสดงข้อผิดพลาด และตัวนับอักขระ:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/tilEmail"
    style="@style/Widget.Material3.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Email address"
    app:helperText="We will never share your email">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress" />

</com.google.android.material.textfield.TextInputLayout>

MaterialCardView

MaterialCardView เพิ่มระดับเงา มุมโค้ง และเส้นขอบให้กับคอนเทนเนอร์:

<com.google.android.material.card.MaterialCardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardCornerRadius="12dp"
    app:cardElevation="4dp"
    app:strokeColor="@color/outline"
    app:strokeWidth="1dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:orientation="vertical">

        <TextView android:text="Card Title" />
        <TextView android:text="Card content goes here" />

    </LinearLayout>
</com.google.android.material.card.MaterialCardView>

FloatingActionButton (FAB)

FAB ใช้เน้นการดำเนินการหลักบนหน้าจอ โดย Material 3 มี 3 ขนาด:

<!-- Standard FAB -->
<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:contentDescription="Add item"
    app:srcCompat="@drawable/ic_add" />

<!-- Extended FAB (icon + text) -->
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    android:text="New Note"
    app:icon="@drawable/ic_edit"
    android:layout_gravity="bottom|end" />

MaterialToolbar

แทนที่ ActionBar เริ่มต้นด้วย MaterialToolbar เพื่อใช้รูปแบบ Material 3:

<!-- In layout XML -->
<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorSurface"
    app:title="My App"
    app:navigationIcon="@drawable/ic_menu" />

// In Activity:
setSupportActionBar(binding.toolbar)
binding.toolbar.setNavigationOnClickListener {
    onBackPressedDispatcher.onBackPressed()
}

แถบข้อความ Snackbar

Snackbar แสดงข้อความสั้น ๆ ที่ดำเนินการต่อได้บริเวณด้านล่างของหน้าจอ ควรเลือกใช้แทน Toast:

// Simple message:
Snackbar.make(binding.root, "Item deleted", Snackbar.LENGTH_SHORT).show()

// With action:
Snackbar.make(binding.root, "Item deleted", Snackbar.LENGTH_LONG)
    .setAction("Undo") {
        viewModel.undoDelete()
    }
    .setAnchorView(binding.fab)   // appears above the FAB
    .show()

ชิปและ ChipGroup

ชิปคือองค์ประกอบขนาดกะทัดรัดสำหรับตัวกรอง แท็ก และตัวเลือก:

<com.google.android.material.chip.ChipGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:singleSelection="true">

    <com.google.android.material.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Kotlin"
        style="@style/Widget.Material3.Chip.Filter" />

    <com.google.android.material.chip.Chip
        android:text="Android"
        style="@style/Widget.Material3.Chip.Filter" />

    <com.google.android.material.chip.Chip
        android:text="Jetpack"
        style="@style/Widget.Material3.Chip.Filter" />

</com.google.android.material.chip.ChipGroup>

BottomSheetDialog

แผ่นด้านล่างแบบโมดัลจะเลื่อนขึ้นจากด้านล่าง และบรรจุเนื้อหาหรือการดำเนินการต่าง ๆ:

class OptionsBottomSheet : BottomSheetDialogFragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.bottom_sheet_options, container, false)
    }
}

// Show it from a Fragment:
val sheet = OptionsBottomSheet()
sheet.show(parentFragmentManager, "options")

Material You — สีแบบไดนามิก

Android 12 ขึ้นไปรองรับ สีแบบไดนามิก — ชุดสีของแอปจะปรับตามวอลเปเปอร์ของผู้ใช้โดยอัตโนมัติ:

// In Application.onCreate():
override fun onCreate() {
    super.onCreate()
    DynamicColors.applyToActivitiesIfAvailable(this)
}

// That's it! Colors automatically extract from wallpaper on Android 12+
// On older Android, fallback colors from your theme are used.

ตรวจสอบความเข้าใจอย่างรวดเร็ว

ควรใช้คอมโพเนนต์ Material ใดครอบ EditText เพื่อให้มีป้ายกำกับแบบลอยและข้อความแสดงข้อผิดพลาดในบรรทัดเดียวกัน

ทบทวน: พื้นฐาน Material Design

Material Components ช่วยให้สร้างแอปที่สวยงามได้ง่าย:

  • MaterialButton — รูปแบบทึบ มีเส้นขอบ และข้อความ
  • TextInputLayout + TextInputEditText — ป้ายกำกับแบบลอยและข้อผิดพลาด
  • MaterialCardView — การ์ดที่มีระดับเงาและเส้นขอบ
  • FloatingActionButton — ปุ่มดำเนินการหลัก
  • Snackbar — ข้อความที่ปิดได้พร้อมการดำเนินการ
  • Chip — แท็ก ตัวกรอง และตัวเลือก
  • สีแบบไดนามิก — ปรับตามวอลเปเปอร์บน Android 12 ขึ้นไป

ถัดไป: ธีม สไตล์ และโหมดมืด

คำถามที่พบบ่อย

บทเรียน “พื้นฐาน Material Design” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “พื้นฐาน Material Design” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Android Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Android Academy มีบทเรียนทั้งหมด 5 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “พื้นฐาน Material Design”

ใช้งาน Material Components ได้แก่ MaterialButton, TextInputLayout, MaterialCardView, FloatingActionButton, Snackbar, Chip, BottomSheetDialog และ Dynamic Color คุณปฏิบัติ Android Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Android Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Android Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 5 บทเรียน

บทเรียน “พื้นฐาน Material Design” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Android Academy นี้ได้ไหม

ได้ บทเรียน Android Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐาน Material Design
  2. ธีมและสไตล์
  3. มุมมองที่กำหนดเอง
  4. ภาพเคลื่อนไหวและการเปลี่ยนผ่าน
  5. การนำทางด้านล่างและแท็บ
← กลับไปที่ Android Academy