今天抽空把dialog做个总结,我们在平常android开发中一般用到dialog,如果不是特殊需要的话要么用原生的要么用自定义的dialog
原生的dialog用法一般是这样的
new AlertDialog.Builder(this).setTitle("warn") .setMessage("test") .setPositiveButton("ok",null) .setNegativeButton("cancel",null).show();或者是用v7包里面的
new android.support.v7.app.AlertDialog.Builder(this).setTitle("warn") .setMessage("test") .setPositiveButton("ok",null) .setNegativeButton("cancel",null).show(); //v7兼容性比较好,有的机型 colorAccent 设置值,使用上面的包dialog里面字cancel和ok的字颜色并不会生效,但是使用v7包 的话就会生效这样虽然很方便,但是的话,如果有自定义需求的话就很不美观,我们可能需要自定义对话框效果,还有动画效果等等,这个时候就可以给dialog设置自定义view
private void showCustomDialogView() { //设置style文件目的,是解决底部弹出的时候,覆盖掉默认background距离上下左右的16dp的间距 AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.full_width_dialog); final AlertDialog dialog = builder.create(); View view = View.inflate(this,R.layout.custom_view,null); //设置弹窗的view dialog.setView(view); dialog.show(); //下面这些设置必须在show之后 Window window = dialog.getWindow(); //设置弹出的动画 window.setWindowAnimations(R.style.testAnim); //设置弹窗的宽度和高度 WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); //弹出位置位于底部 lp.gravity = Gravity.BOTTOM; //设置dialog的宽和高 lp.width = LinearLayout.LayoutParams.MATCH_PARENT; lp.height = LinearLayout.LayoutParams.WRAP_CONTENT; window.setAttributes(lp); }
<!--下面这个style是用来覆盖默认的dialog窗体背景样式会距离上下左右默认16dp的问题--> <style name="full_width_dialog" parent="@style/Theme.AppCompat.Dialog"> <item name="android:windowBackground">@android:color/white</item> </style><!--下面这个style是用来实现dialog从底部进入和从底部滑出的动画--> <style name="testAnim" parent="@android:style/Animation"> <item name="android:windowEnterAnimation">@anim/in</item> <item name="android:windowExitAnimation">@anim/out</item> </style>
动画文件in:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromYDelta="100%p" android:toYDelta="0"/> </set>
动画文件out:
<?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%p"/> </set>
网上还看到一种setContentview的,也差不多:
private void showCustomDialogContentView() { AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.full_width_dialog); final AlertDialog dialog = builder.create(); dialog.show(); View view = View.inflate(this,R.layout.custom_view,null); //setContentView的话设置自定义view必须放在show后面 //其实就是窗体设置必须在dialog之后 Window window = dialog.getWindow(); //设置内容 window.setContentView(view); //使得EditText可用 window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); //设置dialog宽和高 window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //设置弹出动画 window.setWindowAnimations(R.style.testAnim); WindowManager.LayoutParams wlp = dialog.getWindow().getAttributes(); //下面这行代码是设置从底部弹出 wlp.gravity = Gravity.BOTTOM; dialog.getWindow().setAttributes(wlp); }
还有一种方式是用透明布局activity实现弹出框 配合刚才in和out进出acitivty的动画,感觉挺好的
<!--下面这个style是用来作为透明背景activity来使用的,实现类似dialog一样的效果--> <style name="NoTitleTranslucentTheme" parent="AppTheme"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsTranslucent">true</item> </style>
如果项目中有大量相同的自定义dialog,也可以直接继承dialog,setContentView,写个自定义类,暴露接口出来,就ok了,还有一种就是直接用popupwindow,指定显示的位置,也可以达到同样的效果