Popup弹出自定义布局对话框


TestActivity2.java


package df.util.test;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import df.android.util.R;

/**
 * Created with IntelliJ IDEA.
 * User: pdh
 * Date: 13-4-15
 * Time: 下午4:03
 * To change this template use File | Settings | File Templates.
 */
public class TestActivity2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.

        final Activity act = this;
        Button btn = new Button(act);
        btn.setText("测试");
        btn.setTextSize(30.0f);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //To change body of implemented methods use File | Settings | File Templates.
//                payDianxin();
                Toast.makeText(act,"hfeihfwoe",Toast.LENGTH_LONG).show();
            }
        });

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        RelativeLayout lay = new RelativeLayout(this);
        lay.setBackgroundResource(R.drawable.enjoyit_dialog_bg);
        lay.addView(btn);
        setContentView(lay, params);

        (new Handler()).postDelayed(new Runnable() {
            @Override
            public void run() {
                //To change body of implemented methods use File | Settings | File Templates.
                checkFeePrompt(act);
            }
        }, 100);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_DOWN: {
                break;
            }
            default: {
                break;
            }

        }
        return super.onKeyDown(keyCode, event);    //To change body of overridden methods use File | Settings | File Templates.
    }

    //todo: wulong 计费SDK换了
    public static void checkFeePrompt(final Activity act) {
        int v_id = 124;

        FrameLayout.LayoutParams scrParam = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
        scrParam.setMargins(0, 0, 0, 0);
        ScrollView scrollView = new ScrollView(act);
        scrollView.setId(++v_id);
        scrollView.setBackgroundColor(Color.BLUE);//Color.TRANSPARENT);
        scrollView.setVerticalScrollBarEnabled(true);
        scrollView.setScrollbarFadingEnabled(false);
        scrollView.setLayoutParams(scrParam);


        FrameLayout.LayoutParams relativeParames = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        RelativeLayout relativeLayout = new RelativeLayout(act);
        relativeLayout.setId(++v_id);
        relativeLayout.setBackgroundColor(Color.rgb(32, 32, 32));
        relativeParames.setMargins(30,20,30,20);
        scrollView.addView(relativeLayout, relativeParames);

        RelativeLayout.LayoutParams vParames = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        vParames.addRule(RelativeLayout.ALIGN_TOP);
        vParames.setMargins(0, 0, 0, 30);

        final TextView contentText = new TextView(act);
        contentText.setId(++v_id);
        contentText.setGravity(Gravity.LEFT);
        contentText.setText("测试一下.");
        contentText.setTextColor(Color.BLACK);
        contentText.setTextSize(20.0f);
        contentText.setPadding(0, 0, 0, 0);
        contentText.setBackgroundColor(Color.TRANSPARENT);
        contentText.setTextColor(Color.WHITE);
        contentText.setGravity(Gravity.CENTER);
        relativeLayout.addView(contentText, vParames);

        int btnBgColor = Color.WHITE;//Color.rgb(213, 213, 213);//Color.rgb(157, 206, 255);
        int btnTextColor = Color.BLACK;
        float btnTextSize = 20.0f;
        String btnOkStr = "确定";

        Typeface btnTextType = Typeface.DEFAULT;

        vParames = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        vParames.setMargins(5, 0, 0, 5);
        vParames.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        vParames.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        vParames.addRule(RelativeLayout.BELOW, contentText.getId());

        final Button okBtn = new Button(act);
        okBtn.setId(++v_id);
        okBtn.setText(btnOkStr);
        okBtn.setTextSize(btnTextSize);
        okBtn.setTypeface(btnTextType);
        okBtn.setTextColor(btnTextColor);
        okBtn.setBackgroundColor(btnBgColor);
        okBtn.setPadding(5, 5, 5, 5);

        relativeLayout.addView(okBtn, vParames);


        vParames = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        vParames.setMargins(0, 0, 5, 5);
        vParames.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        vParames.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        vParames.addRule(RelativeLayout.BELOW, contentText.getId());

        final Button cancelBtn = new Button(act);
        cancelBtn.setId(++v_id);
        cancelBtn.setText("取消");
        cancelBtn.setTextSize(btnTextSize);
        cancelBtn.setTypeface(btnTextType);
        cancelBtn.setTextColor(btnTextColor);
        cancelBtn.setBackgroundColor(btnBgColor);
        cancelBtn.setPadding(5, 5, 5, 5);
        relativeLayout.addView(cancelBtn, vParames);

        scrollView.setFocusable(true);
        scrollView.setFocusableInTouchMode(true);

        final PopupWindow pw = new PopupWindow(scrollView,ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT,
                true);//创建PopupWindow实例

//        pw.setBackgroundDrawable(new BitmapDrawable());//删掉这句,否则点击周围空白区域也会关闭对话框
        pw.setOutsideTouchable(true);
        pw.setFocusable(true);//需要设置为ture,表示可以聚焦

        pw.update();
        scrollView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {

                if (KeyEvent.KEYCODE_BACK == i) {
                    return true;
                }
                return false;  //To change body of implemented methods use File | Settings | File Templates.
            }
        });

        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //To change body of implemented methods use File | Settings | File Templates.
                pw.dismiss();
            }
        });

        pw.showAtLocation(act.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }
}



效果图这样的:



你可能感兴趣的:(Popup弹出自定义布局对话框)