opupwindow 在打开activity的时候直接显示出来

因为在onCreate()中加入popupwindow的代码想要在activity一被打开的时候就弹出popupwindow是会报错的,经过实践可以这样儿做:
@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
                // popupwindow的代码
		showPopupWindow(); 
	
	}

public void showPopupWindow() {
		layout = View.inflate(this, R.layout.widget_main, null);
		popupWindow = new PopupWindow(layout,	LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
		popupWindow.setAnimationStyle(R.style.PopupAnimation);
		// 下面两句使back键可以使用
		ColorDrawable dw = new ColorDrawable(-00000);
		popupWindow.setBackgroundDrawable(dw);
		View view = findViewById(R.id.layout_main);
		popupWindow.showAtLocation(view,	Gravity.CENTER | Gravity.CENTER, 0, 0);
		popupWindow.update();
	}

还有一种是我没有实践过别人那里看的:

private Handler mHandler = new Handler();  
  
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    // TODO Auto-generated method stub  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
    mHandler.postDelayed(mRunnable, 500);  
}  
      
private Runnable mRunnable = new Runnable() {  
    public void run() {  
    // 弹出PopupWindow的具体代码  
    }  
}; 



你可能感兴趣的:(opupwindow 在打开activity的时候直接显示出来)