Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีปรับแต่ง Layout ของ Snackbar ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการปรับแต่งรูปลักษณ์ของ Snackbar เช่น สีพื้นหลัง ฟอนต์ สีข้อความ และการเพิ่ม Action button

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@color/colorAccent"
        android:text="Custom Snackbar"
        android:textColor="@android:color/white" />
</LinearLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (MainActivity.java)

package com.example.customsnackbar;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar mSnackBar = Snackbar.make(v, "Custom Color", Snackbar.LENGTH_LONG);
                
                // ดึง TextView ของข้อความหลักและ Action
                TextView mainTextView = mSnackBar.getView().findViewById(com.google.android.material.R.id.snackbar_text);
                TextView actionTextView = mSnackBar.getView().findViewById(com.google.android.material.R.id.snackbar_action);
                
                // ปรับแต่งรูปลักษณ์
                mSnackBar.getView().setBackgroundColor(Color.WHITE);
                
                // โหลดฟอนต์จาก assets (ต้องมีไฟล์ LatoRegular.ttf ใน assets/fonts/)
                Typeface font = Typeface.createFromAsset(getAssets(), "fonts/LatoRegular.ttf");
                mainTextView.setTypeface(font);
                actionTextView.setTypeface(font);
                
                mainTextView.setTextColor(Color.BLACK);
                actionTextView.setTextColor(Color.BLACK);
                
                // เพิ่ม Action button
                mSnackBar.setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Snackbar.make(view, "Action!", Snackbar.LENGTH_SHORT).show();
                    }
                });
                
                mSnackBar.show();
            }
        });
    }
}

ขั้นตอนที่ 4: ตั้งค่า Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.customsnackbar">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

การทดสอบ

รันแอปพลิเคชันบนอุปกรณ์จริง กดปุ่ม "Custom Snackbar" เพื่อดู Snackbar ที่ปรับแต่งแล้ว

วิธีปรับแต่ง Layout ของ Snackbar ใน Android

หมายเหตุเพิ่มเติม

  • ต้องเพิ่ม dependency: implementation 'com.google.android.material:material:1.6.0'
  • วางไฟล์ฟอนต์ LatoRegular.ttf ในโฟลเดอร์ assets/fonts/
  • ใช้ com.google.android.material.R.id แทน android.support.design.R.id สำหรับ Material Components