弹出Android应用程序

时间:2012-08-22 08:33:38

标签: android image popup

我有一个弹出窗口,在单击按钮时显示图像。我必须显示弹出窗口,但图像太小,我希望它占据屏幕的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;
                    }


             }
             });

}

这是显示:

enter image description here

任何想法?

3 个答案:

答案 0 :(得分: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);   

答案 1 :(得分: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);

答案 2 :(得分:0)

我会这样做:

height = (int)(0.9*getWindow().getWindowManager().getDefaultDisplay().getHeight());
params.height = height;