關於Android Popwindow的使用

首先再剛開始的寫的時候佈局出現一些錯誤,明明再佈局中寫的是“”android:layout_width="match_parent"
但是頁面中還是顯示在正中央,左右倆邊還是會出現問題,困擾很久,才解決,是因爲我們如果使用佈局中的`這個根佈局<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“vertical”

>肯定是不行的,所以我們需要在給他寫一個子佈局的一個LinearLayout或者其他的

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"

    >
  <ImageView
      android:id="@+id/iamge_fenxiang"
      android:layout_width="match_parent"
      android:layout_height="@dimen/dp_300"
      android:background="@drawable/fenxiang"
      ></ImageView>
</LinearLayout>

`
//我寫的是pop從底部彈出

 backgroundAlpha(0.5f);//讓背景變暗
                    // 設置佈局文件
                    final PopupWindow mPopupWindow = new PopupWindow(DynamicContentActivity.this);
                    View view = LayoutInflater.from(DynamicContentActivity.this).inflate(R.layout.aleart_fenxiang, null);
                    ImageView iamge_fenxiang = view.findViewById(R.id.iamge_fenxiang);
                    iamge_fenxiang.setScaleType(ImageView.ScaleType.FIT_XY);
                    mPopupWindow.setContentView(view);
                    //設置彈出和消失的動畫
                    mPopupWindow.setAnimationStyle(R.style.showPopupAnimation);

                    // 爲了避免部分機型不顯示,我們需要重新設置一下寬高
                    mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                    mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                    mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000000)); // 設置pop透明效果
                    mPopupWindow.setFocusable(true);// 設置pop獲取焦點,如果爲false點擊返回按鈕會退出當前Activity,如果pop中有Editor的話,focusable必須要爲true
                    mPopupWindow.setTouchable(true); // 設置pop可點擊,爲false點擊事件無效,默認爲true
                    mPopupWindow.setOutsideTouchable(false);// 設置點擊pop外側消失,默認爲false;在focusable爲true時點擊外側始終消失
                    mPopupWindow.showAtLocation(view, Gravity.BOTTOM, Gravity.LEFT, Gravity.RIGHT);
                    //背景變暗消失監聽
                    mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            backgroundAlpha(1);
                        }
                    });
                     // 點擊窗體內其他地方消失
        popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (popupWindow != null && popupWindow.isShowing()) {
                    backgroundAlpha(1);
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                return false;
            }
        });

                }```

  //pop背景變暗
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
         lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }```
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章