2014-08-30 115 views
0

我在android中创建了一个按钮,当点击它时会显示弹出窗口..但是代码不会像那样工作..它没有错误,但不显示弹出窗口...请helpme。 。这里是我的代码 公共类MainActivity扩展活动{在android中弹出窗口弹出窗口

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final RelativeLayout objrl = (RelativeLayout) findViewById(R.id.myrl);  
    final Button objButton = (Button) findViewById(R.id.mybutton); 
    objButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      PopupWindow objPopupWindow = new PopupWindow(objrl, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);     
      objPopupWindow.setAnimationStyle(R.drawable.background2); 
      objPopupWindow.showAtLocation(objButton, Gravity.CENTER_HORIZONTAL, 10, 10); 
     } 
    }); 

} 
+0

入住这Andwer http://stackoverflow.com/a/18867300/1160282 – SilentKiller 2014-08-30 11:21:07

+0

哦.. !!我不明白你为什么挑剔物体......?解释 – FaKe 2014-08-30 11:21:19

+0

我只是格式化了你的代码。 – SilentKiller 2014-08-30 11:22:14

回答

0
PopupWindow popupWindowDogs = popupWindowDogs(); 

called below function where they want ::- 





public PopupWindow popupWindowDogs() { 

     // initialize a pop up window type 
     PopupWindow popupWindow = new PopupWindow(this); 

     // the drop down list is a list view 
     ListView listViewDogs = new ListView(this); 

     // set our adapter and pass our pop up window contents 
     listViewDogs.setAdapter(dogsAdapter(popUpContents)); 

     // set the item click listener 
     listViewDogs.setOnItemClickListener(new DogsDropdownOnItemClickListener()); 

     // some other visual settings 
     popupWindow.setFocusable(true); 
     popupWindow.setWidth(250); 
     popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 

     // set the list view as pop up window content 
     popupWindow.setContentView(listViewDogs); 

     return popupWindow; 
    } 
+0

我只想显示一个弹出窗口,点击一个按钮...请帮助 – FaKe 2014-08-30 12:13:22

0

我已经发现了一些奇怪的东西,在你的代码

  • 你指定WRAP_CONTENT,但是没有指定其在所有
  • 内容传递一个绘制的动画风格的setAnimationStyle方法。

在我看来,如果你指定一个有效的动画风格和内容视图,它应该会出现。

+0

我已经添加了drawable已经......但没有显示.. :-( – FaKe 2014-09-01 05:03:02

+0

@YurekaMathammalRajendran你不应该向该方法传递一个drawable,还应该用'setContentView'为它指定一个内容视图。 – 2014-09-01 05:18:19

0

我想你错过了OnClickListener这里面的代码 objPopupWindow.setContentView(objrl);