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

วิธีจัดข้อความให้อยู่กึ่งกลางแนวนอนและแนวตั้งใน TextView ของ Android

การจัดตำแหน่งข้อความกึ่งกลางใน TextView (Horizontal & Vertical Center)

ตัวอย่างนี้สาธิตวิธีใช้แอตทริบิวต์ android:gravity เพื่อจัดข้อความให้อยู่กึ่งกลางแนวนอน แนวตั้ง หรือทั้งสองทิศทางใน TextView

ขั้นตอนที่ 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp">
    <!-- จัดกึ่งกลางแนวตั้ง -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="ข้อความกึ่งกลางแนวตั้ง (Center Vertical)"
        android:background="#E8F5E9"
        android:textSize="16sp" />
    
    <!-- จัดกึ่งกลางแนวนอน -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:text="ข้อความกึ่งกลางแนวนอน (Center Horizontal)"
        android:background="#E3F2FD"
        android:textSize="16sp" />
    
    <!-- จัดกึ่งกลางทั้งสองทิศทาง (แนะนำ) -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="ข้อความกึ่งกลางทั้งสองทิศทาง (Center)"
        android:background="#FFF3E0"
        android:textSize="16sp" />
</RelativeLayout>

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

package app.com.sample;

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);
    }
}

ขั้นตอนที่ 4: กำหนดค่าใน AndroidManifest.xml

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

การทดสอบ

รันแอปพลิเคชัน จะเห็น TextView 3 ตัวแสดงตัวอย่างการจัดตำแหน่งต่างๆ

วิธีจัดข้อความให้อยู่กึ่งกลางแนวนอนและแนวตั้งใน TextView ของ Android

ค่า gravity ที่ใช้ได้

ค่า คำอธิบาย
center กึ่งกลางทั้งแนวนอนและแนวตั้ง (เทียบเท่า center_horizontal|center_vertical)
center_horizontal กึ่งกลางแนวนอน (ซ้าย-ขวา)
center_vertical กึ่งกลางแนวตั้ง (บน-ล่าง)
top ชิดขอบบน
bottom ชิดขอบล่าง
start ชิดขอบเริ่มต้น (ซ้ายใน LTR, ขวาใน RTL)
end ชิดขอบสิ้นสุด (ขวาใน LTR, ซ้ายใน RTL)
left ชิดขอบซ้าย (คงที่ ไม่เปลี่ยนตาม RTL)
right ชิดขอบขวา (คงที่ ไม่เปลี่ยนตาม RTL)
fill_horizontal ขยายข้อความเต็มความกว้าง
fill_vertical ขยายข้อความเต็มความสูง
fill ขยายเต็มทั้งสองทิศทาง
clip_horizontal ตัดส่วนที่เกินความกว้าง
clip_vertical ตัดส่วนที่เกินความสูง

ความแตกต่าง: gravity vs layout_gravity

  • android:gravity - จัดตำแหน่ง **เนื้อหาภายใน** View (ข้อความใน TextView, Child views ใน Layout)
  • android:layout_gravity - จัดตำแหน่ง **View เอง** ภายใน Parent (ใช้ใน LinearLayout, FrameLayout, GridLayout)

การตั้งค่าในโค้ด Java

textView.setGravity(Gravity.CENTER);                    // กึ่งกลางทั้งสอง
textView.setGravity(Gravity.CENTER_HORIZONTAL);         // กึ่งกลางแนวนอน
textView.setGravity(Gravity.CENTER_VERTICAL);           // กึ่งกลางแนวตั้ง
textView.setGravity(Gravity.START | Gravity.TOP);       // รวมหลายค่าได้