android-their definition dialog

Source: Internet
Author: User

android-their definition dialog


Sunday April 27, 2014 sunny mood calmthis blog post to share a feature that is often used in development-Define your own dialog box, where I use the graphics resources in Android shape, detailed usage, you see the code bar, Android has more than the graphics resources, the wizard will also summarize the share. Convenient for you to use. Let's take a look at the detailed steps for defining dialog: 1. Change the System default dialog style (style, theme)
2. Define your own dialog layout file
3. Ability to encapsulate a class yourself. inherited from Dialog or directly using the dialog class to implement, in order to facilitate reuse later, it is recommended to encapsulate a dialog class
source code Download: http://download.csdn.net/detail/wwj_748/7261031 Interested friends can add the group I created. There is a wealth of learning resources in it: 299402133 (mobile development enthusiast Group)
: detailed implementation code such as the following:
1. Change Style/04_customdialog/res/values/styles.xml
Add the following code:
<!--dialog box theme--    <style name= "Dialogtheme" parent= "@android: Style/theme.dialog" >        <item name= " Android:windowbackground "> @android:color/transparent</item>        <item name=" Android:windownotitle " >true</item>    </style>

2. Define your own 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;/** * Customize dialog box * * @author WWJ * */public class Customdialog Extend s Dialog {private static int default_width = 160;//default width private static int default_height = 120;//default height public Customdialo G (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);//Set Content Setconten TView (layout);//Set form Properties Window window = GetWindow (); Windowmanager.layoutparams params = window.getattributes ();//set width, height, density, alignment float density = getdensity (context); Params.width = (int) (width * density);p arams.height = (int) (height * density);p arams.gravity = Gravity.center;window.SetAttributes (params);} /** * Get display density * * @param context * @return */public float getdensity (context context) {Resources res = Context.getresource S ();D isplaymetrics dm = Res.getdisplaymetrics (); return dm.density;}}


3. Define the layout yourself/04_customdialog/res/layout/dialog_layout.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Fill_ Parent "    android:layout_height=" fill_parent "    android:background=" @drawable/dialog_bg "    android: gravity= "center"    android:orientation= "vertical" >    <progressbar        style= "@style/dialogtheme"        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"/>    <textview        android:id= "@+id/message"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_ Content "        android:layout_margintop=" 5DP "        android:text=" is running ... "/></linearlayout>

An image resource is used in the layout file:/04_customdialog/res/drawable/dialog_bg.xml
<?

XML version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" Android:shape= "Rectangle" > <corners android:radius= "10DP"/> <solid android:color= "#55000000" /></shape>



4. Display your own Definition dialog box
Package Com.wwj.custom.dialog;import android.app.activity;import android.os.bundle;/** * 1. Change the System default dialog style (style, theme) *  * 2. Define the dialog layout file yourself *  3. Be able to encapsulate a class, inherit from dialog or directly use dialog class to implement, in order to facilitate reuse later, it is recommended to 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 Customdialog = new Customdialog (this,r.layout.dialog_layout, R.style.dialogtheme); CustomDialog.show ();}}





Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

android-their definition dialog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.