-
(1분 읽기) Kotlin BottomSheet 완벽 정리 🚀Android 개발/XML & UI 2025. 1. 30. 13:45
안드로이드 개발에서 가장 인기 있는 화면 전환 방식 중 하나인 BottomSheet!
Dialog보다 직관적이고 UX/UI 측면에서 자연스럽게 화면 전환이 가능해.
면접에서 물어보면 완벽하게 설명하고, 실제 개발에서도 바로 적용할 수 있게 정리해봤어! 😎
💡 BottomSheet란?
BottomSheet는 화면 하단에서 슬라이드 되어 올라오는 UI 컴포넌트야.
부드럽고 직관적인 사용자 경험을 제공하면서도 공간 활용도가 뛰어나!- 📲 Dialog처럼 단순 팝업 형태가 아니어서 화면 연결이 자연스러워
- 🎨 공간 합이 좋고 메인 화면을 가리지 않으면서도 필요한 정보만 표시 가능
📁 프로젝트 폴더 구조 예시
app/ ├── src/ │ ├── main/ │ │ ├── java/com/example/bottomsheet/ │ │ │ ├── MainActivity.kt # 메인 액티비티 │ │ │ ├── BottomSheetBehaviorActivity.kt # BottomSheetBehavior 예제 │ │ │ ├── BottomSheetDialogFragment.kt # BottomSheetDialogFragment 예제 │ │ ├── res/ │ │ │ ├── layout/ │ │ │ │ ├── activity_main.xml # 메인 화면 레이아웃 │ │ │ │ ├── activity_bottom_sheet_behavior.xml # BottomSheetBehavior 예제 XML │ │ │ │ ├── fragment_bottom_sheet.xml # BottomSheetDialogFragment 예제 XML
🎯 BottomSheet 구현 방법 2가지
Kotlin에서는 두 가지 방식으로 BottomSheet를 구현할 수 있어:
1️⃣ BottomSheetBehavior
- XML에 직접 배치하고 Activity 또는 Fragment에서 제어해
- findViewById()로 BottomSheetBehavior 객체를 가져와 상태를 조작할 수 있어
- 직접적인 제어와 화면과의 연결이 자연스럽다는 장점이 있어
📌 activity_bottom_sheet_behavior.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/> </androidx.coordinatorlayout.widget.CoordinatorLayout>
📌 BottomSheetBehaviorActivity.kt
class BottomSheetBehaviorActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_bottom_sheet_behavior) val bottomSheet = findViewById<View>(R.id.bottom_sheet) val behavior = BottomSheetBehavior.from(bottomSheet) behavior.state = BottomSheetBehavior.STATE_EXPANDED // 보이기 } }
2️⃣ BottomSheetDialogFragment
- Fragment 기반으로 show() 메서드를 통해 호출해
- 다른 화면에서 쉽게 호출하고 재사용성이 높아
- FragmentManager를 사용해서 관리하고, UI 분리가 쉬워
📌 fragment_bottom_sheet.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white"> <TextView android:id="@+id/bottom_sheet_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello from BottomSheetDialogFragment!" android:textSize="18sp"/> </FrameLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
📌 BottomSheetDialogFragment.kt
class MyBottomSheetFragment : BottomSheetDialogFragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_bottom_sheet, container, false) } }
📌 MainActivity.kt에서 실행
val bottomSheet = MyBottomSheetFragment() bottomSheet.show(supportFragmentManager, "MyBottomSheet")
🔥 BottomSheetBehavior vs BottomSheetDialogFragment vs BottomSheetDialog
방식 설명 Fragment 필요 여부 관리 방법 BottomSheetBehavior XML에 직접 배치하고 Activity나 Fragment에서 제어 ❌ 필요 없음 findViewById()로 직접 관리 BottomSheetDialog Dialog처럼 **show()**로 호출 ❌ 필요 없음 show() 호출 BottomSheetDialogFragment Fragment 기반 다이얼로그로 UI 분리가 용이 ✅ 필요 FragmentManager로 관리
⚖️ 장점과 단점 비교
✅ BottomSheetBehavior 장점
- 화면 연결이 자연스럽고 직관적이야
- Activity나 Fragment와의 의존성이 낮아 독립적으로 사용 가능해
- 애니메이션 커스터마이징이 자유로워
❌ BottomSheetBehavior 단점
- 직접 제어해야 해서 코드가 길어질 수 있어
- 복잡한 UI 관리가 힘들고, Fragment 간 데이터 전달이 어려울 수 있어
✅ BottomSheetDialogFragment 장점
- Fragment 기반이라 화면 분리 및 재사용이 용이해
- Lifecycle 관리가 쉬워서 상태 유지가 좋아
- show() 호출로 간단히 띄울 수 있어
❌ BottomSheetDialogFragment 단점
- FragmentManager 의존성이 있어 코드가 복잡해질 수 있어
- Activity 또는 Fragment가 제거되면 함께 사라져
💡 언제 사용해야 할까?
- BottomSheetBehavior 👉 단순한 UI나 화면 연결이 자연스러워야 할 때
- 예: 메뉴, 알림, 간단한 폼
- BottomSheetDialogFragment 👉 복잡한 UI 또는 재사용성이 필요할 때
- 예: 상세 정보 보기, 입력 폼
🎯 면접에서 이렇게 답변하자! 🚀
"BottomSheet는 화면 하단에서 슬라이드 되어 올라오는 UI 컴포넌트로, 사용자 경험을 자연스럽게 연결할 수 있습니다. BottomSheetBehavior는 XML에 직접 배치하고 Activity나 Fragment에서 제어하며, 연결이 자연스럽고 애니메이션 커스터마이징이 자유롭습니다. 반면, BottomSheetDialogFragment는 Fragment 기반이라 화면 분리와 재사용이 용이하고, Lifecycle 관리가 편리합니다. 단순한 UI는 BottomSheetBehavior를, 복잡한 UI나 재사용이 필요한 경우는 BottomSheetDialogFragment를 사용하는 것이 좋습니다."
🚀 요약
- BottomSheetBehavior: 단순 UI, Activity/Fragment 연결이 자연스러움
- BottomSheetDialogFragment: 복잡한 UI, 재사용성 필요, Fragment 기반 관리
- 자연스러운 화면 연결은 BottomSheetBehavior, 복잡한 UI 관리는 BottomSheetDialogFragment!
이제 BottomSheet의 모든 것을 완벽히 이해했지? 😎
면접에서도 당황하지 않고 자신 있게 대답할 수 있을 거야! 🚀
상황에 맞게 BottomSheetBehavior와 BottomSheetDialogFragment를 적절히 선택해보자! 🎉'Android 개발 > XML & UI' 카테고리의 다른 글
(30초 읽기) 안드로이드에서 텍스트 자간(글자 간격) 조정 방법! 🎨✨ (0) 2025.02.17 (1분 읽기) 폰트 다운로드 & 프로젝트에 추가하기 🎨 (1) 2025.02.17 (30초 읽기) 리사이클러뷰 (RecyclerView) 🍏 (0) 2025.02.01 (30초 읽기) BottomSheetDialogFragment에서는 CoordinatorLayout이 필요할까? 🤔 (0) 2025.01.30 (1분 읽기) 리스트뷰(ListView) vs 리사이클러뷰(RecyclerView) 완벽 비교 🤔 (0) 2025.01.16