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

วิธีปิด Action Bar อย่างถาวรใน Android

การซ่อน Action Bar/Toolbar ทั้งแอปหรือเฉพาะ Activity

ตัวอย่างนี้แสดงวิธีปิด Action Bar อย่างถาวรโดยใช้ Theme Style windowNoTitle และ windowActionBar

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

Android Studio → File → New Project → กรอกรายละเอียด → Finish

ขั้นตอนที่ 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:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="ไม่มี Action Bar"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
</RelativeLayout>

ขั้นตอนที่ 3: MainActivity.java

package app.tutorialspoint.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: Theme Style - res/values/styles.xml

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

    <!-- Theme ไม่มี Action Bar -->
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

ขั้นตอนที่ 5: ใช้ Theme ใน AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.tutorialspoint.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.NoActionBar"> <!-- ใช้ Theme นี้ -->
        <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>

วิธีอื่นๆ ในการซ่อน Action Bar

1. ซ่อนเฉพาะ Activity (ใน Java/Kotlin)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ต้องเรียกก่อน setContentView
    if (getSupportActionBar() != null) {
        getSupportActionBar().hide();
    }
    setContentView(R.layout.activity_main);
}

2. ใช้ Theme.AppCompat.NoActionBar (Built-in)

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

3. ใช้ Theme.MaterialComponents (Material Design)

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

ความแตกต่าง

วิธีขอบเขตเหมาะสำหรับ
Theme.NoActionBarทั้งแอป / Activityแอปที่ไม่ต้องการ Action Bar เลย
getSupportActionBar().hide()เฉพาะ Activity นั้นซ่อนเงื่อนไข/ชั่วคราว
windowNoTitleTheme levelCustom theme แบบเต็มรูปแบบ

หมายเหตุ

  • หากใช้ Toolbar แทน Action Bar ให้ใช้ Theme.NoActionBar แล้วเพิ่ม Toolbar เองใน Layout
  • Full Screen ให้เพิ่ม <item name="android:windowFullscreen">true</item>