PopupWindow 弹出在控件上
2017-09-28 18:18
232 查看
popupWindow的布局文件popup.xml
Activity中弹出PopupWindow
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_parent" android:layout_height="wrap_content" android:background="#206d95e5" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="popupWindow" android:textSize="16sp" /> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="button" android:textSize="16sp" /> </LinearLayout>
Activity中弹出PopupWindow
public void showPopupWindow(View v){ View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup, null); Button button = (Button) contentView.findViewById(R.id.btn); final PopupWindow popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "onclick==action", Toast.LENGTH_SHORT).show(); } }); //检测屏幕消失的事件 popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { Log.d("Chunna.zheng","popupwindow消失啦!"); } }); // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框 popupWindow.setBackgroundDrawable(getResources().getDrawable( R.mipmap.ic_launcher)); //获取自身的长宽高 contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); int popupHeight = contentView.getMeasuredHeight(); int popupWidth = contentView.getMeasuredWidth(); //获取控件在屏幕上的位置,并赋值给location数组 int[] location = new int[2]; v.getLocationOnScreen(location); //在控件上方显示 popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0]), location[1] - popupHeight/2); // 如果要在控件下方显示,则使用这个方法 //popupWindow.showAsDropDown(v); }
相关文章推荐
- 一个简单的PopupWindow做的选项菜单点击按钮会在按钮下弹出几个下拉控件
- 控件:PopupWindow --- 弹出窗口(基本操作)
- Android popupwindow在指定控件正下方滑动弹出效果
- 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu
- ios实现底部PopupWindow的示例代码(底部弹出菜单)
- Flex 自定义组件 -- 弹出窗口 [PopUpManager TitleWindow 组件]的使用
- Android自定义PopupWindow显示在控件上方或者下方
- Android之PopupWindow类似微信右上角的弹出菜单
- 软键盘弹出+popupWindow的问题
- 消息提示类控件之PopupWindow(悬浮框)的基本使用
- Android popupWindow弹出窗体实现方法分析
- Android自定义PopupWindow显示在控件上方或者下方
- popupwindow弹出后出现当前activity和前一个activity一起出现的状况(window透明的设置)
- Android之PopupWindow弹出对话框
- Android PopupWindow实现从顶部弹出下拉菜单左、中、右
- PopupWindowFromBottom 从底部弹出popupwindow
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- Android之PopupWindow弹出对话框
- Android中PopupWindow显示在控件的左边
- Android之PopupWindow弹出对话框