Development/Android

안드로이드 액션바 제거하기

Jamie 2021. 11. 8. 14:12
반응형

안드로이드 개발시에 AppCompatActivity를 상속받는 Activity를 MainActivity로 사용할경우

앱바(Appbar) 혹은 액션바(ActionBar)가 액티비티 상단에 기본적으로 나타나게 되어있다.

 

안드로이드 앱바 (https://developer.android.com/training/appbar/setting-up#utility)

 

화면기획에 따라 ActionBar를 제거해야하는 일이 있는데 다음과 같은 방법으로 제거가 가능하다.

 

1. android:theme에서 제거하기

android:theme에서 제거하기위해서는 현재 manifest에서 application의 속성에 사용중인 theme를 확인한다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.settings">

    <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/Theme.AndroidSettingsSample">
        <activity
            android:name=".ui.SettingsActivity"
            android:exported="true"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

위의 manifest.xml 파일에서 android:theme에 Theme.AndroidSettingsSample에 따라가보면 다음과 같이 resource xml 파일이 있다.

 

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.AndroidSettingsSample" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

위의 xml 코드에서 style name 속성의 parent의 속성값을 변경해준다.

Theme.MaterialComponents.DayNight.DarkActionBar -> Theme.MaterialComponents.DayNight.NoActionBar

 

* 여기서 NoActionBar의 위치는 환경의 기본테마에 따라 다를 수 있습니다.

 

2. 코드상에서 hide() 메서드를 이용하여 제거하기

프로젝트 생성후 AppCompatActivity를 상속받는 MainActivity에서 다음과 같은 코드를 작성해준다.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)
        if (savedInstanceState == null) {
            // Load to settings fragment
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings, SettingsFragment())
                .commit()
        }
        //supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.hide() // Disable android appbar
    }
}

위의 코드처럼 supportActionBar?.hide() 메서드를 호출하면 간단히 ActionBar를 제거할 수 있다.

다만 위의 방법대로하면 다른 Activity 생성시 같은 코드를 넣어서 ActionBar를 제거해주어야한다.

 

출처

반응형