728x90
위 이미지 처럼 타이틀바에 아무것도 넣지 않고 뒤로가기 버튼만 추가를 해보자.
앱에서 활성화된 화면에서 잠시 이동시에 정보나 검색조건 등 선택혹은 확인 후 돌아갈때
위 이미지처럼 해 놓으면 사용하기 좋고 깔끔한 듯해서 만들어 보았다.
먼전 layout에 AppBarLayout을 추가하고 그 안에 Toolbar를 넣어주었다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SearchFilter">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.app.AppBarOverlay"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_filter"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/Theme.app.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
.....
</LinearLayout>
해당 모양으로 Toolbar를 추가해 주고,
java Code쪽에 OnCreate 메서드에 아래와 같이 추가해 주었다.
Toolbar toolbar = binding.toolbarFilter;
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("");
깔끔하게 타이틀에 들어가는 텍스트는 없애주고,
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
해당 옵션을 true로 해서 뒤로가기 버튼을 활성화 시켜주었다.
이제 해당 버튼을 눌렀을때 이벤트 처리만이 남았다.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home: //뒤로가기버튼 클릭
finish();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
해당 뒤로가기 버튼을 눌렀을때 종료를 할 수 있도록 이벤트 처리를 위와 같이 해주면 된다.
이후 실행하여 해당 화면을 실행시 아래와 같은 결과를 얻을 수 있다.
끝~
728x90
'Programing > android' 카테고리의 다른 글
최신 안드로이드버전에서 카카오 sdk 추가시 maven 추가로 인한 오류 해결 (0) | 2021.11.30 |
---|---|
[android] 다이얼로그 로딩 화면 구현, loading...중 화면 만들기. (0) | 2021.11.18 |
[android studio] 빌드시 ..checking AAR metadata..compileSdkVersion ... 오류해결 (0) | 2021.11.14 |
[android studio] Class referenced... Cannot resolve class ... 오류 해결 (0) | 2021.11.10 |
[android studio] themes 색상, 상태바 액션바 색상변경 (0) | 2021.11.07 |
댓글