0

我有一个弹出窗口,在单击按钮时显示图像。我必须显示弹出窗口,但图像太小,我希望它占据屏幕的 90%,并为其添加一个关闭按钮。

这是我的代码:

PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
ImageView imageView;
boolean click = true;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_bmi);

    popUp = new PopupWindow(this);
    layout = new LinearLayout(this);
    tv = new TextView(this);

    imageView = new ImageView(this);

    imageView.setImageResource(R.drawable.animalbite);

    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int fullScreenWidth = display.getWidth();

    int dialogWidth = (int) (fullScreenWidth * 0.9);
    WindowManager.LayoutParams params = getWindow().getAttributes();
    params.height = LayoutParams.WRAP_CONTENT;
    params.width = dialogWidth;
    getWindow().setAttributes(params);

    imageView.setLayoutParams(params);
    //tv.setText("Hi this is a sample text for popup window");
    layout.addView(imageView, params);
    popUp.setContentView(layout);

      Button ViewBmi = (Button) findViewById(R.id.ViewBmi);

      ViewBmi.setOnClickListener(new View.OnClickListener() {

             public void onClick(View v) {

                  if (click) {
                        popUp.showAtLocation(layout, Gravity.CENTER, 10 ,10);
                        click = false;
                    } else {
                        popUp.dismiss();
                        click = true;
                    }


             }
             });

}

这是显示:

在此处输入图像描述

有任何想法吗?

4

3 回答 3

2

试试这个...

PopupWindow popUp;
    LinearLayout layout;
    TextView tv;
    LayoutParams params,params2;
    ImageView imageView;
    boolean click = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Display display = getWindowManager().getDefaultDisplay();
        final int height = (display.getHeight()*90)/100;
        final int width = (display.getWidth()*90)/100;

        popUp = new PopupWindow(this);
        layout = new LinearLayout(this);
        tv = new TextView(this);

        imageView = new ImageView(this);
        imageView.setImageResource(R.drawable.ic_launcher);
        params = new LayoutParams(width,height-50);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(imageView, params);

        Button button = new Button(this);
        button.setText("Close");
        params2 = new LayoutParams(100,50);
        layout.addView(button,params2);   


        popUp.setContentView(layout);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                 popUp.dismiss();
                 click = true;
            }
        });

          Button ViewBmi = (Button) findViewById(R.id.button1);

          ViewBmi.setOnClickListener(new View.OnClickListener() {

                 public void onClick(View v) {

                      if (click) {
                            popUp.showAtLocation(layout, Gravity.CENTER, 10, 10);
                            popUp.update(10, 20, width,height);
                            click = false;
                        } else {
                            popUp.dismiss();
                            click = true;
                        }


                 }
                 });

    }

试试这个....

Button button = new Button(this);
        button.setText("Close");
        params2 = new LayoutParams(100,50);
    --->params2.setMargins(100, 0, 0, 0);
        layout.addView(button,params2);   
于 2012-08-22T09:25:27.177 回答
0

尝试:

params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
...
layout.addView(imageView, params);

这样 ImageView 将尽可能宽。

如果底层对话框不够宽,那么你可以设置它的宽度也全屏:

WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes(params);

或者,如果您希望它占据屏幕的 90%,那么:

// get the width of the whole screen
Display display = getWindow().getDefaultDisplay();
int fullScreenWidth = display.getWidth();


//then set 90% of it to the width of the Dialog:
int dialogWidth = (int) fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);
于 2012-08-22T08:37:39.987 回答
0

我会这样做:

height = (int)(0.9*getWindow().getWindowManager().getDefaultDisplay().getHeight());
params.height = height;
于 2012-08-22T09:15:20.287 回答