Android开发PopupWindow的使用,PopupWindow 的基本使用,参数介绍,PopupWindow 如何显示

控件.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showPopupWindow(v);
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        int x = location[0]-myPopupWindow1.getWidth()+(map_right_second.getWidth()/2); //把下面第一个0换成x就会显示在控件x方向中间部分       
        int y = location[1]-myPopupWindow1.getHeight()+(map_right_second.getHeight()/2);//把下面第二个0换成y就会显示在控件y方向中间部分
        myPopupWindow2.showAtLocation(v, Gravity.CENTER, 0, 0);//(控件,相对屏幕的位置,距离控件x方向的距离,距离控件y方向的距离)
    }
});
//popwindow
private PopupWindow myPopupWindow;
private View infoView;
public void showPopupWindow(View view){
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;//屏幕宽度
    infoView = LayoutInflater.from(TongjiActivity.this).inflate(R.layout.popwindow_view,null);//布局文件
    myPopupWindow = new PopupWindow(infoView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);//(布局文件(View),popwindow宽,popwindow高,指定PopupWindow能否获得焦点)
    myPopupWindow.setContentView(infoView);
    myPopupWindow.setBackgroundDrawable(getDrawable());//设置背景透明以便点击外部消失
    myPopupWindow.setOutsideTouchable(true);//点击外部收起
    myPopupWindow.setFocusable(true);//设置焦点生效
    myPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {

        }
    });
    final TimePicker mTimePicker = (TimePicker) infoView.findViewById(R.id.time_picker);
    final DatePicker mDatePicker = (DatePicker) infoView.findViewById(R.id.date_picker);
    mTimePicker.setIs24HourView(true);
    final Button tv_sure_list_datecheck = (Button) infoView.findViewById(R.id.btn_ok);
    tv_sure_list_datecheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            
        }
    });
// 显示PopupWindow,其中:
// 第一个参数是PopupWindow的锚点,第二和第三个参数分别是PopupWindow相对锚点的x、y偏移
//window.showAsDropDown(anchor, xoff, yoff);
}

//背景

private Drawable getDrawable(){
    ShapeDrawable bgdrawable =new ShapeDrawable(new OvalShape());
    bgdrawable.getPaint().setColor(TongjiActivity.this.getResources().getColor(R.color.color_half_translate));
    return bgdrawable;
}

//背景颜色

<color name="color_half_translate">#90000000</color>

猜你喜欢

转载自blog.csdn.net/congcongguniang/article/details/85065124