android的popupwindow常用形式
一般android用到的pop弹窗
直接上代码
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
/**
* Created by xieshengqi on 16/11/12.
*/
public class PopListViewWindow extends PopupWindow {
private ListView listView;
private TextView textView;
private ImageView close_image;
public PopListViewWindow(Context context) {
super(context);
initView(context);
}
public PopListViewWindow(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public PopListViewWindow(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(final Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.pop_listview_layout, null);
listView = (ListView) view.findViewById(R.id.pop_listview_listView);
textView = (TextView) view.findViewById(R.id.pop_listView_title_textView);
close_image = (ImageView) view.findViewById(R.id.pop_addqingdan_cancle);
LinearLayout.LayoutParams pa = (LinearLayout.LayoutParams) listView.getLayoutParams();
pa.height = DisplayUtil.getScreenHeight() / 2;
listView.setLayoutParams(pa);
setContentView(view);
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
// 实例化一个ColorDrawable颜色为半透明
final ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);
// 设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
this.setOutsideTouchable(true);
close_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
点击pop窗口之外的地方隐藏窗口可以实现监听
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = view.findViewById(
R.id.pop_choosecuoti_uselayout).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y > height) {
dismiss();
}
}
return true;
}
});
height为窗口实际的控件高度。
这是一般常用的一个pop弹窗