One, custom dialog Inherit dialog
Public class extends Dialog {
Second, set the style for dialog
Create a new style inheritance in a style
@android: style/theme.dialog or @android:style/theme.holo.dialog
- Set style to remove border
- Remove title
- Set window transparency
- Set click on the outside of the dialog box can disappear etc.
- Set animation
<!--<style name= "Mydialog" parent= "@android: Style/theme.dialog" > - <stylename= "Mydialog"Parent= "@android: Style/theme.holo.dialog"> <!--whether there is a border - <Itemname= "Android:windowframe">@null</Item> <!--is it above the suspended activity ? - <Itemname= "Android:windowisfloating">True</Item> <!--title - <Itemname= "Android:windownotitle">True</Item> <!--Shadows - <Itemname= "Android:windowistranslucent">True</Item><!--Translucent - <!--entering and exiting the animation - <Itemname= "Android:windowanimationstyle">@style/mydialoganimation</Item> <!--you can disappear from the outside. - <Itemname= "Android:windowcloseontouchoutside">True</Item> </style> <stylename= "Mydialoganimation"> <!--Enter - <Itemname= "Android:windowenteranimation">@anim/dialog_enter</Item> <!--Exit - <Itemname= "Android:windowexitanimation">@anim/dialog_exit</Item> </style>
Enter animation
Dialog_enter
Dialog_exit
System comes with can be found directly to be used under the SDK to find
Directory \sdk\platforms\ corresponding API version
<Setxmlns:android= "Http://schemas.android.com/apk/res/android"Android:shareinterpolator= "false" > < ScaleAndroid:fromxscale= "0.9"Android:toxscale= "1.0"Android:fromyscale= "0.9"Android:toyscale= "1.0"Android:pivotx= "50%"Android:pivoty= "50%"android:duration= "$" /> <AlphaAndroid:fromalpha= "0.0"Android:toalpha= "1.0"android:duration= "$" /> </Set>
Exit
<Setxmlns:android= "Http://schemas.android.com/apk/res/android"Android:shareinterpolator= "false"> < Scaleandroid:duration= "$"Android:fromxscale= "1.0"Android:fromyscale= "1.0"Android:pivotx= "50%"Android:pivoty= "50%"Android:toxscale= "0.9"Android:toyscale= "0.9"/> <Alphaandroid:duration= "$"Android:fromalpha= "1.0"Android:toalpha= "0.0"/></Set>
Third, set the style in the construction method
Context mcontext; Public Mydialog (Context context) { Super(context, r.style.mydialog); this. mcontext=context; }
Iv. Setting the layout
@Override protected void onCreate (Bundle savedinstancestate) { super. OnCreate (savedinstancestate); Setcontentview (R.layout.layout_dialog); }
The layout file is 2 TextView
V. Rewrite the show method, set width, height, etc.
@Override public void Show () { super .show (); /** * Set width full screen, to be set after show */ Layoutparams layoutparams = GetWindow (). Getatt Ributes (); layoutparams.gravity =gravity.bottom; Layoutparams.width = layoutparams.match_parent; Layoutparams.height = layoutparams.wrap_content; GetWindow (). Getdecorview (). Setpadding ( 0, 0, 0, 0); GetWindow (). SetAttributes (Layoutparams); }
Six, complete dialog class
/*** Dialog July 30, 2016*/ Public classMydialogextendsDialog {Context mcontext; PublicMydialog (Context context) {Super(context, r.style.mydialog); This. mcontext=context; } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.layout_dialog); } @Override Public voidShow () {Super. Show (); /*** Set width full screen, to be set at the back of show*/Layoutparams Layoutparams=GetWindow (). GetAttributes (); Layoutparams.gravity=Gravity.bottom; Layoutparams.width=layoutparams.match_parent; Layoutparams.height=layoutparams.wrap_content; GetWindow (). Getdecorview (). setpadding (0, 0, 0, 0); GetWindow (). SetAttributes (Layoutparams); } }
Custom dialog width fills the screen