Computer >> 컴퓨터 >  >> 프로그래밍 >> Android

Android에서 액션 바(작업 표시줄)를 영구적으로 비활성화하는 방법

Android에서 액션 바를 영구적으로 비활성화하기

이 튜토리얼에서는 Android 애플리케이션에서 액션 바(Action Bar)를 영구적으로 비활성화하는 방법을 단계별로 살펴봅니다. 액션 바를 제거하면 화면 공간을 콘텐츠에 온전히 활용할 수 있어, 이미지 뷰어·게임·동영상 플레이어처럼 몰입형 UI가 필요한 앱에 특히 유용합니다.

1단계 — 새 프로젝트 생성

Android Studio에서 File → New Project 메뉴로 이동한 후, 프로젝트 생성에 필요한 모든 정보를 입력해 새 프로젝트를 만듭니다.

2단계 — 레이아웃 작성 (res/layout/activity_main.xml)

res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 화면 중앙에 "No Action Bar"라는 문구를 표시하는 간단한 레이아웃입니다.

<?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="No Action Bar"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

</RelativeLayout>

3단계 — MainActivity 작성 (src/MainActivity.java)

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

4단계 — NoActionBar 테마 정의 (res/values/styles.xml)

액션 바를 제거하는 핵심 단계입니다. res/values/styles.xml에 windowActionBar를 false로, windowNoTitle을 true로 설정하는 NoActionBar 스타일을 정의합니다.

<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>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

</resources>

5단계 — 테마 적용 (AndroidManifest.xml)

정의한 스타일을 실제로 적용하려면 AndroidManifest.xml에서 해당 액티비티의 테마를 @style/AppTheme.NoActionBar로 지정해야 합니다. 이 설정이 누락되면 액션 바가 그대로 표시되므로 주의하세요.

<?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"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

앱 실행 및 결과 확인

이제 애플리케이션을 실행해 결과를 확인해 보겠습니다. 실제 Android 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바의 Run 아이콘을 클릭하고, 목록에서 자신의 모바일 기기를 선택합니다. 그러면 기기 화면에 액션 바 없이 콘텐츠만 표시되는 것을 확인할 수 있습니다.

Android에서 액션 바(작업 표시줄)를 영구적으로 비활성화하는 방법

추가 팁

특정 시점에만 일시적으로 액션 바를 숨기고 싶다면 코드에서 getSupportActionBar().hide()를 호출하는 방법도 있습니다. 다시 표시할 때는 getSupportActionBar().show()를 사용하면 됩니다. 반면 위 예제처럼 테마 수준에서 설정하면 원하는 액티비티나 앱 전체에 영구적으로 적용할 수 있다는 장점이 있습니다.