Android-PopupWindow弹窗、对话框详解

2021-09-24 14:41:26 浏览数 (1)

简介:

PopupWindow 刻船舰类似对话框风格的窗口

效果:

使用方法:

使用PopupWindow 创建对话框风格的串口秩序如下两步即可:

1. PopupWindow 的构造器创建PopupWindow对象

2. PopupWindow 的showAsDropDown() 将其显示效果设置为下拉显示

3. PopupWindow 的showAtLoacation() 方法将PopupWindow() 在指定位置显示出来

下拉显示效果:

具体实现方法:

代码语言:javascript复制
public class MainActivity extends Activity {
    private  PopupWindow popupWindow;
    private View root;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        root = this.getLayoutInflater().inflate(R.layout.cell,null);//add cell.xml above you mainActivity window
        popupWindow = new PopupWindow(root,560,700);//create a popupWindow object
        root.findViewById(R.id.button01).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //close the popupWindow
                popupWindow.dismiss();
            }
        });
    }
    public void send(View source){
        //set the location of PopupWindow
        popupWindow.showAtLocation(findViewById(R.id.send),Gravity.CENTER,20,20);//you can remove this effect
        //Use DropDown way to display
        popupWindow.showAsDropDown(root);
    }
}

mainActivity的布局文件:

代码语言:javascript复制

/layout/cell.xml

代码语言:javascript复制

0 人点赞