Android custom dialog and the ability to lose focus (background transparency)

Source: Internet
Author: User

Preface: Due to the need to update the display animation in the project needs, so think of dialog, custom dialog not difficult, many online tutorials, but in the realization of dialog background transparent requirements, encountered a little problem, some online methods on my machine did not realize, Only the twists and turns found another way to implement. It was a bit of a hassle, but it worked well.

This method is written here, one is to share with you, the second is to make a record, for the future needs.

Don't say, on the code:

Here is the dialog custom layout file, which is used for the task, refer to.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Horizontal"     >    <textview        android:layout_height= "wrap_content"        android:layout_ Width= "Wrap_content"        android:text= "in execution"        android:textsize= "18sp"        />    <imageview         Android:id= "@+id/dialog_image"        android:layout_height= "wrap_content"        android:layout_width= "Wrap_ Content "        android:layout_gravity=" center "        />    </LinearLayout>


Custom dialog, the code that executes the task is removed and can be added to the OnCreate ().

/** * Execution Status dialog box form * @author Administrator * */class Mydialog extends Dialog{imageview imageview;public mydialog (Context con Text) {super (context);//TODO auto-generated Constructor stub}public mydialog (Context Context,int theme) {Super (context , theme);//Todo auto-generated constructor stub} @Overrideprotected void OnCreate (Bundle savedinstancestate) {//Todo Aut O-generated method Stubsuper.oncreate (savedinstancestate);//Remove Activity title Requestwindowfeature (window.feature_no_ TITLE); Setcontentview (r.layout.dialog);        Set Title ImageView = (ImageView) Findviewbyid (r.id.dialog_image);}}

Before the implementation, we experimented with the method of dialog setting flag, some say yes, but I didn't realize it. Also leave the code for reference:

Cause dialog to lose focus Dialog.getwindow (). SetFlags (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);d Ialog.setcanceledontouchoutside (true);

The true approach is implemented using a style, which requires two resource files, a color, and a style.

<color name= "Transparent" > #00000000 </color>

<style name= "Dialog" parent= "@android: Style/theme.dialog" >        <item name= "Android:windowframe" > @null </item><!--Border--        <item name= "android:windowisfloating" >true</item><!-- Whether it appears on activity--        <item name= "android:windowistranslucent" >false</item><!--Translucent--        <item name= "Android:windownotitle" >true</item><!--Untitled--        <item name= "Android: Windowbackground "> @color/transparent</item><!--Background Transparent--        <item name=" Android: Backgrounddimenabled ">false</item><!--Blur--    </style>

After you have set up the resource file, generate the instance code on the call as follows:

Mydialog dialog = new Mydialog (this,r.style.dialog);d ialog.show ();

This is over.


Reference: http://www.cnblogs.com/windlivefamily/articles/2133956.html


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android custom dialog and the ability to lose focus (background transparency)

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.