1. 程式人生 > >Android彈出視窗

Android彈出視窗

protected void PopUp()
    {
        final PopupWindow popup = new PopupWindow(TestActivity.this);

        View popView = LayoutInflater.from(TestActivity.this).inflate(R.layout.activity_show1, null);

        popup.setContentView(popView);

        popup.setFocusable(true);

        DisplayMetrics metric 
= new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); int width = metric.widthPixels; // 螢幕寬度(畫素) int height = metric.heightPixels; // 螢幕高度(畫素) popup.setWidth(Integer.parseInt(new DecimalFormat("0").format(width * 0.8))); popup.setHeight(Integer.parseInt(
new DecimalFormat("0").format(height * 0.8))); popup.showAtLocation(TestActivity.this.findViewById(R.id.textViewSync), Gravity.CENTER, 0, 0); popView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popup.dismiss(); } }); }