Android PopupWindow 弹框布局要显示在某个空间下面位置
生活随笔
收集整理的這篇文章主要介紹了
Android PopupWindow 弹框布局要显示在某个空间下面位置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
說明:
彈框布局要顯示在某個空間下面需求。使用PopupWindow 彈框控件。
代碼:
使用PopupWindow。
1、PopupWindow初始化
private PopupWindow popupWindow;//初始化搜索popup (可以啟動初始化)private void initSearchPopupWindow() {LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);final View contentview = inflater.inflate(R.layout.popup_search_varieties_select, null);//自己的彈框布局final EditText et_search = (EditText) contentview.findViewById(R.id.et_search);//布局中的編輯框TextView tv_search = (TextView) contentview.findViewById(R.id.tv_search);//布局中的搜索按鈕contentview.setFocusable(true); // 這個很重要contentview.setFocusableInTouchMode(true);popupWindow = new PopupWindow(contentview, RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT);popupWindow.setFocusable(true);popupWindow.setOutsideTouchable(false);contentview.setOnKeyListener(new View.OnKeyListener() {//監聽系統返回鍵@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {popupWindow.dismiss();return true;}return false;}});}2、點擊某個空間,啟用PopupWindow,制定其顯示位置iv_search.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {popupWindow.showAsDropDown(rl_header);//PopupWindow顯示在rl_header空間下面}});
其它說明:
顯示PopupWindow:
showAsDropDown(View anchor):相對某個控件的位置(正左下方),無偏移
showAsDropDown(View anchor, int xoff, int yoff):相對某個控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相對于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移
總結
以上是生活随笔為你收集整理的Android PopupWindow 弹框布局要显示在某个空间下面位置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html中使用JS实现图片轮播效果
- 下一篇: Android城市列表,首字母排序,右侧