Some features of Android Dialog

Source: Internet
Author: User

1. The difference between Dialog and Alertdialog.

Alertdialog is a special form of Dialog. In this class, we can add one, two or three buttons to set the caption. So, when we want to use the Alertdialog default button form, the Alertdialog is more convenient, and there is a class alertdialog.builder easy to create a alertdialog.

  

2. Dialog and Alertdialog to be aware of when writing code.

We can give a Dialog a custom Layout. There are two locations to set Layout.

    • One is the structure of the body
    • One is in the OnCreate method

  If you use Alertdialog, you need to write in the struct

        NULL );        Setview (Contentview);

And not directly write Setcontentview (R.layout ...); This results in an error:e/androidruntime (4544): Android.util.AndroidRuntimeException:requestFeature () must be called before Adding content
  
If you write in OnCreate, you can use Setcontentview directly (R.layout ...); without causing an error. However, there are differences between the two ways!

if it is written in a struct, the layoutinflater is used, and the real shell remains the default Dialog of the shell. That is, there are Dialog default title, default background color and so on.

but if written in OnCreate, use Setcontentview (R.layout ...); In this case, the Dialog default layout shell does not exist and uses our custom layout. For example, the background color of your Layout is transparent, so the background of the Dialog is transparent. In the case of the structure, the background color is the default (black or white) of the system Dialog. And this method can no longer use the system default title bar, because it does not exist. So if you write on:

        TextView TitleText = (TextView) Findviewbyid (Android. R.id.title);        Titletext.settext ("Testtitle");

You'll get a nullpointer. Because Android. R.id.title This Layout has been overwritten.

if the use of Dialog, in the structure, can be directly written setcontentview (R.layout.cutomized_dialog); Without Layoutinflater.

In the onCreate words, is also the same, directly write Setcontentview (R.layout.cutomized_dialog);

In other words, if you are using Dialog, there is no difference between the two methods of writing. will use the default Dialog style. If you want to modify the style, you need to give Dialog a custom style.

I used to create a Dialog with a transparent background, but no matter how I changed style, I didn't succeed. The final solution is to use Alertdialog and then use your own Layout in the OnCreate method, and set its background to transparent.

Dialog transparency effect succeeded. The solution is to set the background of the Dialog to a transparent picture. (There is an article for reference, http://blog.csdn.net/sodino/article/details/5822147)

< Item name = "Android:windowbackground" >@drawable/a_transparent_image</item>

See the following XML file for details.

3. Some parameters
GetWindow (). Setdimamount (0);

This function is used to set the color around the Dialog. The system defaults to a translucent gray. The value is set to 0 for full transparency.

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stylename= "Dialog"Parent= "@android: Style/theme.dialog">    <!--Border -    <Itemname= "Android:windowframe">@null</Item>    <!--is it on the activity ? -    <Itemname= "Android:windowisfloating">True</Item>    <!--Translucent -    <Itemname= "Android:windowistranslucent">False</Item>    <!--No title -    <Itemname= "Android:windownotitle">True</Item>    <!--background Transparency This method does not make -        <Itemname= "Android:windowbackground">@color/transparent</Item>    <!--The correct method of transparent background -        <Itemname= "Android:windowbackground">@drawable/a_transparent_image</Item>    <!--Blur -           <Itemname= "android:backgrounddimenabled">False</Item>    </style></Resources><Itemname= "Android:windowbackground">@color/transparent</Item>

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.