안드로이드
Android Action Bar 숨기기 in Kotlin
devGeek
2022. 3. 3. 22:32
반응형
Action Bar
Action Bar란 활동 제목, Application 수준 탐색 및 기타 대화형 항목을 표시할 수 있는 Activity 내의 기본 Toolbar 이다. 예를 들면, 아래 이미지(그림1)와 같이 "ActionBarHideExample"과 같이 프로젝트를 만들면 기본 값으로 존재하는 뷰다.
그림1. Action Bar 표시됨 | 그림 2. Action Bar 숨겨짐 |
---|---|
![]() |
![]() |
Action Bar를 숨기는 이유
Action Bar를 숨기게 되면 콘텐츠를 표시하는 데 더 많은 공간을 사용할 수 있어 더 몰입할 수 있는 사용자 환경을 제공할 수 있다고 생각한다.
.xml 에서 Action Bar 숨기기
아래와 같이 프로젝트 폴더 중에서 app > res > values > themes.xml 파일에서 코드 한 줄 만으로 앱 전체의 Action Bar를 숨길 수 있다.
그림. 프로젝트 구조
아래 코드 중 style 태그의 속성 중 parent의 값을 "Theme.AppCompat.Light.NoActionBar"
값으로 변경해주면 된다.
## themes.xml ##
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.ActionBarHideExample" 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>
Programmatically Action Bar 숨기기
이 방법은 특정 Activity에서만 Action Bar를 숨겨야 할 때 사용하면 용이할 것 같다.
해당 Activity의 Action Bar를 받아와 mActionBar.hide()
를 통해 숨긴다.
MainActivity.xml
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mActionBar = supportActionBar
mActionBar!!.hide()
}
}
반응형