Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách ẩn Action Bar vĩnh viễn trong Android?

Giới thiệu

Hướng dẫn ẩn Action Bar (Thanh hành động) hoàn toàn cho toàn bộ ứng dụng hoặc Activity cụ thể thông qua Theme/Style.

Các bước thực hiện

Bước 1: Tạo dự án mới

Khởi tạo project Android Studio.

Bước 2: Giao diện đơn giản (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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="Không có Action Bar"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
</RelativeLayout>

Bước 3: MainActivity cơ bản (src/MainActivity.java)

package app.tutorialspoint.com.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Bước 4: Định nghĩa theme NoActionBar (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 không Action Bar -->
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

Bước 5: Áp dụng theme trong AndroidManifest.xml

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

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Activity sẽ hiển thị không có Action Bar ở trên cùng.

Cách ẩn Action Bar vĩnh viễn trong Android?

Các cách áp dụng Theme NoActionBar

1. Toàn bộ ứng dụng (trong <application>)

android:theme="@style/AppTheme.NoActionBar"

2. Chỉ một Activity (trong <activity>)

<activity android:name=".MainActivity" android:theme="@style/AppTheme.NoActionBar">

3. Kế thừa từ Theme.MaterialComponents (AndroidX hiện đại)

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

Lưu ý

  • Nếu dùng Toolbar thay thế Action Bar, hãy thêm <item name="windowActionBar">false</item> vào theme
  • Theme NoActionBar vẫn giữ lại status bar (thanh trạng thái)
  • Để fullscreen thật sự: thêm <item name="android:windowFullscreen">true</item>