Dialog的代码:DrawDialog.kt
class DrawDialog(context: Context, themeResId: Int) : Dialog(context, themeResId) {
private val TAG = DrawDialog::class.java.simpleName
var mContext: Context = context
var drawDot: TextView? = null
var drawLine: TextView? = null
var drawSurface: TextView? = null
var drawTemplate: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setCanceledOnTouchOutside(true)
window?.setGravity(Gravity.BOTTOM)
window?.setWindowAnimations(R.style.BottomSelectAnimation)
var view: View = LayoutInflater.from(mContext).inflate(R.layout.dialog_content_circle, null)
initView(view)
setContentView(view)
}
private fun initView(view: View) {
drawDot = view.findViewById(R.id.text_draw_dot)
drawLine = view.findViewById(R.id.text_draw_line)
drawSurface = view.findViewById(R.id.text_draw_circle)
drawTemplate = view.findViewById(R.id.text)
}
override fun show() {
super.show()
val layoutParams = window!!.attributes
layoutParams.gravity = Gravity.BOTTOM
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
window!!.decorView.setPadding(DensityUtil.dp2px(mContext, 8f), 0, DensityUtil.dp2px(mContext, 8f), DensityUtil.dp2px(mContext, 8f))
window!!.attributes = layoutParams
}
}
style:在styles.xml文件当中加入:
<style name="BottomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowAnimationStyle">@style/BottomSelectAnimation</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
<style name="BottomSelectAnimation" parent="Animation.AppCompat.Dialog">
<item name="android:windowEnterAnimation">@anim/tranlate_dialog_in</item>
<item name="android:windowExitAnimation">@anim/translate_dialog_out</item>
</style>
布局文件:dialog_content_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_dialog"
android:orientation="vertical">
<TextView
android:id="@+id/text_draw_dot"
android:layout_width="match_parent"
android:layout_height="@dimen/column_height"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:drawableStart="@drawable/locate"
android:drawablePadding="16dp"
android:gravity="center_vertical"
android:padding="16dp"
android:text="@string/draw_dot"
android:textColor="#666666"
android:textSize="14sp"/>
<TextView
android:id="@+id/text_draw_line"
android:layout_width="match_parent"
android:layout_height="@dimen/column_height"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:drawableStart="@drawable/line"
android:drawablePadding="16dp"
android:gravity="center_vertical"
android:padding="16dp"
android:text="@string/draw_line"
android:textColor="#666666"
android:textSize="14sp"/>
<TextView
android:id="@+id/text_draw_circle"
android:layout_width="match_parent"
android:layout_height="@dimen/column_height"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:drawableStart="@drawable/circle"
android:drawablePadding="16dp"
android:gravity="center_vertical"
android:padding="16dp"
android:text="@string/draw_surface"
android:textColor="#666666"
android:textSize="14sp"/>
<TextView
android:id="@+id/text_draw_arrow"
android:layout_width="match_parent"
android:layout_height="@dimen/column_height"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:drawableStart="@drawable/arrow"
android:drawablePadding="16dp"
android:gravity="center_vertical"
android:padding="16dp"
android:text="@string/draw_arrow"
android:textColor="#666666"
android:textSize="14sp"/>
</LinearLayout>
用于转换尺寸的DensityUtil,来自网上,但找不到来源了:
object DensityUtil {
/**
* dp转px
*
* @param context
* @param dpVal
* @return
*/
fun dp2px(context: Context, dpFloat: Float): Int{
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpFloat,context.resources.displayMetrics).toInt()
}
/**
* sp转px
*
* @param context
* @param spVal
* @return
*/
fun sp2px(context: Context,spFloat: Float): Int{
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spFloat,context.resources.displayMetrics).toInt()
}
/**
* px转dp
*
* @param context
* @param pxVal
* @return
*/
fun px2dp(context: Context,pxFloat: Float): Float{
var scale : Float = context.resources.displayMetrics.density
return (pxFloat/scale)
}
/**
* px转sp
*
* @param pxVal
* @param pxVal
* @return
*/
fun px2sp(context: Context, pxFloat: Float):Float {
return (pxFloat / context.resources.displayMetrics.scaledDensity)
}
}
在Activity当中的使用:
private var drawDialog: DrawDialog? = null
drawDialog = DrawDialog(this, R.style.BottomDialog)
drawDialog?.show()
文章中除了已经声明的代码之外,仍有部分代码及思路来源网上,如有侵权烦请告知,谢谢~