Android Custom Dialog Simple instance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.