对话框 dialog 整理
1、简单的对话框(默认布局):
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("尊敬的用户"); builder.setMessage("你真的要卸载我吗?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); AlertDialog alert = builder.create(); alert.show();
2、带选项的对话框:
AlertDialog.Builder builder=new AlertDialog.Builder(InfoActivity.this); builder.setTitle("头像"); builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { } }); String[] items=new String[]{"相册上传","拍摄上传","下载头像"}; builder.setItems(items, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { if(which == 0){ //从相册获取图片 // Intent intent = new Intent(Intent.ACTION_PICK, null); // intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); // startActivityForResult(intent, PHOTO_ZOOM); } else if(which == 1){ //从拍照获取图片 // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment // .getExternalStorageDirectory(),"temp.jpg"))); // startActivityForResult(intent, PHOTO_GRAPH); } else if(which == 2){ // Matrix m = image.getImageMatrix(); } } }); builder.create().show();
3、自定义对话框 修改宽度、高度
编写一个对话框样式,这里可以修改颜色,有了这个样式,对话框才可以自定义宽度和高度(如果不考虑宽高的话,这步可以省略):
<style name="DialogShareTheme" parent="Theme.AppCompat.Dialog"> <item name="android:background">@color/white</item> <item name="android:windowBackground">@color/transparent</item> </style>
接着,编写自定义对话框的布局文件:dialog.xml
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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="match_parent"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Test" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
最后,就可以在java中设置其宽度高度了:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ProductionInformationActivity.this, R.style.DialogShareTheme); //加入样式 final View dialogView = LayoutInflater.from(ProductionInformationActivity.this).inflate(R.layout.dialog,null); //加入布局 dialogBuilder.setView(dialogView); Dialog dialog = dialogBuilder.create(); dialog.getWindow().setGravity(Gravity.BOTTOM); dialog.show(); WindowManager.LayoutParams p = dialog.getWindow().getAttributes(); p.width = MyApp.getScreenWidth(this); //设置dialog的宽度为当前手机屏幕的宽度 dialog.getWindow().setAttributes(p);
4、将对话框修改成圆角的(或修改颜色):
定义一个shape文件,然后就可以在java中使用了:
Window window = dialog_demo.getWindow();
window.setBackgroundDrawable(getDrawable(R.drawable.shape_dialog_demo));
5、给对话框添加动画:
先添加一个动画文件(res->anim文件夹中),文件名为a.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fromXScale="0.0" android:fromYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.0" android:toYScale="1.0"> </scale> </set>
接着在style.xml文件中加上:
//对话框动画 <style name="dialog_animation" parent="@android:style/Animation.Dialog"> <item name="android:windowEnterAnimation">@anim/a</item> <!--<item name="android:windowExitAnimation">@anim/a</item>--> </style>
最后在java中调用:
Window window = dialog.getWindow();
window.setWindowAnimations(R.style.dialog_animation);

更多精彩