自定义popupWindow

1.方法

private void showPopupWindow(View view, final TypedArray typedArray) {
// TODO Auto-generated method stub
if (popupWindow == null) {
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setBackgroundResource(R.drawable.white_border_bg);
linearLayout.setOrientation(LinearLayout.VERTICAL);
popupWindow = new PopupWindow(linearLayout,
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, false);
// 需要设置一下此参数,点击外边可消失
popupWindow.setBackgroundDrawable(new ColorDrawable(resources
.getColor(android.R.color.white)));
// 设置点击窗口外边窗口消失
popupWindow.setOutsideTouchable(true);
// 设置此参数获得焦点,否则无法点击
popupWindow.setFocusable(true);
}
int dp_30 = (int) resources.getDimension(R.dimen.dp_30);
int dp_10 = (int) resources.getDimension(R.dimen.dp_10);
LinearLayout linearLayout = (LinearLayout) popupWindow.getContentView();
linearLayout.removeAllViews();
for (int i = 0; i < typedArray.length(); i++) {
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setText(typedArray.getResourceId(i, 0));
textView.setPadding(dp_10, 0, 0, 0);
textView.setTag(i);
textView.setBackgroundResource(R.drawable.title_color_select);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, dp_30);
linearLayout.addView(textView, layoutParams);
textView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

if (typedArray.equals(typedArray1)) {
tv_repair_level.setText(typedArray.getResourceId((Integer) v.getTag(), 0));
index_level = (Integer) v.getTag();
repair_level = index_level + "";

} else if(typedArray.equals(typedArray2)) {
tv_repair_type.setText(typedArray.getResourceId((Integer) v.getTag(), 0));
index_work = (Integer) v.getTag();
repair_type = index_work + "";
}

if (popupWindow != null) {
popupWindow.dismiss();
}
}
});
}
popupWindow.showAsDropDown(view);

}

posted @ 2015-02-10 17:57  种花小哥  阅读(299)  评论(0编辑  收藏  举报