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

วิธีเปลี่ยนขนาด Action Bar ใน Android

ภาพรวม

ตัวอย่างนี้แสดงวิธีปรับขนาดความสูงของ Action Bar (หรือ Toolbar) ผ่าน Theme Style

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

Android Studio → File → New Project (เลือก Template ที่มี Action Bar เช่น Empty Activity)

ขั้นตอนที่ 2: แก้ไข Style (res/values/styles.xml)

<resources>
    <!-- Base application theme -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- เปลี่ยนขนาด Action Bar ที่นี่ (ค่าเริ่มต้น ~56dp) -->
        <item name="actionBarSize">36dp</item>
    </style>
</resources>

ขั้นตอนที่ 3: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

ขั้นตอนที่ 4: MainActivity

// Java
package com.example.actionbarsize;

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

ขั้นตอนที่ 5: Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.actionbarsize">
    <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"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

การทดสอบ

รันแอป - Action Bar จะมีความสูง 36dp แทนค่าเริ่มต้น 56dp

วิธีอื่นๆ และข้อควรระวัง

1. ใช้ Toolbar แทน Action Bar (แนะนำ)

Action Bar เป็น Legacy ให้ใช้ Toolbar แทน ซึ่งปรับแต่งได้ยืดหยุ่นกว่า:

<!-- Layout -->
<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"/>

// Activity
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// ปรับขนาดได้จาก Layout โดยตรง

2. Theme.NoActionBar + Custom Toolbar

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- ไม่มี Action Bar เริ่มต้น -->
</style>

3. ข้อควรระวัง

  • actionBarSize ต้องใช้หน่วย dp หรือ dip
  • ขนาดเล็กเกินไปอาจทำให้ Title/Icon/Menu ไม่พอดี
  • Material Design Guidelines แนะนำ 56dp (Mobile) / 64dp (Tablet/Desktop)
  • หากใช้ Theme.MaterialComponents.* ให้ใช้ toolbarStyle แทน

สรุป

วิธีง่ายที่สุด: เพิ่ม <item name="actionBarSize">XXdp</item> ใน Style แต่แนะนำให้ย้ายมาใช้ Toolbar แทนเพื่อความยืดหยุ่น