效果如图所看到的,点击開始button,popWindow从下往上出来,再点击popWindow外面,popWindow又从上往下消失

能够看出来,上面的popupWindow是半透明的,后面我会细说。

最主要的是activity_main了,非常easy,就仅仅是一个button,这里我就不贴代码了。

接下来的是,popWindow的界面了

代码例如以下:这里注意我里面的那个凝视

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" > <!-- 这里的linearLayout加android:background=""这个属性要慎重,假设加了后,popwindow是不能半透明了的 --> <Button
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@android:color/holo_red_light"
android:text="第一个button" /> <Button
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_light"
android:text="第二个button" /> <Button
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_light"
android:text="第三个button" /> </LinearLayout>

然后在res/下新建一个目录anim,进而anim下新建两个xml文件,如图所看到的:



当中,pophidden_anim的代码例如以下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="50%p" /> <alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>

popshow_anim的代码例如以下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" /> <alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>

然后在values/styles.xml增加下面代码,变成这个样子,上面的那些是自带的

<resources>

    <!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices. -->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here. -->
</style> <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style> <!-- 这个是增加的代码 -->
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定显示的动画xml --> <item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的动画xml -->
</style> </resources>

之后就是Activity里面的代码了,我里面都写好了全部的凝视,应该能够看得非常清楚

package com.example.popwindow;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
showPopwindow();
} });
} /**
* 显示popupWindow
*/
private void showPopwindow() {
// 利用layoutInflater获得View
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popwindowlayout, null); // 以下是两种方法得到宽度和高度 getWindow().getDecorView().getWidth() PopupWindow window = new PopupWindow(view,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT); // 设置popWindow弹出窗口可点击,这句话必须加入,而且是true
window.setFocusable(true); // 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
window.setBackgroundDrawable(dw); // 设置popWindow的显示和消失动画
window.setAnimationStyle(R.style.mypopwindow_anim_style);
// 在底部显示
window.showAtLocation(MainActivity.this.findViewById(R.id.start),
Gravity.BOTTOM, 0, 0); // 这里检验popWindow里的button能否够点击
Button first = (Button) view.findViewById(R.id.first);
first.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { System.out.println("第一个按钮被点击了");
}
}); //popWindow消失监听方法
window.setOnDismissListener(new OnDismissListener() { @Override
public void onDismiss() {
System.out.println("popWindow消失");
}
}); }
}

当中window.setFocusable(true)和window.setBackgroundDrawable()必须填写,假设是想让popWindow半透明,就是上面的那个方法,

假设仅仅是单纯的调用这种方法就这样写window.setBackgroundDrawable(new BitmapDrawable());

关于popupWindow显示位置的详细方法,你能够看这个博客popupWindow在指定位置上的显示

这个资源我已经上传,0积分下载,有须要的能够下载看一看点击这里下载

