久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

解決Android popupWindow設置背景透明度無效的問題

瀏覽:112日期:2022-09-22 18:35:18

有的時候當我們使用popwindow時將當前的activity當做View傳給其他fragment使用時會導致我們設置背景變暗效果失效,導致這個原因的問題是因為我們傳view后當彈出popupwindw的時候已經不再當前的activity了,所以會顯示無效

因此在設置的時候我們這樣處理:

public void backgroundAlpha(float bgAlpha) {//MApp.getInstance().getMainActivity()就是你想要點擊時要顯示的主activity WindowManager.LayoutParams lp = ActivityB.getInstance().getMainActivity().getWindow().getAttributes(); lp.alpha = bgAlpha; //0.0-1.0 ActivityB.getInstance().getMainActivity().getWindow().setAttributes(lp); ActivityB.getInstance().getMainActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); }

即activity A作為View在B中顯示,那么A中的設置透明度時getwindow要從B取,也就是B.getwindow();

補充知識:Android 的PopupWindow的簡單設置及空白區域顏色

我就廢話不多說了,大家還是直接看代碼吧~

//設置空白的背景色WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();lp.alpha = 0.3f;getActivity().getWindow().setAttributes(lp);// 用于PopupWindow的ViewView contentView = LayoutInflater.from(getContext()).inflate(R.layout.quote_info_pop, null, false);// 創建PopupWindow對象,其中:// 第一個參數是用于PopupWindow中的View,第二個參數是PopupWindow的寬度,// 第三個參數是PopupWindow的高度,第四個參數指定PopupWindow能否獲得焦點PopupWindow window = new PopupWindow(contentView, getScreenWith() / 3 * 2, (int) getScreenHeight() / 3 * 2, true);// 設置PopupWindow的背景window.setBackgroundDrawable(getResources().getDrawable(R.drawable.white_shap));// 設置PopupWindow是否能響應外部點擊事件window.setOutsideTouchable(true);// 設置PopupWindow是否能響應點擊事件window.setTouchable(true);// 顯示PopupWindow,其中:// 第一個參數是PopupWindow的錨點,第二和第三個參數分別是PopupWindow相對錨點的x、y偏移listView = contentView.findViewById(R.id.list);quoteInfoAdapter = new QuoteInfoAdapter(getActivity(), mList);listView.setAdapter(quoteInfoAdapter);window.showAsDropDown(v, getScreenWith() / 6, 0);// 或者也可以調用此方法顯示PopupWindow,其中:// 第一個參數是PopupWindow的父View,第二個參數是PopupWindow相對父View的位置,// 第三和第四個參數分別是PopupWindow相對父View的x、y偏移// window.showAtLocation(parent, gravity, x, y);//添加pop窗口關閉事件window.setOnDismissListener(new poponDismissListener());還要加上監聽,否則,pop消失時,透明度不會變回來class poponDismissListener implements PopupWindow.OnDismissListener { @Override public void onDismiss() { // TODO Auto-generated method stub //Log.v('List_noteTypeActivity:', '我是關閉事件'); WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes(); lp.alpha = 1f; //0.0-1.0 getActivity().getWindow().setAttributes(lp); }}

以上這篇解決Android popupWindow設置背景透明度無效的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 都安| 克拉玛依市| 凤庆县| 伊吾县| 高淳县| 墨脱县| 宕昌县| 南江县| 通许县| 孙吴县| 安岳县| 安平县| 西林县| 山阴县| 兴业县| 梁河县| 启东市| 阳谷县| 奎屯市| 伊金霍洛旗| 尖扎县| 司法| 克东县| 三原县| 黎城县| 濮阳县| 诸城市| 喜德县| 通山县| 莱芜市| 池州市| 隆回县| 丹江口市| 太白县| 商河县| 综艺| 镇原县| 连州市| 朔州市| 全州县| 阿图什市|