If the system's built-in dialog cannot meet our daily development needs, we must build our own custom dialog. Especially for an app, a unified style will give us a comfortable feeling, therefore, the style and tone of the dialog are generally consistent with the app topic. This blog mainly introduces two ways to customize the dialog.
MainActivity code
Package com. example. e01_consumerdialog; import android. OS. bundle; import android. app. activity; import android. app. alertDialog; import android. app. alertDialog. builder; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. view. layoutInflater; import android. view. menu; import android. view. view; import android. widget. button; public class MainActivity extends Activity {private Button button; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button1); button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub AlertDialog. builder builder = new Builder (MainActivity. this); builder. setTitle ("Logon interface"); View view = LayoutInflater. from (MainActivity. this ). inflate (R. layout. dialog, null); builder. setView (view); // set the custom Layout view builder. setPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}); builder. setNegativeButton ("cancel", new OnClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}); builder. show () ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
Layout file of dialog. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: id = "@ + id/textView2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/editText1" android: layout_marginTop = "27dp" android: layout_toLeftOf = "@ + id/editText1" android: text = "Password:"/> <EditText android: id = "@ + id/editText2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBaseline = "@ + id/textView2" android: layout_alignBottom = "@ + id/textView2" android: layout_alignLeft = "@ + id/editText1" android: layout_alignRight = "@ + id/editText1" android: EMS = "10" android: inputType = "textPassword"/> <EditText android: id = "@ + id/editText1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBaseline = "@ + id/textView1" android: layout_alignBottom = "@ + id/textView1" android: layout_toRightOf = "@ + id/textView1" android: EMS = "10"/> <TextView android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: layout_alignParentTop = "true" android: text = "username:"/> </RelativeLayout>
The preceding custom dialog simulates the function of implementing a login box option, but its disadvantages are also obvious. The buttons must use the PositiveButton style of dialog, is there any custom method that makes our permissions more powerful? We can see from the official API
Tip:If you want a custom dialog, you can instead displayActivity
As a dialog instead of usingDialog
APIs. Simply create an activity and set its themeTheme.Holo.Dialog
In<activity>
Manifest element:
<activity android:theme="@android:style/Theme.Holo.Dialog" >
That's it. The activity now displays in a dialog window instead of fullscreen.
The above tip tells us that if you want to customize the dialog, you can also add the dialog attribute to your own activity in the list file to achieve the effect of the dialog form!