Android custom dialog and its layout
Last Update:2015-04-19
Source: Internet
Author: User
<span id="Label3"></p><p><p><em></em> The <span style="line-height: 0px;">Default dialog in Real-world project development does not meet the requirements, you need to customize the dialog and its layout, and respond to the events of the controls in the Layout. </span></p></p><p><p><span style="line-height: 0px;">On</span></p></p><p><p><em></em></p></p><p><p></p></p><p><p>Custom Dialog,logoutdialog:</p></p><p><p>To pass the custom layout into the constructor, you can get the control through Dialog.findviewbyid in the activity, otherwise null is Returned.</p></p><p><p>public class Logoutdialog extends dialog{<br>Context context;<br>Public Logoutdialog (context Context) {<br>Super (context);<br>this.context=context;<br>This.setcontentview (r.layout.logout_dialog);<br>}</p></p><p><p>Public logoutdialog (context context, int Theme) {<br>Super (context, theme);<br>This.context = context;<br>This.setcontentview (r.layout.logout_dialog);<br>}<br><br>@Override<br>protected void OnCreate (Bundle Savedinstancestate) {<br>Super.oncreate (savedinstancestate);<br>This.setcontentview (r.layout.logout_dialog);<br>}<br>}</p></p><p><p>Custom Layout file, Logout_dialog.xml:</p></p><p><?xml version= "1.0" encoding= "utf-8"?><br><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<br>Android:layout_width= "250dp"<br>android:layout_height= "wrap_content"<br>android:orientation= "vertical"<br>android:background= "@android: color/transparent"<br>><br> <br><linearlayout<br>Android:layout_width= "match_parent"<br>android:layout_height= "wrap_content"<br>Android:background= "@drawable/dialog_top_bg"<br>android:orientation= "vertical"<br>android:paddingtop= "10dp"<br>><br><imageview<br>Android:layout_width= "50dp"<br>android:layout_height= "50dp"<br>Android:src= "@drawable/pop_icon1"<br>android:layout_gravity= "center_horizontal"<br>Android:layout_marginbottom= "10dp"<br>/><br><textview<br>Android:layout_width= "wrap_content"<br>android:layout_height= "wrap_content"<br>android:text= "confirm Logout account"<br>android:layout_gravity= "center_horizontal"<br>Android:layout_marginbottom= "20dp"<br>Android:textcolor= "#FF3C25"<br>android:layout_marginleft= "20dp"<br>android:layout_marginright= "20dp"<br>/><br></LinearLayout><br><linearlayout<br>Android:layout_width= "match_parent"<br>android:layout_height= "40dp"<br>android:gravity= "center_vertical"<br>Android:background= "@drawable/dialog_buttom_bg"<br>><br><textview<br>Android:id= "@+id/back_btn_dialog"<br>Android:layout_width= "wrap_content"<br>android:layout_height= "match_parent"<br>android:text= "back"<br>android:layout_weight= "1"<br>Android:textcolor= "#FFFFFF"<br>android:gravity= "center"<br>/><br><textview<br>Android:layout_width= "wrap_content"<br>android:layout_height= "match_parent"<br>android:text= "|"<br>Android:textcolor= "#C9CACC"<br>android:gravity= "center"<br>/><br><textview<br>Android:id= "@+id/submit_btn_dialog"<br>Android:layout_width= "wrap_content"<br>android:layout_height= "match_parent"<br>android:text= "ok"<br>android:layout_weight= "1"<br>Android:textcolor= "#FFFFFF"<br>android:gravity= "center"<br>/><br></LinearLayout></p><p><p></LinearLayout></p></p><p><p>Two shape layouts, dialog_buttom_bg.xml:</p></p><p><p><?xml version= "1.0" encoding= "utf-8"?><br><shape xmlns:android= "http://schemas.android.com/apk/res/android"<br>><br><corners<br>android:bottomleftradius= "@dimen/dialog_corners"<br>android:bottomrightradius= "@dimen/dialog_corners"<br>/><br><gradient<br>Android:startcolor= "#FF3E25"<br>Android:endcolor= "#FF3E25"<br>Android:centercolor= "#FF3E25"<br>Android:angle= "270"<br>/><br></shape></p></p><p><p>Dialog_top_bg.xml:</p></p><p><p><?xml version= "1.0" encoding= "utf-8"?><br><shape xmlns:android= "http://schemas.android.com/apk/res/android" ><br><corners<br>android:topleftradius= "@dimen/dialog_corners"<br>android:toprightradius= "@dimen/dialog_corners"<br>/><br><gradient<br>Android:startcolor= "#ffffff"<br>Android:endcolor= "#ffffff"<br>Android:centercolor= "#ffffff"<br>Android:angle= "270"<br>/><br></shape></p></p><p><p>Custom styles style, To change the default dialog style. Add a new style under Values/styles.xml:</p></p><p><p><style name= "updateerrorfinishdialog" parent= "@android: style/theme.dialog" ><br><item name= "android:windowframe" > @null </item><br><item name= "android:windownotitle" >true</item><br><item name= "android:windowbackground" > @drawable/dialog_background</item><br><item name= "android:windowisfloating" >true</item><br><item name= "android:windowcontentoverlay" > @null </item><br></style></p></p><p><p>Called in the Activity:</p></p><p><p>Dialog dialog==new logoutdialog (tempactivity.this, r.style.updateerrorfinishdialog);</p></p><p><p>Dialog.setcanceledontouchoutside (false);<br>Dialog.show ();</p></p><p><p>TextView submit_btn_dialog= (TextView) Dialog.findviewbyid (r.id.submit_btn_dialog);<br>TextView back_btn_dialog= (TextView) Dialog.findviewbyid (r.id.back_btn_dialog);<br>Submit_btn_dialog.setonclicklistener (new View.onclicklistener () {<br>@Override<br>public void OnClick (View V) {<br>Toaster.showlongtoast ("ok");<br>Dialog.dismiss ();<br>}<br>});<br>Back_btn_dialog.setonclicklistener (new View.onclicklistener () {<br>@Override<br>public void OnClick (View V) {<br>Toaster.showlongtoast ("return");<br>Dialog.dismiss ();<br>}<br>});</p></p><p><p>Android custom dialog and its layout</p></p></span>