Simple Android custom Dialog instance
In Android applications, the most indispensable thing is the custom Dialog. The default Dialog style provided by the system generally does not compose the style of our applications.
Custom Dialog requires three steps:
1. Mainly rewrite the Java class of Dialog
2. Customize the layout file and set the Dialog Theme. Add one to the style. xml file.
3. Usage
1. Create the CustomPopDialog2.java class
Import android. app. dialog; import android. content. context; import android. graphics. bitmap; import android. view. layoutInflater; import android. view. view; import android. view. windowManager. layoutParams; import android. widget. imageView;/*** the custom Dialog application displays the image in the center of the pop-up box and click other areas to automatically close the Dialog ** @ author SHANHY (365384722@QQ.COM) * @ date August 1, December 4, 2015 */public class CustomPopDialog2 extends Dialog {public CustomPopDialog2 (Context context) {super (context);} public CustomPopDialog2 (Context context, int theme) {super (context, theme);} public static class Builder {private Context context; private Bitmap image; public Builder (Context context) {this. context = context;} public Bitmap getImage () {return image;} public void setImage (Bitmap image) {this. image = image;} public CustomPopDialog2 create () {LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); final CustomPopDialog2 dialog = new CustomPopDialog2 (context, R. style. dialog); View layout = inflater. inflate (R. layout. dialog_share_qrcode, null); dialog. addContentView (layout, new LayoutParams (android. view. viewGroup. layoutParams. WRAP_CONTENT, android. view. viewGroup. layoutParams. WRAP_CONTENT); dialog. setContentView (layout); ImageView img = (ImageView) layout. findViewById (R. id. img_qrcode); img. setImageBitmap (getImage (); return dialog ;}}}
Here is a simple description. We need to prepare a View layout file for custom Dialog, which focuses on the create () method. In this example, an image is directly displayed.
Ii. Customize the layout file of the View and add theme to style. xml
3. Use custom Dialog
Bitmap bitmap = xxxxx; // here is the Bitmap for obtaining images. You can also input other parameters to the Dialog custocustocustocustocustodidialog2.builder dialogBuild = new CustomPopDialog2.Builder (context); dialogBuild. setImage (bitmap); CustomPopDialog2 dialog = dialogBuild. create (); dialog. setCanceledOnTouchOutside (true); // click the external area to close the dialog. show ();
Finally: