自定义PopupWindow
2016-07-17 11:53
295 查看
一、自定义
二、布局
三、用法
-------------------------------------------------------------------------------
zxp的写法
用法:
public class MyPopupWindow extends PopupWindow { // 根视图 private View mRootView; public View getmRootView() { return mRootView; } // LayoutInflater LayoutInflater mInflater; public MyPopupWindow (Activity context) { super(context); mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mRootView = mInflater.inflate(R.layout.popwindow, null);//可以把这个布局抽出来 setContentView(mRootView); this.setWidth(LayoutParams.WRAP_CONTENT); this.setHeight(LayoutParams.WRAP_CONTENT); // 设置PopUpWindow弹出的相关属性 setTouchable(true); setOutsideTouchable(true); setFocusable(true); setBackgroundDrawable(new BitmapDrawable(context.getResources())); update(); getContentView().setFocusableInTouchMode(true); getContentView().setFocusable(true); setAnimationStyle(R.style.AppBaseTheme); } }
二、布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="121dp" android:layout_height="wrap_content" android:gravity="center_horizontal" android:background="@drawable/pop_bg"> <TextView android:id="@+id/tv_pop_trave" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="40dp" android:text="旅拍" android:textSize="18dp" android:textColor="@drawable/popwindow_color_selector" android:drawableLeft="@drawable/popwindow_travel_drawableleft_selector" android:drawablePadding="10dp" android:gravity="center" android:clickable="true"/> <TextView android:layout_width="110dp" android:layout_height="0.4dp" android:background="#a784a4" /> <TextView android:id="@+id/tv_pop_honeymoon" android:layout_width="wrap_content" android:layout_height="40dp" android:text="蜜月" android:textSize="18dp" android:textColor="@drawable/popwindow_color_selector" android:drawableLeft="@drawable/popwindow_honeymoon_drawableleft_selector" android:drawablePadding="10dp" android:gravity="center" android:clickable="true"/> <TextView android:layout_width="110dp" android:layout_height="0.4dp" android:background="#a784a4" /> <TextView android:id="@+id/tv_pop_wedding" android:layout_width="wrap_content" android:layout_height="40dp" android:text="婚礼" android:textSize="18dp" android:textColor="@drawable/popwindow_color_selector" android:drawableLeft="@drawable/popwindow_wedding_drawableleft_selector" android:drawablePadding="10dp" android:gravity="center" android:clickable="true"/> </LinearLayout>
三、用法
private MyPopupWindow myPopupWindow;//弹出框 private View rootView;
myPopupWindow = new MyPopupWindow(this); // 设置点击其他位置mTestPopwindow2消失 myPopupWindow.setOnDismissListener(this);//需要实现接口PopupWindow.OnDismissListener OnMyPopwindow(); rootView = myPopupWindow.getmRootView(); //设置布局里面的监听 rootView.findViewById(R.id.tv_pop_trave).setOnClickListener(this);
/** * 弹出下拉窗口 */ private void OnMyPopwindow() { if (myPopupWindow == null) return; DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); // location获得控件的位置 int[] location = new int[2]; View v = llPop;//llPop为要弹出框的上方控件 if (v != null) v.getLocationOnScreen(location); // 控件在屏幕的位置 myPopupWindow.setAnimationStyle(R.style.AppBaseTheme); // mTestPopwindow2弹出在某控件(button)的下面 myPopupWindow.showAtLocation(v, Gravity.TOP | Gravity.LEFT, location[0] - v.getWidth() + CommonUtil.dipTopx(5), location[1] + v.getHeight()); // location[1] + v.getHeight() }
myPopupWindow.dismiss();//弹出框消失
-------------------------------------------------------------------------------
zxp的写法
/** * 共同的popWindow,并且将背景置透明 * * @param popview * @param tv * @param x */ public void showPopList (View popview, TextView tv, double x) { final PopupWindow mPopupWindow = new PopupWindow(popview); mCurrentPopupWindow = mPopupWindow; ViewHolder viewHolder = new ViewHolder(popview); mCureentViewHolder = viewHolder; DisplayMetrics metrics = new DisplayMetrics(); //保存屏幕大小 getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); int width = metrics.widthPixels; int height = metrics.heightPixels; int lastWidth = width; int lastHeight = (int) (height * x); mPopupWindow.setWidth(lastWidth); mPopupWindow.setHeight(lastHeight); ColorDrawable cd = new ColorDrawable(0x000000); mPopupWindow.setBackgroundDrawable(cd);//设置背景颜色 //产生背景变暗效果 WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes(); lp.alpha = 0.4f; getActivity().getWindow().setAttributes(lp); mPopupWindow.setOutsideTouchable(true); //点击popupwindow外部,popupwindow也会dismiss mPopupWindow.setFocusable(true); mPopupWindow.setContentView(popview); //恢复背景颜色 mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { //在dismiss中恢复透明度 public void onDismiss () { WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes(); lp.alpha = 1f; getActivity().getWindow().setAttributes(lp); } }); mPopupWindow.showAsDropDown(tv); }
用法:
if (mViewProvinceCity == null) { mViewProvinceCity = LayoutInflater.from(v.getContext()).inflate(R.layout.popwindow_store_province_city, null); } showPopList(mViewProvinceCity, tvProvinceCity, 0.4);
相关文章推荐
- 《linux学习》之常用命令:查看版本信息
- dial tcp: lookup index.docker.io: no such host
- mac 安装nginx
- linux用户组与权限管理
- 个人Linux发行版SwairOS
- nginx 499错误
- POJ 3050——Hopscotch(dfs,爆搜)
- linux xshell远程登录nodejs启动 断开链接会关闭进程问题
- linux系统启动优化
- nginx proxy X-Forwarded-For
- Linux htop工具使用详解
- linux系统优化
- linux shell 编程(四)
- linux下的nginx中php文件出现缓存的问题
- websphere 6.1迁移至tomcat 6.0
- Linux命令缩写总结
- [python]如何调用shell命令
- [python]如何调用shell命令
- 关于Centos7下搭建LNMP环境的问题小结
- Hadoop 2.0的两种RPC