手把手教popupWindow从下往上,以达到流行效果的更多相关文章

  1. 手把手教你 在Pytorch框架上部署和测试 关键点人脸检测项目DBFace,成功实现人脸检测效果

    这期教向大家介绍仅仅 1.3M 的轻量级高精度的关键点人脸检测模型DBFace,并手把手教你如何在自己的电脑端进行部署和测试运行,运行时bug解决. 01. 前言 前段时间DBFace人脸检测库横空出 ...

  2. Android listview 制作表格样式+由下往上动画弹出效果实现

    效果是这样的:点击按下弹出表格的按钮,会由下往上弹出右边的列表,按下返回按钮就由上往下退出界面. 布局文件: activity_main.xml <RelativeLayout xmlns:an ...

  3. 手把手教你Windows下Go语言的环境搭建

    1.想写GO语言首先得下载go语言的开发包 官方下载地址:https://code.google.com/p/go/downloads/list 我用的是Win7 64位的操作系统,截图如下: 2.把 ...

  4. 手把手教你轻松实现listview上拉加载

    上篇讲了如何简单快速的的实现listview下拉刷新,那么本篇将讲解如何简单快速的实现上拉加载更多.其实,如果你已经理解了下拉刷新的实现过程,那么实现上拉加载更多将变得轻松起来,原理完全一致,甚至实现 ...

  5. 手把手教你实现Android RecyclerView上拉加载功能

    摘要 一直在用到RecyclerView时都会微微一颤,因为一直都没去了解怎么实现上拉加载,受够了每次去Github找开源引入,因为感觉就为了一个上拉加载功能而去引入一大堆你不知道有多少BUG的代码, ...

  6. 手把手教你将本地项目文件上传至github

    相信大家都听过Git(分布式版本号控制系统)和github吧.没听过也没关系(Google一下),反正以后要去公司肯定会听过. 我是在今年年初才接触Git.之后就一发不可收拾.仅仅要有比較好的项目就G ...

  7. 手把手教你用原始方式上传项目至GitHub

    小编GitHub:https://github.com/ds1889 首先你得注册一个自己的GitHub账号,注册网址:https://github.com/join 有了自己的账号以后,就可以进行登 ...

  8. 2020年,手把手教你如何在CentOS7上一步一步搭建LDAP服务器的最新教程

    同步滚动:关 什么是LDAP 什么是LDAP? 要想知道一个概念,最简单的办法就是wikipedia,当然也可以百科. LDAP全称是轻型目录访问协议(Lightweight Directory Ac ...

  9. 手把手教你在Modelarts平台上进行视频推理

    摘要:为了方便小伙伴们进行视频场景的AI应用开发,Modelarts推理平台将视频推理场景中一些通用的流程抽取出来预置在基础镜像中,小伙伴们只需要简单地编写预处理及后处理脚本,便可以像开发图片类型的A ...

随机推荐

  1. MOCKITO 应用示例

    package com.paic.wms.service.auditflow.impl; import static org.junit.Assert.*; import java.util.Arra ...

  2. Mac OS X将CSV格式转换为Excel文档格式,Excel转CSV中文乱码问题

    一:在Mac上假设你使用Excel打开windows导出的CSV格式文档.你会发现表格中全部的的内容都显示在A列. 那么,怎样恢复正常呢,你能够将CSV格式的文档导入到Excel文档中,这样就正常显示 ...

  3. POJ 1160 Post Office (动态规划)

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15412   Accepted: 8351 Desc ...

  4. JS-JavaScript学习笔记(一)

    javaScript 1.文档的输出:document.write() 可输出字符,表达式,html标签.函数 2.不论什么类型和字符串相加,都会被转换成字符串类型. 比如:var i=5; var ...

  5. 《Head First 设计模式》学习笔记——模板方法模式

    模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以详细方法以及详细构造函数的形式实现.然后声明一些抽象方法来迫使子类实现剩余的逻辑.不同的子类能够以不同的方式实现这些抽象方法,从而对剩余的逻辑有 ...

  6. Github干货系列:C++资源集合-

    Awesome CPP,这又是一个 Awesome XXX 系列的资源整理,由 fffaraz 发起和维护.内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. ...

  7. 高度关注!国务院对A股发出强烈信号↓

    高度关注!国务院对A股发出强烈信号↓http://dwz.cn/2qHBd1郎咸平:中国股市存在一大隐疾 使其成为全球市场的一个另类!http://dwz.cn/2qHBVy一不小心,马云又完成了四场 ...

  8. ubuntu 经常使用软件及环境

    安装完系统后,更新系统 sudo apt-get update # 更新源索引 sudo apt-get dist-upgrade # 更新系统 sudo apt-get autoremove 1安装 ...

  9. 为何要fork()两次来避免产生僵尸进程?

    为何要fork()两次来避免产生僵尸进程?   当我们只fork()一次后,存在父进程和子进程.这时有两种方法来避免产生僵尸进程: 父进程调用waitpid()等函数来接收子进程退出状态. 父进程先结 ...

  10. android中file的使用实例

    File是android的4种存储方式的一种.File就是文件的意思一个文件,你无非是想进行读写操作.所以这就用到两个流.一个数输入流,一个是输出流.FileOutstream,和FileInputS ...