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

วิธีตั้งค่าไอคอนแอปพลิเคชัน Android

ตัวอย่างนี้สาธิตวิธีการเปลี่ยนไอคอนแอปพลิเคชัน Android ให้เป็นไอคอนของตัวเอง

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

Android Studio → File → New Project

ขั้นตอนที่ 2: เตรียมไฟล์ไอคอน

  1. เตรียมรูปภาพไอคอน (แนะนำขนาด 512x512 px, รูปแบบ PNG, พื้นที่โปร่งใส)
  2. ใน Android Studio: คลิกขวาที่โฟลเดอร์ res → New → Image Asset
  3. Asset Type: Launcher Icons (Adaptive and Legacy)
  4. Source Asset: เลือกไฟล์รูปภาพของคุณ
  5. ปรับ Trim, Padding, Scaling ให้เหมาะสม → Next → Finish

วิธีนี้จะสร้างไอคอนทุกขนาดที่จำเป็น (mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi) ให้อัตโนมัติในโฟลเดอร์ mipmap-*/

ขั้นตอนที่ 3: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ขั้นตอนที่ 4: MainActivity.java

package com.example.sample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

ขั้นตอนที่ 5: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

หมายเหตุ:

  • android:icon - ไอคอนสี่เหลี่ยม (Legacy)
  • android:roundIcon - ไอคอนวงกลม (Adaptive Icon - Android 8.0+)
  • ทั้งคู่มักจะชี้ไปที่ @mipmap/ic_launcher และ @mipmap/ic_launcher_round ซึ่งถูกสร้างโดย Image Asset Studio

ขนาดไอคอนมาตรฐาน (สำหรับสร้างเอง)

Density ขนาด (px) โฟลเดอร์
mdpi 48x48 mipmap-mdpi
hdpi 72x72 mipmap-hdpi
xhdpi 96x96 mipmap-xhdpi
xxhdpi 144x144 mipmap-xxhdpi
xxxhdpi 192x192 mipmap-xxxhdpi

การทดสอบ

รันแอป → ตรวจสอบไอคอนบน Launcher ของอุปกรณ์ → ควรแสดงไอคอนใหม่ที่ตั้งค่าไว้