2014-08-30 113 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);