用PopupWindow实现从底部滑出选择菜单

2024-06-18 11:58

本文主要是介绍用PopupWindow实现从底部滑出选择菜单,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在开发PopupWindow的弹出效果是必不可少的。许多应用的App都喜欢用到PopupWindow。如:新浪微博客户端、微信客户端、大众点评客户端等等。今天给大家

介绍微信客户端里用PopupWindow实现的一种从底部滑出选择菜单的效果。

本实例弹出窗口主要是继承PopupWindow类来实现的弹出窗体,布局可以根据自己定义设计。弹出效果主要使用了translate和alpha样式实现,具体实例如下:

第一步:设计弹出窗口xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="vertical"><LinearLayout android:id="@+id/pop_layout"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="vertical"android:layout_alignParentBottom="true"android:background="@drawable/btn_style_alert_dialog_background"><Buttonandroid:id="@+id/btn_take_photo"android:layout_marginLeft="20dip"android:layout_marginRight="20dip"android:layout_marginTop="20dip"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="拍照"android:background="@drawable/btn_style_alert_dialog_button"android:textStyle="bold"/><Buttonandroid:id="@+id/btn_pick_photo"android:layout_marginLeft="20dip"android:layout_marginRight="20dip"android:layout_marginTop="5dip"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="从相册选择"android:background="@drawable/btn_style_alert_dialog_button"android:textStyle="bold"/><Buttonandroid:id="@+id/btn_cancel"android:layout_marginLeft="20dip"android:layout_marginRight="20dip"android:layout_marginTop="15dip"android:layout_marginBottom="15dip"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="取消"android:background="@drawable/btn_style_alert_dialog_cancel"android:textColor="#ffffff"android:textStyle="bold"/>
</LinearLayout>
</RelativeLayout>

 第二步:创建SelectPicPopupWindow类继承PopupWindow:

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;public class SelectPicPopupWindow extends PopupWindow {private Button btn_take_photo, btn_pick_photo, btn_cancel;private View mMenuView;public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {super(context);LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);mMenuView = inflater.inflate(R.layout.alert_dialog, null);btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);//取消按钮btn_cancel.setOnClickListener(new OnClickListener() {public void onClick(View v) {//销毁弹出框dismiss();}});//设置按钮监听btn_pick_photo.setOnClickListener(itemsOnClick);btn_take_photo.setOnClickListener(itemsOnClick);//设置SelectPicPopupWindow的Viewthis.setContentView(mMenuView);//设置SelectPicPopupWindow弹出窗体的宽this.setWidth(LayoutParams.FILL_PARENT);//设置SelectPicPopupWindow弹出窗体的高this.setHeight(LayoutParams.WRAP_CONTENT);//设置SelectPicPopupWindow弹出窗体可点击this.setFocusable(true);//设置SelectPicPopupWindow弹出窗体动画效果this.setAnimationStyle(R.style.AnimBottom);//实例化一个ColorDrawable颜色为半透明ColorDrawable dw = new ColorDrawable(0xb0000000);//设置SelectPicPopupWindow弹出窗体的背景this.setBackgroundDrawable(dw);//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框mMenuView.setOnTouchListener(new OnTouchListener() {public boolean onTouch(View v, MotionEvent event) {int height = mMenuView.findViewById(R.id.pop_layout).getTop();int y=(int) event.getY();if(event.getAction()==MotionEvent.ACTION_UP){if(y<height){dismiss();}}               return true;}});}}

第三步:编写MainActivity类实现测试:

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;public class MainActivity extends Activity {//自定义的弹出框类SelectPicPopupWindow menuWindow;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView tv = (TextView) this.findViewById(R.id.text);//把文字控件添加监听,点击弹出自定义窗口tv.setOnClickListener(new OnClickListener() {           public void onClick(View v) {//实例化SelectPicPopupWindowmenuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);//显示窗口menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置}});}//为弹出窗口实现监听类private OnClickListener  itemsOnClick = new OnClickListener(){public void onClick(View v) {menuWindow.dismiss();switch (v.getId()) {case R.id.btn_take_photo:break;case R.id.btn_pick_photo:               break;default:break;}}};}

第四:运行效果如下:


这篇关于用PopupWindow实现从底部滑出选择菜单的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1071980

相关文章

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

OpenCV图像形态学的实现

《OpenCV图像形态学的实现》本文主要介绍了OpenCV图像形态学的实现,包括腐蚀、膨胀、开运算、闭运算、梯度运算、顶帽运算和黑帽运算,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起... 目录一、图像形态学简介二、腐蚀(Erosion)1. 原理2. OpenCV 实现三、膨胀China编程(

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件