POPUP窗口显示在android中

时间:2014-08-30 11:11:33

标签: android android-popupwindow

我已经在android中创建了一个按钮,当点击它时会显示弹出窗口..但代码不能正常工作..它没有错误但没有显示弹出窗口...请帮助..我的代码是 公共类MainActivity扩展了Activity {

@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 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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;
    }

答案 2 :(得分:0)

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

  • 您已指定WRAP_CONTENT但尚未指定其内容
  • 将drawable作为动画样式传递给setAnimationStyle方法。

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

答案 3 :(得分:0)

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