Android-custom Dialog
Sunday, April 27, 2014, the weather is fine and calm. This blog post will share a feature that is frequently used in development-custom dialog box. Here I use the image resource shape in Android. how to use it, let's take a look at the code. Android has many graphic resources, which will be summarized and shared later for your convenience. Let's take a look at the specific steps for customizing the Dialog: 1. Modify the default Dialog style (style and topic) of the system)
2. Customize the Dialog layout File
3. You can encapsulate a class by yourself, inherit from the Dialog class or directly use the Dialog class for implementation. To facilitate reuse in the future, we recommend that you encapsulate a Dialog class by yourself.
Download source code: Ghost:299402133 (mobile development enthusiasts Group)
:The specific implementation code is as follows:
1. Modify the style/04_CustomDialog/res/values items? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vc3R5bGVzLnhtbDxiciAvPsztvNPS1M/CtPrC66O6PHByZSBjbGFzcz0 =" brush: java; ">
2. Custom Dialog
Package com. wwj. custom. dialog; import android. app. dialog; import android. content. context; import android. content. res. resources; import android. util. displayMetrics; import android. view. gravity; import android. view. window; import android. view. windowManager;/*** custom Dialog box ** @ author wwj **/public class CustomDialog extends Dialog {private static int default_width = 160; // default width: private static int default_height = 120; // default Height: public CustomDialog (Context context) {super (context);} public CustomDialog (Context context, int layout, int style) {this (context, default_width, default_height, layout, style);} public CustomDialog (Context context, int width, int height, int layout, int style) {super (context, style); // setContentView (layout); // set the Window attribute window Window = getWindow (); WindowManager. layoutParams params = window. getAttributes (); // set the width, height, density, and alignment float density = getDensity (context); params. width = (int) (width * density); params. height = (int) (height * density); params. gravity = Gravity. CENTER; window. setAttributes (params);}/*** get display density ** @ param context * @ return */public float getDensity (Context context) {Resources res = context. getResources (); DisplayMetrics dm = res. getDisplayMetrics (); return dm. density ;}}
3. custom layout/04_CustomDialog/res/layout/dialog_layout.xml
The layout file uses an image resource:/04_CustomDialog/res/drawable/dialog_bg.xml
4. display the custom dialog box
Package com. wwj. custom. dialog; import android. app. activity; import android. OS. bundle;/*** 1. modify the default Dialog style (style and topic) ** 2. custom Dialog layout file ** 3. you can encapsulate a class by yourself, inherit from the Dialog class or directly use the Dialog class for implementation. To facilitate future reuse, we recommend that you encapsulate a Dialog class ** @ author wwj **/public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); CustomDialog custocustocusto= new customDialog (this, R. layout. dialog_layout, R. style. dialogTheme); customDialog. show ();}}