码迷,mamicode.com
首页 > Windows程序 > 详细

自定义popupWindow

时间:2015-02-10 18:18:05      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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);

}

自定义popupWindow

标签:

原文地址:http://www.cnblogs.com/ponos/p/4284507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!