写个底部弹窗
继承Dialog,初始化时调用setContentView()设置布局。
class CustomDialog(context: Context) : Dialog(context, R.style.bottom_dialog) {
init {
setContentView(R.layout.dialog_custom)
setBottom()
}
}
DialogKtx
fun Dialog.setCommon() {
window?.run {
val params = attributes
params.gravity = Gravity.CENTER
attributes = params
}
}
fun Dialog.setBottom() {
window?.run {
decorView.setPadding(0, 0, 0, 0)
val params = attributes
params.height = ViewGroup.LayoutParams.WRAP_CONTENT
params.width = ViewGroup.LayoutParams.MATCH_PARENT
params.gravity = Gravity.BOTTOM
attributes = params
}
}
style.xml
<!-- CommonDialog -->
<style name="common_dialog" parent="@android:style/Theme.Dialog">
<!-- 无边框 -->
<item name="android:windowFrame">@null</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 背景 -->
<item name="android:background">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 是否模糊 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 遮罩层 -->
<item name="android:backgroundDimAmount">0.5</item>
</style>
<!-- BottomDialog动画 -->
<style name="bottom_animation" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/dialog_enter</item>
<item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>
<!-- BottomDialog -->
<style name="bottom_dialog" parent="@style/common_dialog">
<item name="android:windowAnimationStyle">@style/bottom_animation</item>
</style>
dialog_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="100%"
android:toYDelta="0%" />
</set>
dialog_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="0%"
android:toYDelta="100%" />
</set>
网友评论