Android Custom Toolbar 만들기
사용 버전: Android Studio 4.0.1
사용 언어: Kotlin 1.4.10
기본적으로 안드로이드 앱을 만들면 Toolbar 또는 Appbar 또는 Actionbar가 존재합니다.
이것을 사용자가 맞춤 설정할 수 있습니다.
Material Componenets를 사용합니다.
https://shwoghk14.blogspot.com/2020/06/android-material-components.html
사용 언어: Kotlin 1.4.10
안드로이드 Custom Toolbar 만들기를 해보겠습니다.
기본적으로 안드로이드 앱을 만들면 Toolbar 또는 Appbar 또는 Actionbar가 존재합니다.
이것을 사용자가 맞춤 설정할 수 있습니다.
Material Componenets를 사용합니다.
https://shwoghk14.blogspot.com/2020/06/android-material-components.html
res - values - styles.xml로 갑니다.

AppTheme의 parent를 NoActionBar로 만듭니다.
DayNight의 경우는 야간 모드가 존재하는 스타일입니다.

그리고 layout으로 갑니다.
CoordinatorLayout을 사용합니다.

AppBarLayout과 Toolbar를 추가합니다.

그리고 밑에는 ScrollView를 배치했습니다.

실행해 봅시다.

좋네요. 하지만, 툴바가 이름도 없고 허전하네요.
fragment로 가서 아래를 추가합니다.


멋지네요.
다음은 ScrollView와 연동해서 Toolbar를 숨겨보겠습니다.
Toolbar에는 layout_scrollFlags를 넣고, ScrollView는 NestedScrollView로
변경하고, layout_behavior를 추가합니다.

실행해봅시다.

스크롤을 올리면 위와 같이 툴바가 사라집니다.
뒤로 가기 버튼을 만들어 봅시다.
setDisplayHomeAsUpEnabled를 설정합니다.


버튼은 생겼으나 눌러도 아무런 효과가 없습니다.
setNavigationOcClickListener를 추가합니다.

버튼을 누르면 아래 문구가 나옵니다.

끝.
카테고리: Android
댓글
댓글 쓰기
궁금한 점은 댓글 달아주세요.
Comment if you have any questions.