티스토리 뷰
2021.06.05 업데이트
둥근 모서리 추가
build.gradle (Module: app)
dependencies {
implementation 'com.google.android.material:material:1.1.0'
}
BottomSheetDialog 는 머티리얼 디자인이므로 의존성 추가
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnClick"
android:layout_width="200dp"
android:layout_height="80dp"
android:text="클릭"
android:textSize="30dp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:background="#FF7F00"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:gravity="center"
android:padding="4dp"
android:background="#FF7F00">
<TextView
android:layout_width="320dp"
android:layout_height="160dp"
android:text="혀가 길지 않은 개발자"
android:textSize="28dp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="20dp">
<Button
android:id="@+id/btnOK"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="확인"
android:textSize="16dp"
android:textStyle="italic|bold"
android:textColor="#FF7F00"
android:background="#FFFFFF" />
<Button
android:id="@+id/btnClose"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="닫기"
android:textSize="16dp"
android:textStyle="italic|bold"
android:layout_marginLeft="8dp"
android:textColor="#FF7F00"
android:background="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
BottomSheetFragment.kt
package com.jwsoft.kotlinproject
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
class BottomSheetFragment(context: Context) : BottomSheetDialogFragment() {
private val mContext: Context = context
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.bottom_sheet, container, false)
val btnOK: Button = view.findViewById(R.id.btnOK)
val btnClose: Button = view.findViewById(R.id.btnClose)
btnOK.setOnClickListener {
Toast.makeText(mContext, "확인", Toast.LENGTH_SHORT).show()
dismiss()
}
btnClose.setOnClickListener {
Toast.makeText(mContext, "닫기", Toast.LENGTH_SHORT).show()
dismiss()
}
return view
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
private lateinit var btnClick: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnClick = findViewById(R.id.btnClick)
val bottomSheetFragment = BottomSheetFragment(applicationContext)
btnClick.setOnClickListener {
bottomSheetFragment.show(supportFragmentManager, bottomSheetFragment.tag)
}
}
}
2021.06.05 업데이트
둥근 모서리 추가.
android:background="@drawable/shp_rect_nl100_r20_r20_r20_r20 이것만 해주면 안됨.
values/styles 에 스타일 추가.
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsTranslucent">true</item>
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@android:color/transparent</item>
</style>
프레그먼트에 테마 설정 추가.
override fun getTheme(): Int = R.style.BottomSheetDialog
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] FrameLayout (0) | 2020.07.02 |
---|---|
[Kotlin] Fragment (0) | 2020.07.01 |
[Kotlin] BottomSheetDialog (0) | 2020.07.01 |
[Kotlin] SharedPreferences + Singleton Pattern (0) | 2020.06.30 |
[Kotlin] Handler vs runOnUiThread vs AsyncTask (0) | 2020.06.29 |
- Total
- Today
- Yesterday
- TabLayout
- Vue.js #Vue.js + javascript
- ViewPager2
- 자바
- View
- CoordinatorLayout
- XML
- ViewModel
- Livedata
- ArrayList
- JSONObject
- Design Pattern
- activity
- Intent
- 코틀린
- DataBinding
- James Kim
- fragment
- JSONArray
- coroutine
- Architecture Pattern
- recyclerview
- java
- Kotlin
- handler
- 안드로이드 #코틀린 #Android #Kotlin
- MVVM
- Android
- 혀가 길지 않은 개발자
- 안드로이드
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |