ภาพรวม
ตัวอย่างนี้แสดงวิธีปรับแต่งสีและฟอนต์ของ ListView ใน Android โดยใช้ Custom Adapter เพื่อควบคุมลักษณะของแต่ละรายการ (List Item) ได้อย่างเต็มที่
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็นเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 2: จัดทำ Layout หลัก (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="10dp"
android:background="#fbf9ea"
tools:context=".MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
ขั้นตอนที่ 3: สร้าง Layout สำหรับ List Item (res/layout/list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp"
android:textColor="#333333"
android:fontFamily="sans-serif-medium"
android:background="@android:color/white" />
ขั้นตอนที่ 4: สร้าง Custom Adapter (src/CustomAdapter.java)
package com.app.sample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class CustomAdapter extends ArrayAdapter<String> {
private Context context;
private List<String> items;
public CustomAdapter(@NonNull Context context, int resource, @NonNull List<String> items) {
super(context, resource, items);
this.context = context;
this.items = items;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
String item = items.get(position);
TextView textView = view.findViewById(R.id.textView);
textView.setText(item);
// สลับสีพื้นหลังแบบคู่/คี่ (Zebra Striping)
if (position % 2 == 0) {
view.setBackgroundColor(context.getResources().getColor(android.R.color.white));
} else {
view.setBackgroundColor(context.getResources().getColor(android.R.color.darker_gray));
}
return view;
}
}
ขั้นตอนที่ 5: MainActivity (src/MainActivity.java)
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.lv);
List<String> data = Arrays.asList(
"รายการที่ 1", "รายการที่ 2", "รายการที่ 3",
"รายการที่ 4", "รายการที่ 5", "รายการที่ 6",
"รายการที่ 7", "รายการที่ 8", "รายการที่ 9",
"รายการที่ 10"
);
CustomAdapter adapter = new CustomAdapter(this, R.layout.list_item, data);
listView.setAdapter(adapter);
}
}
ขั้นตอนที่ 6: กำหนดค่า Manifest (Manifests/AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.app.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 จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity แล้วคลิกไอคอน Run - ListView จะแสดงข้อมูลพร้อมสีสลับและฟอนต์ที่กำหนดเอง