用PopWindow做下拉框
最近在做下拉框,本来想用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做下拉框的更多相关文章
- 用PopupWindow做下拉框
最近在做下拉框,本来想用spinner,可是spinner达不到项目要求,跟同学同事问了一圈,都在用popwindow, 网上看了一下,popwindow挺简单的,可定制性挺强的,符合我的要求,所以, ...
- C# 生成月份及天选择列表,方便做下拉框联动
月份及天选择列表,很方便做下拉框联动 /// <summary> /// 获取月份选择列表(根据当前语言环境显示月份名称) /// </summary> private IEn ...
- GridControl控件绑定RepositoryItemImageComboBox 作为下拉框使用
如果,时间长时了,已前做过的东西,都记不得了,所以记录一下. 废话不多说. 1.拖出gridview控件,然后将字段绑定上去 2.将要做下拉框的控件加入RepositoryItemImageCombo ...
- devpress GridControl控件绑定RepositoryItemImageComboBox 作为下拉框使用 zt
1.拖出gridview控件,然后将字段绑定上去 2.将要做下拉框的控件加入RepositoryItemImageComboBox控件 3.绑定数据 ; i < ; i++) { //如果取值时 ...
- 月薪10K必备--C#下拉框联动
下拉框联动 很多网站上都用到下拉框联动,就是第一个下拉框没有选择任何项,第二个下拉框就没有选项.这样的做法更加谨慎,更加紧密. 下面我就教大家怎么做下拉框联动: 首先在窗 ...
- Ext 下拉框联动第一次显示不正常的问题
做下拉框联动,异步加载数据,第一次显示时数据不准确,不要在combo_2的下拉框直接绑定store,在combo_1的改变事件里调用下面的方法 function GetAllCustomerBrand ...
- 下拉框选择blur与click冲突问题
缘由:今天在做下拉框选择时,遇到click和blur冲突问题:具体现象如下 1.intput框获取焦点(focus事件)时显示隐藏的下拉框,失去焦点(blur事件)则隐藏下拉框 2.点击选择(clic ...
- easyui datagrid combobox下拉框获取数据问题
最近在使用easyui的datagrid,在可编辑表格中添加一个下拉框,查了下API,可以设置type : 'combobox',来做下拉框,这下拉框是有了,可是这后台数据怎么传过来呢,通过查API可 ...
- PHP.TP框架下商品项目的优化3-php封装下拉框函数
php封装下拉框函数 因为在项目中会经常使用到下拉框,所以根据一个表中的数据制作下拉框函数,以便调用 //使用一个表的数据做下拉框函数 function buildSelect($tableName, ...
随机推荐
- hdu 4858 项目管理(STL集装箱)
项目管理 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 自己编译的openwrt在开机时一直打印jffs2_scan_eraseblock(): Magic bitmask 0x1985 not found...的错误提示
具体的输入信息是这样的. uboot> http Link down: eth0 Ethernet mode (duplex/speed): / Mbps HTTP server is star ...
- [翻译]初识SQL Server 2005 Reporting Services Part 1
原文:[翻译]初识SQL Server 2005 Reporting Services Part 1 构建和部署基本报表 如果曾经存在一项工作使得“真正的”开发者给他的上司泡蘑菇,那就是构建报表.毕竟 ...
- 在希望的田野上--生物柴油(Biodiesel)光明的未来
请看下图: 这是科学家Bernie Tao教授给美国Purdue大学的学生们出的题目"有关大豆.谷物产品的创新竞赛",实质上,就是鼓舞研究.开发及应用生物柴油(Biodiesel) ...
- 读书笔记—CLR via C#章节4-7
前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...
- 枚举for/in
for/in循环可以遍历对象中所有可以枚举的属性(包括自有属性和继承属性).对象继承的内置方法不能枚举,凡是在代码中给对象自己或者继承的类添加的属性方法都是可枚举的,但是对象自有的内置属性可不可以枚举 ...
- 尝试使用Memcached
尝试使用Memcached遇到的狗血问题 乘着有时间,尝试下利用Memcached进行分布式缓存,其中遇到了不少问题及狗血的事情,开篇记录下,希望对您有帮助. 我之前的项目为:Asp.Net MV ...
- How To: Use CLR Profiler
(翻译)How To: Use CLR Profiler 第一次翻译对我而言比较长的E文,有很多不足之处,请见谅.(个人的习惯GC又做了名词又做了名词) 原文:http://msdn.micros ...
- .NET软件开发资源
.NET软件开发资源 最近建了一个.NET软件开发资源的360网盘共享群,把收集的一些.NET软件开发资源分享给大家,也欢迎大家把好的东东分享一下. 资源主要有:开发工具.控件资源.书籍教程.网页设计 ...
- C#通过模板导出Word(文字,表格,图片)
C#通过模板导出Word(文字,表格,图片) C#导出Word,Excel的方法有很多,这次因为公司的业务需求,需要导出内容丰富(文字,表格,图片)的报告,以前的方法不好使,所以寻找新的导出方法, ...