ภาพรวม
ตัวอย่างนี้แสดงวิธีจัดแนวข้อความให้ชิดทั้งสองข้าง (Justify) ใน TextView โดยใช้ Attribute android:justificationMode (รองรับ Android 8.0+ / API 26+)
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project
ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<!-- Justified Text (API 26+) -->
<TextView
android:id="@+id/tvJustified"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:textSize="18sp"
android:justificationMode="inter_word"
android:text="The text in this TextView is justified. This feature is introduced in Android version >= 8.0. The text in this TextView is justified. This feature is introduced in Android version >= 8.0. The text in this TextView is justified. This feature is introduced in Android version >= 8.0." />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#CCC"
android:layout_marginVertical="16dp"/>
<!-- Normal Text (ไม่ Justify) -->
<TextView
android:id="@+id/tvNotJustified"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:textSize="18sp"
android:text="The text in this TextView is not justified. No justification. The text in this TextView is not justified. No justification. The text in this TextView is not justified. No justification." />
</LinearLayout>
ขั้นตอนที่ 3: MainActivity (Java/Kotlin)
// Java
package com.example.justifytext;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
// Kotlin
package com.example.justifytext
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
ขั้นตอนที่ 4: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.justifytext">
<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/Theme.JustifyText">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
การทดสอบ
รันบนอุปกรณ์ Android 8.0+ (API 26+) - TextView แรกจะจัดแนวชิดทั้งสองข้าง TextView ที่สองจะเป็นปกติ
คำอธิบาย justificationMode
none(ค่าเริ่มต้น) - ไม่ Justifyinter_word- เพิ่มช่องว่างระหว่างคำ (Standard Justify)inter_character- เพิ่มช่องว่างระหว่างตัวอักษร (สำหรับภาษา CJK)
รองรับภาษาไทย
ภาษาไทยไม่มีช่องว่างระหว่างคำ การใช้ inter_word อาจไม่ได้ผล ต้องใช้ inter_character หรือ Library ช่วย เช่น ThaiTextView หรือจัดการเองด้วย Spannable
โค้ดแบบ Dynamic (Runtime)
// Java (API 26+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
textView.setJustificationMode(Layout.JUSTIFICATION_MODE_INTER_WORD);
}
// Kotlin
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
textView.justificationMode = Layout.JUSTIFICATION_MODE_INTER_WORD
}
ข้อจำกัด
- ทำงานได้เฉพาะ Android 8.0 (API 26) ขึ้นไป
- ไม่รองรับภาษาที่ไม่มี Space ระหว่างคำ (ไทย, จีน, ญี่ปุ่น, โครเอ็น) ด้วย inter_word
- ประสิทธิภาพ: ข้อความยาวมากอาจทำให้ Layout ช้าลง