In Android applications, the most missing is the custom dialog, for the system provided by default dialog style, generally do not compound the style we apply.
Custom dialog requires 3 steps:
1, the main rewrite dialog Java class
2, clear dialog Theme, in the Style.xml file add one can
3. How to use
First, create the Custompopdialog2.java class
PackageCom.lenovocw.music.app.widget;ImportCOM.LENOVOCW.ZHUHAIZXT.R;ImportAndroid.app.Dialog;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.WindowManager.LayoutParams;ImportAndroid.widget.ImageView;/** * This custom dialog app in: Share pop-up box to view QR code * * @author shanhy ([email protected]) * @date December 4, 2015 */ Public class CustomPopDialog2 extends Dialog { Public CustomPopDialog2(Context context) {Super(context); } Public CustomPopDialog2(Context context,intTheme) {Super(context, theme); } Public Static class Builder { PrivateContext context;PrivateBitmap image; Public Builder(Context context) { This. Context = Context; } PublicBitmapGetImage() {returnImage } Public void setimage(Bitmap image) { This. image = Image; } PublicCustomPopDialog2Create() {Layoutinflater inflater = (layoutinflater) context. Getsystemservice (Context.layout_inf Later_service);FinalCUSTOMPOPDIALOG2 dialog =NewCUSTOMPOPDIALOG2 (Context,r.style.dialog); View layout = Inflater.inflate (R.layout.dialog_share_qrcode,NULL); Dialog.addcontentview (Layout,NewLayoutparams (Android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Android.view.ViewGr Oup. Layoutparams.wrap_content)); Dialog.setcontentview (layout); ImageView img = (ImageView) Layout.findviewbyid (R.id.img_qrcode); Img.setimagebitmap (GetImage ());returnDialog } }}
Here's a simple explanation, our custom dialog needs to prepare a view layout file, focusing on the Create () method, which in this case shows a picture directly.
Second, add theme in Style.xml, we call R.style.dialog in the Create method
<stylename="Dialog"Parent="Android:style/theme.dialog"> <Item name="Android:background">#00000000 </item><Item name="Android:windowbackground"> @android: color/transparent</Item> <Item name="Android:windownotitle">true</Item> <Item name="Android:windowisfloating">true</Item> </style>
Iii. using a custom dialog
Bitmap bitmap = xxxxx;// 这里是获取图片Bitmap,也可以传入其他参数到Dialog中 CustomPopDialog2.Builder dialogBuild = new CustomPopDialog2.Builder(context); dialogBuild.setImage(bitmap); CustomPopDialog2 dialog = dialogBuild.create(); dialog.setCanceledOnTouchOutside(true);// 点击外部区域关闭 dialog.show();
Eventually:
Android Custom Dialog Simple instance