最近在做下拉框,本来想用spinner,可是spinner达不到项目要求,跟同学同事问了一圈,都在用popwindow,

网上看了一下,popwindow挺简单的,可定制性挺强的,符合我的要求,所以,借鉴网上看的代码,自己撸了一

遍。写篇博客以防忘记。

首先,先写个自定义布局,代码如下

<?xml version="1.0" encoding="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);
}
}

(由于暂时没有发现好的动画效果,所以没有添加动画,如果大家有发现好的动画,还请告知一二,在此谢过)

效果图:

用PopWindow做下拉框的更多相关文章

  1. 用PopupWindow做下拉框

    最近在做下拉框,本来想用spinner,可是spinner达不到项目要求,跟同学同事问了一圈,都在用popwindow, 网上看了一下,popwindow挺简单的,可定制性挺强的,符合我的要求,所以, ...

  2. C# 生成月份及天选择列表,方便做下拉框联动

    月份及天选择列表,很方便做下拉框联动 /// <summary> /// 获取月份选择列表(根据当前语言环境显示月份名称) /// </summary> private IEn ...

  3. GridControl控件绑定RepositoryItemImageComboBox 作为下拉框使用

    如果,时间长时了,已前做过的东西,都记不得了,所以记录一下. 废话不多说. 1.拖出gridview控件,然后将字段绑定上去 2.将要做下拉框的控件加入RepositoryItemImageCombo ...

  4. devpress GridControl控件绑定RepositoryItemImageComboBox 作为下拉框使用 zt

    1.拖出gridview控件,然后将字段绑定上去 2.将要做下拉框的控件加入RepositoryItemImageComboBox控件 3.绑定数据 ; i < ; i++) { //如果取值时 ...

  5. 月薪10K必备--C#下拉框联动

                   下拉框联动 很多网站上都用到下拉框联动,就是第一个下拉框没有选择任何项,第二个下拉框就没有选项.这样的做法更加谨慎,更加紧密. 下面我就教大家怎么做下拉框联动: 首先在窗 ...

  6. Ext 下拉框联动第一次显示不正常的问题

    做下拉框联动,异步加载数据,第一次显示时数据不准确,不要在combo_2的下拉框直接绑定store,在combo_1的改变事件里调用下面的方法 function GetAllCustomerBrand ...

  7. 下拉框选择blur与click冲突问题

    缘由:今天在做下拉框选择时,遇到click和blur冲突问题:具体现象如下 1.intput框获取焦点(focus事件)时显示隐藏的下拉框,失去焦点(blur事件)则隐藏下拉框 2.点击选择(clic ...

  8. easyui datagrid combobox下拉框获取数据问题

    最近在使用easyui的datagrid,在可编辑表格中添加一个下拉框,查了下API,可以设置type : 'combobox',来做下拉框,这下拉框是有了,可是这后台数据怎么传过来呢,通过查API可 ...

  9. PHP.TP框架下商品项目的优化3-php封装下拉框函数

    php封装下拉框函数 因为在项目中会经常使用到下拉框,所以根据一个表中的数据制作下拉框函数,以便调用 //使用一个表的数据做下拉框函数 function buildSelect($tableName, ...

随机推荐

  1. VBS学习日记(一个) 开始了解

    Vbs 一个 Windows 脚本,其代表 :Microsoft Visual Basic Script Editon.( 微软可视化BASIC 脚本版),VBS 是 Visual Basic 的的一 ...

  2. 在标记的HREF属性中javascript:alert(this.innerHTML)会怎么样?

    原文:在标记的HREF属性中javascript:alert(this.innerHTML)会怎么样? <a href="javascript:alert(this.innerHTML ...

  3. 《互联网初创企业password》书评

    今天试用了一下<互联网初创企业password>这本书.我觉得这本书开始的很真实,从学校刚毕业那会儿.它是生命,他们的牛b时间,一直想做点什么来证明自己.具体地,并且现在是在最好的时候.互 ...

  4. Oracle笔试题库 附参考答案

    1.  下列不属于ORACLE的逻辑结构的是(C) 区 段 数据文件 表空间 2. 下面哪个用户不是ORACLE缺省安装后就存在的用户(A) A . SYSDBA B. SYSTEM C. SCOTT ...

  5. .net创建并安装windows服务案例

    1. 创建windows服务[引用博文]: 1. 将这个服务程序切换到设计视图2. 右击设计视图选择“添加安装程序”3. 切换到刚被添加的ProjectInstaller的设计视图4. 设置servi ...

  6. ArcGIS 10.1 发布使用ArcEngine自定义的GP服务

    1. 新建立GP模型 在VS2010中新建一个普通的程序及,引入ArcEngine相关的dll.在该DLL中定义一个或多个GP类和一个GP工厂类.GP类要继承IGPFunction2接口,GP工厂类要 ...

  7. .NET源码

    值得珍藏的.NET源码,不保存就没机会了 很早以前,我们通过http://referencesource.microsoft.com/netframework.aspx可以下载到.NET的各版本公开源 ...

  8. 优秀Python学习资源

    优秀Python学习资源收集汇总(强烈推荐)   Python是一种面向对象.直译式计算机程序设计语言.它的语法简捷和清晰,尽量使用无异义的英语单词,与其它大多数程序设计语言使用大括号不一样,它使用縮 ...

  9. 重拾C

    重拾C,一天一点点_10 来博客园今天刚好两年了,两年前开始学编程. 忙碌近两个月,项目昨天上线了,真心不容易,也不敢懈怠,接下来的问题会更多.这两天调试服务器,遇到不少麻烦. 刚出去溜达了一下,晚上 ...

  10. z-index 解析

    很多人对z-index的认识仅止于:z-index是控制元素在页面中的堆叠顺序,z-index值高的元素显示在z-index值低的前面.而其中的原因才很少有人去深究,直到自己在实际项目中碰到由于z-i ...