http://blog.csdn.net/sxsboat/article/details/7340759
Android in OnCreate () pop-up Popwindow, many people have similar needs, but directly in OnCreate () call Popwindow showatlocation () method is reported abnormal, The reason is that the activity has not been initialized at this time, there are some solutions on the Internet, but generally through a short period of time to pop up the implementation, code is not strong enough. In fact, you can constantly detect the state of the current activity, once the initialization is complete immediately call Popwindow's Showatlocation () method display. The detailed code is as follows:
[Java]View Plaincopy
- /************************************************************************
- *
- * This class is used to pop up a popwindow at initialization time, the file used is as follows, especially to note that the
- * android:minheight= "1DP" and Android:minwidth= "1DP" properties. The key to ejecting Popwindow when initializing is
- * Wait for the entire activity to initialize before calling the Showatlocation () method, otherwise it will be out of the ordinary.
- * The implementation of the method is to constantly detect whether the acitvity is initialized, once the completion of the call
- * Showatlocation () method shows Popwindow.
- *
- * The following are the layout files:
- * <?xml version= "1.0" encoding= "Utf-8"?>
- * <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
- * android:id= "@+id/main"
- * android:layout_width= "Fill_parent"
- * android:layout_height= "Fill_parent"
- * android:minheight= "1DP"
- * android:minwidth= "1DP"
- * android:orientation= "Vertical" >
- * </LinearLayout>
- *
- * Stone
- * Completion time: 2012/3/10
- * Version: 1.0
- *
- ************************************************************************/
- Package CN.SD.SX.INITPOPW;
- Import android.app.Activity;
- Import Android.graphics.Color;
- Import Android.os.Bundle;
- Import Android.os.Handler;
- Import android.view.Gravity;
- Import Android.view.View;
- Import Android.view.ViewGroup.LayoutParams;
- Import Android.widget.LinearLayout;
- Import Android.widget.PopupWindow;
- Import Android.widget.TextView;
- Import CN.SD.SX.POPW.R;
- Public class Popwactivity extends Activity {
- private Popupwindow Mpopupwindow;
- private Handler Mhandler;
- //Detection time interval
- private int detchtime = 5;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Mhandler = new Handler ();
- //Display Popwindow
- Showpopwindow ();
- }
- private void Showpopwindow ()
- {
- LinearLayout view = new LinearLayout (this);
- Layoutparams params = new Layoutparams (Layoutparams.fill_parent,
- Layoutparams.fill_parent);
- TextView txt1 = new TextView (this);
- Txt1.setlayoutparams (params);
- Txt1.setgravity (Gravity.center);
- Txt1.settext ("txt1");
- Txt1.setbackgroundcolor (color.red);
- View.addview (TXT1);
- Mpopupwindow = New Popupwindow (view, 300);
- Mpopupwindow.setoutsidetouchable (true);
- /***************** The following code is used to loop to detect if activity is initialized ***************/
- Runnable showpopwindowrunnable = new Runnable () {
- @Override
- public Void Run () {
- //Get the root element in activity
- View view = Findviewbyid (R.id.main);
- //How the width and height of the root element is greater than 0 indicates that activity has been initialized
- if (view! = null && view.getwidth () > 0 && view.getheight () > 0) { /c2>
- //Display Popwindow
- Mpopupwindow.showatlocation (popwactivity. This.findviewbyid (R.id.main),
- Gravity.center, 0, 0);
- //Stop detection
- Mhandler.removecallbacks (this);
- } Else {
- //If activity is not initialized, wait 5 milliseconds for re-detection
- Mhandler.postdelayed (this, detchtime);
- }
- }
- };
- //Start detection
- Mhandler.post (showpopwindowrunnable);
- /****************** The above code is used to loop to detect if activity is initialized *************/
- }
- }
How Android pops up Popwindow at initialization time