Android弹出式提示框 PopupWindow

Android 弹出消息提醒——PopupWindow。

public class MainActivity extends Activity {

    private PopupWindow pupWindow;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void showWindow(){
        if(pupWindow == null){
             LayoutInflater la = LayoutInflater.from(MainActivity.this);
             View view = la.inflate(R.layout.pupwindow, null);//自定义布局
             tv = (TextView) view.findViewById(R.pupwindow.txt);
             view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pupWindow.dismiss();
                    }
                });
             pupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        }
        tv.setText("this is pup window!");
        //设置PopupWindow的焦点
        pupWindow.setFocusable(true);
        //点击PopupWindow之外的地方PopupWindow会消失
        pupWindow.setOutsideTouchable(true);
        //showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
        pupWindow.showAtLocation(MainActivity.this.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        pupWindow.update();
    }

}

仅用于记录!

你可能感兴趣的:(Android-开发)