0

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

@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);
        }
    });

}
4

4 回答 4

0

你试过这个:objPopupWindow.showAsDropDown(popupButton, 0, 0);

或试试这个http://rajeshandroiddeveloper.blogspot.in/2013/07/android-popupwindow-example-in-listview.html

于 2014-08-30T11:30:22.923 回答
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;
    }
于 2014-08-30T11:51:27.210 回答
0

我在你的代码中发现了一些奇怪的东西

  • 您已指定 WRAP_CONTENT 但根本没有指定其内容
  • 将 drawable 作为动画样式传递给该setAnimationStyle方法。

在我看来,如果您指定了有效的动画样式和内容视图,它应该会出现。

于 2014-08-30T14:05:58.793 回答
0

我认为您在OnClickListener中错过了这段代码 objPopupWindow.setContentView(objrl);

于 2016-01-02T11:16:03.923 回答