最近在做下拉框,本来想用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);
}
}

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

效果图:

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

  1. 用PopWindow做下拉框

    最近在做下拉框,本来想用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. ArcGIS Desktop Python add-ins 插件类型

    Python可以在ArcGIS 桌面程序中创建如下的插件类型: 插件类型 描述 按钮(button) 是一个简单功能定制形式,可以在点击情况下执行一些业务逻辑.它会执行一个或一系列的动作.按钮可以放在 ...

  2. MessageBox的常见用法

    一 函数原型及参数 function MessageBox(hWnd: HWND; Text, Caption: PChar; Type: Word): Integer; hWnd:对话框父窗口句柄, ...

  3. BUG_vector iterator not dereferencable

    1问题: bug提示图下图所示:

  4. hdu4417(Super Mario)—— 二分+划分树

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. 一个基于ASP.NET(C#)的ACCESS数据库操作类

    using System; using System.Collections; using System.Collections.Specialized; using System.Data; usi ...

  6. Oracle function函数赋权

    -- 1.赋权 -- 在原有权的账号下个执行 grant select on psprd.functionName to user; -- 2. 别名 -- 在需要使用别名的账号下执行 CREATE ...

  7. DataType 枚举

    命名空间:   System.ComponentModel.DataAnnotations 成员名称 说明   CreditCard 表示信用卡号码.   Currency 表示货币值.   Cust ...

  8. delete job definition

    $badjobTake2 = Get-SPTimerJob -ID "job-service-instance-f2de35ab-bca3-4e53-b51a-98d3b06a6930&qu ...

  9. C++解析头文件-Qt自动生成信号定义

    目录 一.概述 二.实现思路 三.代码讲解 1.类图 2.QtCppDescription 3.测试 四.源代码 一.概述 上一篇文章C++解析头文件-Qt自动生成信号声明我们主要讲解了怎么去解析C+ ...

  10. CRUSH map 定制实例解析

    1.提取已有的CRUSH map ,使用-o参数,ceph将输出一个经过编译的CRUSH map 到您指定的文件ceph osd getcrushmap -o crushmap.txt 2.反编译你的 ...