Android UI Components Advanced (2)--Imitation Windows dialog box
Before starting this chapter, I wish you a happy mid-Autumn Festival, I believe that a lot of friends are put on three days of false ha!
If you have time, go home to accompany your parents! Shuyujingerfengbuzhi, son want to raise and pro not!
Well, the reason and the blessing of the language is here, today is ready to imitate the Windows Style dialog box!
:
I believe most of the alertdialog are like this:
The dialog box that we are explaining to you today is the following:
Compare the two dialog boxes, standing on the user's point of view, I believe you are more in favor of the second color Bright dialog box
Okay, here's how to make a dialog that mimics Windows style!
logical flow of code:
In fact, the layout of the custom dialog box, and then load and complete the corresponding event processing!
Core code parsing:
① button click Effect: Btnexit_selctor.xml
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_pressed=" false "android:drawable=" @drawable/btnexit "/><item Android : State_pressed= "true" android:drawable= "@drawable/btnexit_s"/></selector>
② Layout file for dialog box: Dialog_win.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/relativelayout1 "android:layout_width=" match_parent "android:layout_height=" Match_paren T "android:orientation=" vertical "> <relativelayout android:id=" @+id/titlelayout "android:layout _width= "Match_parent" android:layout_height= "Wrap_content" android:layout_alignparentleft= "true" Android Oid:layout_alignparenttop= "true" android:background= "#53CC66" android:padding= "5DP" > <textview Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= " Information "android:textcolor=" #ffffff "android:textsize=" 15sp "/> <imagebutton and Roid:id= "@+id/btncancle" android:layout_width= "30DP" android:layout_height= "30DP" Android:background = "@drawable/btnexit_selctor" Android:layout_alignparentright= "true"/> </RelativeLayout> <linearlayout Android:id= "@+id/detaillayout" android:layout_width= "wrap_content" android:layout_height= "wrap_content "Android:layout_alignparentleft=" true "android:layout_below=" @+id/titlelayout "Android:layout_margin left= "30DP" android:layout_margintop= "22DP" android:orientation= "vertical" > <textview Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_marginlef t= "10DP" android:layout_margintop= "10DP" android:text= "Imitation Windows dialog box demo" Android:textcolor = "#04AEDA" android:textsize= "18sp"/> <textview android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_marginleft= "10DP" Android:layout_margint Op= "10DP" android:text= "Author: Coder-pig" android:textcolor= "#04AEDA" android:textsize= "18sp"/> </linearlay out> <linearlayout android:layout_margintop= "30DP" android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "horizontal" android:layout_below= "@+id/detaillayou T "> <button android:id=" @+id/btnblog "android:layout_width=" Match_parent "Android oid:layout_height= "40DP" android:background= "@drawable/btnpress_selctor" android:text= "Access Blog" Android: Layout_margin= "5DP" android:layout_weight= "1" android:textcolor= "#ffffff" Android:textsiz E= "20sp"/> <button android:id= "@+id/btnclose" android:layout_width= "Match_paren T "android:layout_height=" 40DP "android:background=" @drawable/btnpress_selctor "android:text=" Off " Android:layout_margiN= "5DP" android:layout_weight= "1" android:textcolor= "#ffffff" android:textsize= "20SP" /> </LinearLayout> </RelativeLayout>
③mainactivity.java
Package Com.jay.example.windowsdialogdemo;import Android.app.activity;import Android.app.alertdialog;import Android.app.alertdialog.builder;import Android.content.context;import Android.content.intent;import Android.net.uri;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imagebutton;import Android.widget.toast;public class Mainactivity extends Activity {private Button btnshow;private View dialogview;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) Btnshow = (Button) Findviewbyid (r.id.btnshow); Btnshow.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {Builder builder = Mybuilder (mainactivity.this); final Alertdialog dialog = Builder.show ();//settings Click on the outside of the screen, the dialog box does not disappear, do not set the point outside the dialog will disappear Dialog.setcanceledontouchoutside (false); Then listen to the three buttons in the dialog box://① the button to close the dialog: Imagebutton imgcancle = (ImageButton) Dialogview.findviewbyid (r.id.btncancle); Imgcancle.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {Dialog.dismiss ();}}); /① Link to blog button Btnblob = (button) Dialogview.findviewbyid (R.id.btnblog); Btnblob.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {toast.maketext (Getapplicationcontext (), "Access blog", Toast.length _short). Show (); Uri uri = uri.parse ("Http://blog.csdn.net/coder_pig"), Intent Intent = new Intent (Intent.action_view, URI); StartActivity (Intent);d Ialog.dismiss ();}); /③ Click the button to exit: Button Btnclose = (button) Dialogview.findviewbyid (r.id.btnclose); Btnclose.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {Dialog.dismiss ();}}); Defines a method that returns a Builder object, which is actually a way to set the dialog box to display the view content private Builder Mybuilder (context context) {Final Layoutinflater Inflater = MainActivity.this.getLayoutInflater (); Alertdialog.builder Builder = new Alertdialog.builder (context);d Ialogview = Inflater.inflate (r.laYout.dialog_win, NULL); return Builder.setview (Dialogview);}});}
example code download:
http://pan.baidu.com/s/1jGone5o
Android UI Components Advanced (2)--Imitation Windows dialog box