popupWindow 的使用
2015-11-12 20:34
197 查看
废话不说,直接上代码
布局
private void showPopupWindow(View view) { //一个自定义 布局,作为显示的内容 View contentView = LayoutInflater.from(this).inflate(R.layout.activity_myinfo_popupwindow, null); //设置按钮的点击事件 Button bt1 = (Button) contentView.findViewById(R.id.myinfo_popup_button1); Button bt2 = (Button) contentView.findViewById(R.id.myinfo_popup_button2); //创建出PopupWindow final PopupWindow popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ToastUtil.toast(MyInfoActivity.this, "点击了bt1"); popupWindow.dismiss(); } }); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ToastUtil.toast(MyInfoActivity.this, "点击了bt2"); popupWindow.dismiss(); } }); // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框 // 我觉得这里是API的一个bug popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.book_page_bg2)); //设置宽度 popupWindow.setWidth(view.getWidth() / 4 * 3); //设置显示位置 popupWindow.showAtLocation(contentView, Gravity.CENTER, 0, -100); //可点击 popupWindow.setTouchable(true); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { popupWindow.dismiss(); return true; } //如果返回true,touch事件将被拦截 //拦截后,popupWindow的OnTouchEvennt不被调用,这样点击外部区域无法dismiss return false; } }); //将popup展示出来 popupWindow.showAsDropDown(view); } private void initAlert() { AlertDialog.Builder builder = new AlertDialog.Builder(this); }
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@mipmap/book_page_bg1"> <Button android:id="@+id/myinfo_popup_button1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#fffffff0" android:onClick="onclick" android:text="从相册中选取" android:textSize="18sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:background="#eeeeeeee"/> <Button android:id="@+id/myinfo_popup_button2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#fffffff0" android:onClick="onclick" android:text="拍照" android:textSize="18sp" /> </LinearLayout>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories