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

Android에서 투명한 상태 표시줄과 액션바(ActionBar)를 만드는 방법

이 예제에서는 Android에서 투명한 상태 표시줄(Status Bar)과 액션바(ActionBar)를 만드는 방법을 단계별로 소개합니다.

1단계 — Android Studio에서 새 프로젝트 생성

Android Studio를 실행하고 File ⇒ New Project 메뉴로 이동한 뒤, 프로젝트 생성에 필요한 모든 정보를 입력하여 새 프로젝트를 만듭니다.

2단계 — 레이아웃 파일 작성

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"
    android:background="#222222">
    <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>

3단계 — MainActivity 작성

src/MainActivity.java 파일에 다음 코드를 추가합니다.

package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

4단계 — 투명 액션바 스타일 정의

res/values/styles.xml 파일에 다음 코드를 추가합니다. 여기서 AppTheme.ActionBar.Transparent라는 새로운 테마를 정의하여 액션바를 투명하게 만듭니다.

<resources>
    <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>
    <style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
        <item name="android:windowContentOverlay">@null</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="colorPrimary">@android:color/transparent</item>
    </style>
</resources>

5단계 — 매니페스트에 테마 적용

Manifests/AndroidManifest.xml 파일에 다음 코드를 추가하고, MainActivity에 위에서 정의한 투명 액션바 테마를 지정합니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.app.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">
        <activity android:name=".MainActivity" android:theme="@style/AppTheme.ActionBar.Transparent">
            <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에서 투명한 상태 표시줄과 액션바(ActionBar)를 만드는 방법

핵심 포인트 정리

  • windowActionBarOverlay = true: 액션바가 콘텐츠 위에 겹쳐(overlay) 표시되도록 설정합니다.
  • colorPrimary = @android:color/transparent: 액션바의 배경색을 투명하게 만듭니다.
  • android:windowContentOverlay = @null: 액션바 하단의 그림자 선을 제거하여 깔끔한 투명 효과를 완성합니다.