常用的PopupWindow(1)

来源:互联网 发布:淘宝联盟官网登录网址 编辑:程序博客网 时间:2024/06/11 20:27
PopupWindow类:
public class PromptWindowView implements View.OnClickListener, PopupWindow.OnDismissListener {    private static final int popTypeLogin = 1;//1表示没有登录时的引导提示    private static final int popTypeKnow = 2;//2表示没有开通社区时的引导提示    private Activity mActivity;    private PopupWindow popupwindow = null;    private View view = null;    private Button mBtnLogin, mBtnRegister, mBtnKnow;    public PromptWindowView(Activity activity) {        this.mActivity = activity;        initPopWindowView();    }    public void initPopWindowView() {        view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_have_no_login, null, false);        //1.构造一个PopupWindow,参数依次是加载的View,宽高        LinearLayout layout = new LinearLayout(mActivity);        layout.setGravity(Gravity.CENTER);        int width = ScreenUtils.dip2px(mActivity, 310);        int higth = ScreenUtils.dip2px(mActivity, 330);        layout.addView(view, new LinearLayout.LayoutParams(width, higth));        L.e("tag", "弹出窗口的宽度:高度为" + width + "-->" + higth);        //        layout.addView(view, new LinearLayout.LayoutParams(910, 975));        popupwindow = new PopupWindow(layout,                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);        /*popupwindow = new PopupWindow(layout, 1120,                1200);*/        /*popupwindow = new PopupWindow(layout,                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);*/        popupwindow.setAnimationStyle(R.anim.pop_prompt);  //设置加载动画        //这些为了点击非PopupWindow区域,PopupWindow会消失的,如果没有下面的        //代码的话,你会发现,当你把PopupWindow显示出来了,无论你按多少次后退键        //PopupWindow并不会关闭,而且退不出程序,加上下述代码可以解决这个问题        popupwindow.setOutsideTouchable(true);        popupwindow.setTouchable(true);        popupwindow.setTouchInterceptor(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                return false;                // 这里如果返回true的话,touch事件将被拦截                // 拦截后 PopupWindowonTouchEvent不被调用,这样点击外部区域无法dismiss            }        });        popupwindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效        //设置popupWindow里的按钮的事件        mBtnLogin = (Button) view.findViewById(R.id.btn_login);        mBtnRegister = (Button) view.findViewById(R.id.btn_register);        mBtnKnow = (Button) view.findViewById(R.id.btn_know);        mBtnLogin.setOnClickListener(this);        mBtnRegister.setOnClickListener(this);        mBtnKnow.setOnClickListener(this);        popupwindow.setOnDismissListener(this);    }    public void showPromptPop(final int popType) {        if (popType == popTypeLogin) {            view.setBackgroundResource(R.drawable.ic_bg_have_no_login);            mBtnLogin.setVisibility(View.VISIBLE);            mBtnRegister.setVisibility(View.VISIBLE);            mBtnKnow.setVisibility(View.GONE);        } else if (popType == popTypeKnow) {            view.setBackgroundResource(R.drawable.ic_bg_have_no_community);            mBtnLogin.setVisibility(View.GONE);            mBtnRegister.setVisibility(View.GONE);            mBtnKnow.setVisibility(View.VISIBLE);        }        //设置popupWindow显示的位置,参数依次是参照Viewx轴的偏移量,y轴的偏移量        popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);        //设置背景颜色变暗        Window window = mActivity.getWindow();        WindowManager.LayoutParams lp = window.getAttributes();        lp.alpha = 0.5f;        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug        window.setAttributes(lp);    }    //    btn_login);    //    id.btn_register);    //    Id(R.id.btn_open_community);    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_login: // 登录                Intent intent = new Intent(mActivity, UserLoginActivity.class);                mActivity.startActivity(intent);                mActivity.overridePendingTransition(R.anim.activity_bottom_in, R.anim.activity_bottom_out);                break;            case R.id.btn_register:  // 注册                Intent registerIntent = new Intent(mActivity, UserRegisterActivity.class);//手机号姓名注册和开通社区服务                mActivity.startActivity(registerIntent);                break;            case R.id.btn_open_community: // 我知道了                popupwindow.dismiss();                break;            default:                break;        }        if (null != popupwindow) {            popupwindow.dismiss();        }    }    @Override    public void onDismiss() {        //获取所依赖的activity,将其设置为不是阴影        final Window window = mActivity.getWindow();        WindowManager.LayoutParams params = window.getAttributes();        params.alpha = 1f;        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug        window.setAttributes(params);    }    public void dismiss() {        popupwindow.dismiss();    }}
布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="25dp"                android:layout_marginRight="25dp"                android:layout_gravity="center"                android:id="@+id/layout_have_no_login"                android:background="@drawable/ic_bg_have_no_login">       <LinearLayout        android:orientation="horizontal"        android:layout_marginTop="250dp"        android:layout_marginLeft="18dp"        android:layout_marginRight="18dp"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:id="@+id/btn_login"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="53dp"            android:layout_marginRight="9dp"            android:textSize="16sp"            android:textStyle="bold"            android:textColor="@drawable/btn_dialog_bottom_text_selector"            android:background="@drawable/btn_dialog_bottom_selector_login"            />        <Button            android:id="@+id/btn_register"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="53dp"            android:layout_marginLeft="9dp"            android:textSize="16sp"            android:textStyle="bold"            android:textColor="@drawable/btn_dialog_bottom_text_selector"            android:background="@drawable/btn_dialog_bottom_selector_register"            />    </LinearLayout>    <Button        android:id="@+id/btn_know"        android:layout_marginTop="250dp"        android:layout_width="match_parent"        android:layout_height="53dp"        android:layout_marginRight="18dp"        android:layout_marginLeft="18dp"        android:textSize="16sp"        android:textStyle="bold"        android:text="知道了"        android:layout_centerHorizontal="true"        android:textColor="@drawable/btn_dialog_bottom_text_selector"        android:background="@drawable/btn_dialog_bottom_selector"        />   </RelativeLayout>
原创粉丝点击