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

วิธีตรวจสอบว่า Action Bar Title ถูกตัดทอน (Truncated) หรือไม่ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีตรวจสอบว่าหัวข้อ (Title) ของ Action Bar ถูกตัดทอน (Truncated) เนื่องจากพื้นที่ไม่พอหรือไม่ โดยใช้เมธอด isTitleTruncated()

ขั้นตอนการสร้างโปรเจกต์

ขั้นตอนที่ 1: สร้างโปรเจกต์ด้วย Theme ที่มี Action Bar

Theme.AppCompat.Light.DarkActionBar

ขั้นตอนที่ 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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:gravity="center" />

</LinearLayout>

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

package com.example.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Home");
            getSupportActionBar().setSubtitle("sairam");
            
            // ตรวจสอบว่า Title ถูกตัดทอนหรือไม่
            boolean isTruncated = getSupportActionBar().isTitleTruncated();
            textView.setText("Title Truncated: " + isTruncated);
        } else {
            textView.setText("Action Bar ไม่พร้อมใช้งาน");
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(menuItem);
    }
}

เมื่อไหร่ Title จะถูก Truncate?

  • Title ยาวเกินพื้นที่ที่มี (Action Bar แคบ)
  • มือถือแนวตั้ง (Portrait) - ความกว้างน้อย
  • มี Action Buttons/Menus มากจน擠พื้นที่ Title
  • ใช้ Custom View ที่กว้างเกินไป

วิธีแก้ไข Title Truncation

วิธีคำอธิบาย
สั้นลงใช้ Title สั้นกระชับ
Toolbar แทน Action Barยืดหยุ่นกว่า ควบคุม Layout เองได้
Custom Viewใช้ setCustomView() ควบคุม Layout เอง
Scrollable Tabใช้ TabLayout แทน Title ยาว

ตัวอย่าง: ทดสอบ Title ยาว

// ลองตั้ง Title ยาวๆ เพื่อดู Truncation
getSupportActionBar().setTitle("นี่คือหัวข้อที่ยาวมากจนอาจถูกตัดทอนบนหน้าจอแคบ");
// ตรวจสอบทันทีอาจยัง false ต้องรอ Layout pass
textView.post(() -> {
    boolean truncated = getSupportActionBar().isTitleTruncated();
    textView.setText("Truncated: " + truncated);
});

ข้อควรระวัง

  • isTitleTruncated() ใช้ได้เฉพาะกับ Action Bar แบบเดิม (ไม่ใช่ Toolbar)
  • ต้องเรียกหลังจาก Layout pass แล้ว (ใช้ view.post() หรือใน onWindowFocusChanged)
  • บาง Theme/Device อาจไม่รองรับคุณสมบัตินี้
  • แนะนำให้ย้ายไปใช้ Toolbar แทน ซึ่งควบคุมได้ดีกว่าและเป็นมาตรฐาน Material Design

โค้ดสำหรับ Toolbar (แทน Action Bar)

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Toolbar ไม่มี isTitleTruncated() แต่คุณควบคุม TextView เองได้
TextView titleView = toolbar.findViewById(R.id.toolbar_title);
// ใช้ titleView.getLayout().getEllipsisCount() > 0 แทน