全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

android用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做下拉框  # android弹出popwindow  # android  # popwindow  # Android开发Popwindow仿微信右上角下拉菜单实例代码  # Android PopWindow 设置背景亮度的实例  # Android Popwindow弹出框的模板使用示例  # Android 中Popwindow弹出菜单的两种方法实例  # Android自定义组件ListPopWindow  # android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍  # Android自定义PopWindow实现QQ、微信弹出菜单效果  # 自定义  # 一扫  # 下拉框  # 看了  # 都在  # 在此  # 网上  # 一遍  # 暂时没有  # 问了  # 想用  # 达不到  # 第二步  # 必不可少  # 绑定  # 还请  # 如要  # 大家多多  # 空白处  # 先写 


相关文章: 微信网站制作公司有哪些,民生银行办理公司开户怎么在微信网页上查询进度?  简历在线制作网站免费版,如何创建个人简历?  山东云建站价格为何差异显著?  番禺网站制作公司哪家值得合作,番禺图书馆新馆开放了吗?  深圳网站制作平台,深圳市做网站好的公司有哪些?  如何快速搭建高效WAP手机网站?  如何在云虚拟主机上快速搭建个人网站?  如何快速选择适合个人网站的云服务器配置?  制作旅游网站html,怎样注册旅游网站?  如何通过免费商城建站系统源码自定义网站主题与功能?  ,石家庄四十八中学官网?  如何挑选最适合建站的高性能VPS主机?  学校为何禁止电信移动建设网站?  湖州网站制作公司有哪些,浙江中蓝新能源公司官网?  网站制作说明怎么写,简述网页设计的流程并说明原因?  兔展官网 在线制作,怎样制作微信请帖?  如何快速启动建站代理加盟业务?  如何优化Golang Web性能_Golang HTTP服务器性能提升方法  建站之星如何实现PC+手机+微信网站五合一建站?  在线ppt制作网站有哪些,请推荐几个好的课件下载的网站?  如何用AWS免费套餐快速搭建高效网站?  如何在服务器上配置二级域名建站?  长沙做网站要多少钱,长沙国安网络怎么样?  c# 服务器GC和工作站GC的区别和设置  利用JavaScript实现拖拽改变元素大小  武汉网站制作费用多少,在武汉武昌,建面100平方左右的房子,想装暖气片,费用大概是多少啊?  建站主机是否等同于虚拟主机?  如何用PHP工具快速搭建高效网站?  如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?  娃派WAP自助建站:免费模板+移动优化,快速打造专业网站  建站上市公司网站建设方案与SEO优化服务定制指南  岳西云建站教程与模板下载_一站式快速建站系统操作指南  如何通过智能用户系统一键生成高效建站方案?  微信推文制作网站有哪些,怎么做微信推文,急?  ,制作一个手机app网站要多少钱?  个人网站制作流程图片大全,个人网站如何注销?  如何在搬瓦工VPS快速搭建网站?  专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?  宝塔建站无法访问?如何排查配置与端口问题?  焦点电影公司作品,电影焦点结局是什么?  广东企业建站网站优化与SEO营销核心策略指南  网站制作的软件有哪些,制作微信公众号除了秀米还有哪些比较好用的平台?  宝塔建站教程:一键部署配置流程与SEO优化实战指南  如何获取PHP WAP自助建站系统源码?  ui设计制作网站有哪些,手机UI设计网址吗?  标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?  如何在七牛云存储上搭建网站并设置自定义域名?  网站插件制作软件免费下载,网页视频怎么下到本地插件?  C++用Dijkstra(迪杰斯特拉)算法求最短路径  建站之星如何通过成品分离优化网站效率? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。