最近在做下拉框,本来想用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. 不用快捷键就能使用Eclipse的自动完成功能

    偶然间看到了这个,或许有和我一样不喜欢按 alt-/ 兄弟用得上.不用老去按那个 alt-/ 了,还是方便不少.         打开 Eclipse -> Window -> Perfe ...

  2. Blender3d做大型项目真实地形快速建模

    https://www.rr-sc.com/thread-16531254-1-1.html Blender3d是一款优秀的动画和建模工具,但在做一些大型建设项目时需要在真实地形上来表现,Blende ...

  3. CDialog类

    CDilalog包含三个关键函数:OnInitDialog.OnOK和OnCancel,可以覆盖这三个函数初始化对话框.响应点击OK和Cancel按钮.尽管每个函数都响应一条对话框消息,但是不需要你提 ...

  4. Linux监控本机当前状态命令

    vmstat 1.简介 vmstat命令是最常见的Linux监控工具,可以查看系统的状态值,其中包括:CPU.内存.虚拟内存.I/O情况. 2.参数说明 命令格式:  vmstat [-a] [-n ...

  5. How to create a Multi-device Site - some details

    https://developers.google.com/web/fundamentals/getting-started/your-first-multi-screen-site/index?hl ...

  6. SQL笔记---多表左联

    这是实际场景当中的一个例子,拿出来分析总结思路. -- SQL 查询 --SELECT  orderQuery.Rk_SkuCode ,        orderQuery.SkuName,      ...

  7. Spring Boot 2 实践记录之 使用 Powermock、Mockito 对 UUID 进行 mock 单元测试

    由于注册时,需要对输入的密码进行加密,使用到了 UUID.sha1.md 等算法.在单元测试时,使用到了 Powermock,记录如下. 先看下加密算法: import org.apache.comm ...

  8. Python try/except/finally等

    Python try/except/finally等   x = 'abc' def fetcher(obj, index): return obj[index] fetcher(x, 4) 输出: ...

  9. 巧用XML配置校验导入Excel的列数据格式

    <?xml version="1.0"?> <ColumnsSeting xmlns:xsd="http://www.w3.org/2001/XMLSc ...

  10. UE4随笔 二 第一印象

    打开UE4,短暂的兴奋过后,开始大概扫一扫UE4的编辑器,整个界面比UE3更有现代气息: 之前看其他人写的文章,虚幻4最重要的改动集中在下面几个方向上: 跨平台: WIN和MAC平台都能使用,这就意味 ...