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

วิธีตรวจสอบขนาดหน้าจออุปกรณ์ Android (Small, Normal, Large, XLarge) แบบโปรแกรม

ภาพรวม

ตัวอย่างนี้แสดงวิธีการตรวจสอบหมวดหมู่ขนาดหน้าจอ (Screen Size Category) และความหนาแน่น (Density) ของอุปกรณ์ Android โดยใช้ Configuration และ DisplayMetrics

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btnGetScreenSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="GetScreenSize"
        android:text="Get Screen Size" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnGetScreenSize"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:onClick="GetScreenDensity"
        android:text="Get Screen Density" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="Android device screen size category (small, normal, large, xlarge)"
        android:textAlignment="center"
        android:textSize="18sp"
        android:textStyle="bold|italic" />
</RelativeLayout>

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

package com.example.screencategory;

import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

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

    public void GetScreenSize(View view) {
        int screenLayout = getResources().getConfiguration().screenLayout;
        int sizeCategory = screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
        
        String sizeName;
        switch (sizeCategory) {
            case Configuration.SCREENLAYOUT_SIZE_SMALL:
                sizeName = "Small (เล็ก)";
                break;
            case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                sizeName = "Normal (ปกติ)";
                break;
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
                sizeName = "Large (ใหญ่)";
                break;
            case Configuration.SCREENLAYOUT_SIZE_XLARGE:
                sizeName = "XLarge (ใหญ่มาก)";
                break;
            default:
                sizeName = "Unknown (ไม่ทราบ)";
        }
        
        Toast.makeText(this, "Screen Size Category: " + sizeName, Toast.LENGTH_LONG).show();
    }

    public void GetScreenDensity(View view) {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        
        int densityDpi = metrics.densityDpi;
        String densityName;
        
        switch (densityDpi) {
            case DisplayMetrics.DENSITY_LOW:
                densityName = "LDPI (120 dpi)";
                break;
            case DisplayMetrics.DENSITY_MEDIUM:
                densityName = "MDPI (160 dpi) - Baseline";
                break;
            case DisplayMetrics.DENSITY_HIGH:
                densityName = "HDPI (240 dpi)";
                break;
            case DisplayMetrics.DENSITY_XHIGH:
                densityName = "XHDPI (320 dpi)";
                break;
            case DisplayMetrics.DENSITY_XXHIGH:
                densityName = "XXHDPI (480 dpi)";
                break;
            case DisplayMetrics.DENSITY_XXXHIGH:
                densityName = "XXXHDPI (640 dpi)";
                break;
            case DisplayMetrics.DENSITY_TV:
                densityName = "TVDPI (213 dpi)";
                break;
            default:
                densityName = "Custom (" + densityDpi + " dpi)";
        }
        
        Toast.makeText(this, "Screen Density: " + densityName + "\nDensityDpi: " + densityDpi + "\nDensity: " + metrics.density, Toast.LENGTH_LONG).show();
    }
}

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.screencategory">
    <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>

การทดสอบ

รันแอปพลิเคชันบนอุปกรณ์จริง กดปุ่ม "Get Screen Size" และ "Get Screen Density" จะแสดง Toast ตามประเภท

วิธีตรวจสอบขนาดหน้าจออุปกรณ์ Android (Small, Normal, Large, XLarge) แบบโปรแกรม

ตารางอ้างอิง Screen Size & Density

CategoryMin dpตัวอย่างอุปกรณ์
Small426 x 320 dpโทรศัพท์เก่า, หน้าจอเล็ก
Normal470 x 320 dpโทรศัพท์ส่วนใหญ่
Large640 x 480 dpแท็บเล็ต 7 นิ้ว
XLarge960 x 720 dpแท็บเล็ต 10 นิ้ว+
DensityDPIRatio
LDPI1200.75x
MDPI1601.0x (Baseline)
HDPI2401.5x
XHDPI3202.0x
XXHDPI4803.0x
XXXHDPI6404.0x

การใช้งานจริง

  • จัดเตรียม Layout/Resource แยกใน res/layout-sw600dp/, res/drawable-xhdpi/ ฯลฯ
  • ใช้ Configuration.SCREENLAYOUT_SIZE_MASK แยกจาก bit อื่นๆ (longscreen, layoutdir)
  • สำหรับ Responsive Design ควรใช้ ConstraintLayout และ dp/sp แทนการตรวจสอบขนาด