/把文字控件添加监听,点击弹出自定义窗口
- tv.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- //实例化SelectPicPopupWindow
- menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
- //显示窗口
- menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
- }
- });
- }
其中的mian id 必须在layout 中写出,不然,找不到id 就不会显示在屏幕中。
- //为弹出窗口实现监听类
- private OnClickListener itemsOnClick = new OnClickListener(){
- public void onClick(View v) {
- menuWindow.dismiss();
- switch (v.getId()) {
- case R.id.btn_take_photo:
- break;
- case R.id.btn_pick_photo:
- break;
- default:
- break;
- }
- }
- };
这个是popwindow的工具类,定义这popwindow的弹出方向的动画。以及你pop中要显示的条目。
- mport android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.ColorDrawable;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.PopupWindow;
- public class SelectPicPopupWindow extends PopupWindow {
- private Button btn_take_photo, btn_pick_photo, btn_cancel;
- private View mMenuView;
- public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
- super(context);
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mMenuView = inflater.inflate(R.layout.alert_dialog, null);
- btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
- btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
- btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
- //取消按钮
- btn_cancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- //销毁弹出框
- dismiss();
- }
- });
- //设置按钮监听
- btn_pick_photo.setOnClickListener(itemsOnClick);
- btn_take_photo.setOnClickListener(itemsOnClick);
- //设置SelectPicPopupWindow的View
- this.setContentView(mMenuView);
- //设置SelectPicPopupWindow弹出窗体的宽
- this.setWidth(LayoutParams.FILL_PARENT);
- //设置SelectPicPopupWindow弹出窗体的高
- this.setHeight(LayoutParams.WRAP_CONTENT);
- //设置SelectPicPopupWindow弹出窗体可点击
- this.setFocusable(true);
- //设置SelectPicPopupWindow弹出窗体动画效果
- this.setAnimationStyle(R.style.AnimBottom);
- //实例化一个ColorDrawable颜色为半透明
- ColorDrawable dw = new ColorDrawable(0xb0000000);
- //设置SelectPicPopupWindow弹出窗体的背景
- this.setBackgroundDrawable(dw);
- //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
- mMenuView.setOnTouchListener(new OnTouchListener() {
- public boolean onTouch(View v, MotionEvent event) {
- int height = mMenuView.findViewById(R.id.pop_layout).getTop();
- int y=(int) event.getY();
- if(event.getAction()==MotionEvent.ACTION_UP){
- if(y<height){
- dismiss();
- }
- }
- return true;
- }
- });
- }
- }
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/main" //此处的mian 就是mainActivity要声明的
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:padding="@dimen/padding_medium"
- android:text="点击我弹出选择框"
- tools:context=".MainActivity" />
- </RelativeLayout>
额。。。大概也就这样。写的也不是很详细,大家可以下载下来看就明白了。
~3)0LL@WTUBF}DL~Z4T649O.jpg(78.82 KB, 下载次数: 36)
仿微信的弹出popwindow
S)P)$(DG(QZLR_@SA(3[NRQ.jpg(102.24 KB, 下载次数: 22)
仿微信的弹出popwindow
@4K0`A9G2~IVIJ[0UBLHLI9.jpg(76.25 KB, 下载次数: 15)
仿微信的弹出popwindow
下载地址
http://download.csdn.net/detail/liyaming1/6479935