PopupWindow

来源:互联网 发布:网络对拟态环境的冲击 编辑:程序博客网 时间:2024/05/29 14:50
public void initPopup(Context context) {        PopupWindow  window = new PopupWindow(context);        // 此处的width和height获取dimen.xml下大小,单位为dp,用于适配        int width = (int) getResources().getDimension(R.dimen.sms_part_search_popup_width);        int height = (int) getResources().getDimension(R.dimen.sms_part_search_popup_height);        window.setWidth(width);        window.setHeight(height);        window.setFocusable(false);        window.setBackgroundDrawable(new BitmapDrawable());        window.setOutsideTouchable(true);        window.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);        View view = LayoutInflater.from(context).inflate(R.layout.l_sms_popupwindow, null);        mListView = (ListView) view.findViewById(R.id.l_sms_popup_lv);        mListView.setOnItemClickListener(this);        window.setContentView(view);    }

// 显示
int x = (int) getResources().getDimension(R.dimen.sms_part_search_popup_x_off);
window.showAsDropDown(mText, x, 0);
// 消失
window.dismiss();

关于PopupWindow里面放置ListView无法获取焦点的解决办法:
自定义CustomListView如下:重写hasWindowFocus()方法,并return true;

public class CustomListView extends ListView {    public CustomListView(Context context) {        super(context);    }    public CustomListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    public CustomListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);    }    @Override    public boolean hasWindowFocus() {        return true;    }}
0 0
原创粉丝点击