这次我们自定义了一个PopupWindow,更深刻的理解此控件的用法,之后的使用都可以按这个模板.
public class BarCommunitySelectPopWindow extends PopupWindow {
public BarCommunitySelectPopWindow(Context context, final OnclickSelectListener listener) {
super(context);
View view = View.inflate(context, R.layout.view_pop_bar_community, null);
this.setContentView(view);
LinearLayout ll_add_community = (LinearLayout) view.findViewById(R.id.ll_add_community);
LinearLayout ll_add_bar_stock = (LinearLayout) view.findViewById(R.id.ll_add_bar_stock);
ll_add_community.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null) {
listener.addCommunity();
BarCommunitySelectPopWindow.this.dismiss();
}
}
});
ll_add_bar_stock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null) {
listener.addStockBar();
BarCommunitySelectPopWindow.this.dismiss();
}
}
});
this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
this.setHeight(AndroidUtil.dip2px(context, 90));
this.setFocusable(true);
ColorDrawable dw = new ColorDrawable(0x80000000);
this.setBackgroundDrawable(dw);
this.setOutsideTouchable(true);
}
public interface OnclickSelectListener {
void addCommunity();
void addStockBar();
}
}
有个点需要注意的就是
this.setBackgroundDrawable(dw);
一定要设置这个,如果不设置会有一些莫名其妙的错误。