效果如图所看到的,点击開始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. [置顶] 关于redhat系统yum源的配置1

    安装过Linux软件的用户就知道,有时我们安装一个软件,需要依赖其他软件,所以必需找全所有的软件,这是一个极其麻烦的事情,有没什么方式可以让它自己去找依赖呢? 答案当然是肯定,这就需要我们配置一个神器 ...

  2. hadoop源码下载地址

    http://svn.apache.org/repos/asf/hadoop/common/branches/

  3. javascript启示录英文单词生词

    odd:奇怪的 represent:代表 primitive:原始的 trivial:平凡的 demonstrate:证明 keep this at the forefront of your min ...

  4. PhoneGap-----Contacts

    Everything in the code!!! <!DOCTYPE html> <html> <head> <title>Contact Examp ...

  5. win7下怎样设置putty免用户名密码登陆

    putty是一款好用的远程登录linux服务器软件,但每次输入用户名密码毕竟有些烦人,这里教你免用户名密码登陆.   工具/原料 putty 方法/步骤   去百度下载putty,小巧易用,仅有0.5 ...

  6. oschina图形和图像工具开源软件

    图形和图像工具开源软件 http://www.oschina.net/project/tag/181/imagetools?sort=view&lang=21&os=0

  7. mysql left join,right join,inner join用法分析

    下面是例子分析表A记录如下: aID        aNum 1           a20050111 2           a20050112 3           a20050113 4   ...

  8. ASP.NET MVC的跳转攻击问题

    在ASP.NET MVC的自带的模板代码中,有这样一段,用来拦截非登录用户,使其跳转到登录页面,然后登录后在跳转回原页面.所以,期间有一个returnUrl参数用来保存原页面地址.在Login Act ...

  9. iOS 搜索框控件 最简单的dome

    刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...

  10. 七古&#183;夏泳小梅沙

    七古·夏泳小梅沙 文/天地尘埃2020 近日与同学等海泳小梅沙,归后背黑而焦灼如针刺.一周后焦皮始脱尽,发现还是往日那个黄种人.涂鸦一文以记之. 一湾碧水青山前, 夏日方来酷暑煎. 疏狂仅仅愿清凉刻, ...