最近在做下拉框,本来想用spinner,可是spinner达不到项目要求,跟同学同事问了一圈,都在用popwindow,网上看了一下,popwindow挺简单的,可定制性挺强的,符合我的要求,所以,借鉴网上看的代码,自己撸了一遍。写篇博客以防忘记。
首先,先写个自定义布局,代码如下
<?xml version="1.0" en@R_403_5563@="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="110dp" androID:layout_height="wrap_content"> <linearLayout androID:layout_wIDth="100dp" androID:layout_height="wrap_content" androID:background="@drawable/bg_circle_drop_down_qr_code" androID:orIEntation="vertical" androID:layout_marginRight="@dimen/padding_10" androID:paddingBottom="0dp" androID:paddingleft="@dimen/padding_5" androID:paddingRight="@dimen/padding_5" androID:paddingtop="@dimen/padding_5"> <linearLayout androID:ID="@+ID/lin_scan_qr_code" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:gravity="center" androID:orIEntation="horizontal" androID:paddingBottom="@dimen/padding_5" androID:paddingtop="@dimen/padding_5"> <ImageVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:src="@drawable/ic_circle_scan_qr_code" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="@dimen/padding_10" androID:gravity="center" androID:text="扫一扫" androID:textcolor="@color/color_white" androID:textSize="@dimen/text_16" /> </linearLayout> <VIEw androID:layout_wIDth="wrap_content" androID:layout_height="1px" androID:layout_marginleft="@dimen/padding_3" androID:layout_marginRight="@dimen/padding_3" androID:background="@color/color_white" /> <linearLayout androID:ID="@+ID/lin_my_qr_code" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:gravity="center" androID:orIEntation="horizontal" androID:paddingBottom="@dimen/padding_5" androID:paddingtop="@dimen/padding_5"> <ImageVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:src="@drawable/ic_circle_my_qr_code" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="@dimen/padding_10" androID:gravity="center" androID:text="二维码" androID:textcolor="@color/color_white" androID:textSize="@dimen/text_16" /> </linearLayout> </linearLayout></linearLayout>
第二步,在代码中定义popwindow样式,绑定点击事件,代码如下:
// // 获取自定义布局文件pop.xml的视图 VIEw customVIEw = getActivity().getLayoutInflater().inflate(R.layout.lay_circle_pop_drop_down_qr_code,null,false); // 创建PopupWindow实例,200,150分别是宽度和高度 mQrCodePopWindow = new PopupWindow(customVIEw,CommonUtil.diptopx(getContext(),110),VIEwGroup.LayoutParams.WRAP_CONTENT,true); // 设置动画效果 [R.style.AnimationFade 是自己事先定义好的]// popupwindow.setAnimationStyle(R.style.AnimationFade);// popupwindow.settouchable(true);// popupwindow.setoutsIDetouchable(true); mQrCodePopWindow.setBackgroundDrawable(new BitmapDrawable()); customVIEw.findVIEwByID(R.ID.lin_scan_qr_code).setonClickListener(v -> { ToastUtil.show(getContext(),"扫一扫"); dismissQrCodePopWindow(); }); customVIEw.findVIEwByID(R.ID.lin_my_qr_code).setonClickListener(v -> ToastUtil.show(getContext(),"二维码"));
注意,代码中的true为setFoucusable,如要点击空白处隐藏popwindow的话,setFocusable(true)和setBackground()两者必不可少(亲测)。
最后,为空间添加点击事件,控制下拉框的显示隐藏,代码如下:
@OnClick(R.ID.lin_top_right) public voID onClick(VIEw v) { if (mQrCodePopWindow != null&& mQrCodePopWindow.isShowing()) { mQrCodePopWindow.dismiss(); } else { initQrCodePopWindow(); mQrCodePopWindow.showAsDropDown(v); } }
(由于暂时没有发现好的动画效果,所以没有添加动画,如果大家有发现好的动画,还请告知一二,在此谢过)
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的android用PopWindow做下拉框实例代码全部内容,希望文章能够帮你解决android用PopWindow做下拉框实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)