<strong><span style="font-size:24px;">android自定义弹窗PopupWindow</span></strong>
/**
* 自定义弹窗
* @author Administrator
*
*/
public class MyPopup extends PopupWindow {
private MyLogger mylogger = MyLogger.getLogger();
private Context mContext;
//列表弹窗的间隔
protected final int LIST_PADDING = 10;
//实例化一个矩形
private Rect mRect = new Rect();
//坐标的位置(x、y)
private final int[] mLocation = new int[2];
//判断是否需要添加或更新列表子类项
private boolean mIsDirty;
//位置不在中心
private int popupGravity = Gravity.NO_GRAVITY;
//弹窗子类项选中时的监听
private OnItemOnClickListener mItemOnClickListener;
//定义列表对象
private ListView mListView;
//定义弹窗子类项列表
private ArrayList<BenqunItem> mBenqunItems = new ArrayList<BenqunItem>();
public MyPopup(Context context){
//设置布局的参数
this(context, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
public MyPopup(Context context,int width, int height) {
super(LayoutInflater.from(context).inflate(R.layout.title_popup, null), width, height);//一个listview
this.mContext = context;
//设置可以获得焦点
setFocusable(true);
//设置弹窗内可点击
setTouchable(true);
//设置弹窗外可点击
setOutsideTouchable(true);
//设置弹窗的宽度和高度
setWidth(width);
setHeight(height);
setBackgroundDrawable(new BitmapDrawable());
initUI();
}
/**
* 初始化弹窗列表
*/
private void initUI(){
mListView = (ListView) getContentView().findViewById(R.id.title_list);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int index,long arg3) {
//点击子类项后,弹窗消失
dismiss();
if(mItemOnClickListener != null)
mItemOnClickListener.onItemClick(mBenqunItems.get(index), index);
}
});
}
/**
* 显示弹窗列表界面
*/
public void show(View view){
//获得点击屏幕的位置坐标
view.getLocationOnScreen(mLocation);
//设置矩形的大小
mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(),mLocation[1] + view.getHeight());
//判断是否需要添加或更新列表子类项
if(mIsDirty){
populateActions();
}
int[] location = new int[2];
view.getLocationInWindow(location);
//显示弹窗的位置
showAtLocation(view, popupGravity, location[0] - LIST_PADDING, mRect.bottom);
}
public void showAtCenter(View view){
//获得点击屏幕的位置坐标
view.getLocationOnScreen(mLocation);
//判断是否需要添加或更新列表子类项
if(mIsDirty){
populateActions();
}
int[] location = new int[2];
view.getLocationInWindow(location);
// mylogger.i("弹出框的父view在windows中的位置:x="+location[0]+",y="+location[1]);
showAtLocation(view, Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
}
/**
* 设置弹窗列表子项
*/
private void populateActions(){
mIsDirty = false;
//设置列表的适配器
mListView.setAdapter(new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = null;
if(convertView == null){
textView = new TextView(mContext);
textView.setTextColor(mContext.getResources().getColor(android.R.color.black));
textView.setTextSize(14);
//设置文本居中
textView.setGravity(Gravity.CENTER);
//设置文本域的范围
textView.setPadding(0, DisplayUtil.dip2px(mContext, 15), 0, DisplayUtil.dip2px(mContext, 15));
//设置文本在一行内显示(不换行)
textView.setSingleLine(true);
}else{
textView = (TextView) convertView;
}
BenqunItem item = mBenqunItems.get(position);
//设置文本文字
textView.setText(item.T_Title);
return textView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return mBenqunItems.get(position);
}
@Override
public int getCount() {
return mBenqunItems.size();
}
}) ;
}
/**
* 添加子类项
*/
public void addAction(BenqunItem action){
if(action != null){
mBenqunItems.add(action);
mIsDirty = true;
}
}
/**
* 清除子类项
*/
public void cleanAction(){
if(mBenqunItems.isEmpty()){
mBenqunItems.clear();
mIsDirty = true;
}
}
/**
* 设置数据
* @param items
*/
public void setDatas(ArrayList<BenqunItem> items) {
mBenqunItems = items;
}
/**
* 根据位置得到子类项
*/
public BenqunItem getAction(int position){
if(position < 0 || position > mBenqunItems.size())
return null;
return mBenqunItems.get(position);
}
/**
* 设置监听事件
*/
public void setItemOnClickListener(OnItemOnClickListener onItemOnClickListener){
this.mItemOnClickListener = onItemOnClickListener;
}
/**
*
* 弹窗子类项按钮监听事件
*/
public static interface OnItemOnClickListener {
public void onItemClick(BenqunItem item , int position);
}
}
调用弹窗,比如调取相册和摄像机;
代码:
<pre name="code" class="java">/***
* 相册
*/
popupWin2 = new MenuPopup(self, LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, getResources().getColor(
R.color.top_bg), R.layout.mypop_menu, null);
popupWin2.addAction(new PopActionItem(self, "拍照", "1"));
popupWin2.addAction(new PopActionItem(self, "从相册选择", "2"));
popupWin2.addAction(new PopActionItem(self, "取消", "3"));
// 设置弹窗选择事件
popupWin2.setItemOnClickListener(new MenuPopup.OnItemOnClickListener() {
@Override
public void onItemClick(PopActionItem item, int position) {
if ("1".equals(item.mType)) {// 拍照
GetPicUtil.doTakePhoto(HighFabuActivity.this,
Constant.CAMERA_WITH_DATA);
} else if ("2".equals(item.mType)) {// 相册选择
GetPicUtil.doPickPhotoFromGallery(HighFabuActivity.this,
Constant.PHOTO_PICKED_WITH_DATA);
}
}
});