반응형
Notice
Recent Posts
Recent Comments
Link
NOW OR NEVER
[Android] bottomsheet 완전히 펼쳐지게 하기 본문
반응형
bottomsheet layout 파일 전체를 감싸는 태그에 해당 코드 넣기
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
적용한 모습은 아래와 같다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_bottomsheet_calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/all_calendar_bottom_sheet"
android:orientation="vertical"
android:paddingLeft="37dp"
android:paddingRight="37dp"
android:clickable="true"
android:focusable="true"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp">
BottomSheetDialogFragment를 상속 받고 해당 레이아웃을 제어하는 fragment 파일 안에서 아래 함수 넣기
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
val peekHeightInPixels = 0
val behavior = dialog.behavior
if (behavior != null) {
behavior.peekHeight = peekHeightInPixels
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
return dialog
}
- peekHeight는 BottomSheet의 최소 높이를 나타내고, 0으로 설정하면 BottomSheet가 화면의 아래 끝까지 펼쳐진다.
- 원래 xml 파일 상에서 behavior.peekHeight = 0dp로 초기 높이로 설정 해도 onCreateDialog에도 peekHeight를 지정하는 코드와 behavior 상태를 펼쳐진 상태로 지정하는 코드를 넣는 이유
: xml 파일 상에서 behavior.peekHeight = 0dp는 레이아웃 파일을 inflate할 때의 정의된 높이로 계산된다. 그래서 onCreateDialog에서 BottomSheetBehavior 객체를 가져와 peekHeight를 설정하고 상태를 STATE_EXPANDED로 설정함으로써 BottomSheet의 동작을 프로그래밍적으로 제어해야 한다. 이렇게 하면 런타임 중에 BottomSheet를 제어하고 상태를 변경할 수 있다.
'Android' 카테고리의 다른 글
[Android] 직렬화 Serialization (0) | 2024.01.16 |
---|---|
[Android] Navigation에서의 back stack 관리 (0) | 2023.12.26 |
[Android] 키보드에 요소 가려지는 현상 해결 (0) | 2023.12.17 |
[Android] Retrofit 서버 통신 값 null로 올 시 처리 (0) | 2023.12.17 |
[Android] 안드로이드 공식 문서 정리 - Activity 개념 (0) | 2023.11.18 |