服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android使用PopupWindow实现页面点击顶部弹出下拉菜单

android使用PopupWindow实现页面点击顶部弹出下拉菜单

2021-03-29 14:30Never-say-Never Android

这篇文章主要给大家介绍android使用PopupWindow实现页面点击顶部弹出下拉菜单,实现此功能主要通过PopupWindow方法,代码也很简单,需要的朋友可以参考下

实现此功能没有太多的技术难点,主要通过popupwindow方法,同时更进一步加深了popupwindow的使用,实现点击弹出一个自定义的view,view里面可以自由设计,比较常用的可以放一个listview。

demo中我只是一个点击展示,简单的使用了fade in out的动画效果,也没有精美的图片资源,看着也丑,不过这么短的时间,让你掌握一个很好用的技术,可以自己扩展,不很好么?

废话不说了,直接上代码:

mainactivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class mainactivity extends activity implements onclicklistener {
  private popupwindow popupwindow;
  private button button;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    button = (button) findviewbyid(r.id.button1);
    button.setonclicklistener(this);
  }
  @override
  public void onclick(view v) {
    switch (v.getid()) {
    case r.id.button1:
      if (popupwindow != null&&popupwindow.isshowing()) {
        popupwindow.dismiss();
        return;
      } else {
        initmpopupwindowview();
        popupwindow.showasdropdown(v, 0, 5);
      }
      break;
    default:
      break;
    }
  }
  public void initmpopupwindowview() {
    // // 获取自定义布局文件pop.xml的视图
    view customview = getlayoutinflater().inflate(r.layout.popview_item,
        null, false);
    // 创建popupwindow实例,200,150分别是宽度和高度
    popupwindow = new popupwindow(customview, 250, 280);
    // 设置动画效果 [r.style.animationfade 是自己事先定义好的]
    popupwindow.setanimationstyle(r.style.animationfade);
    // 自定义view添加触摸事件
    customview.setontouchlistener(new ontouchlistener() {
      @override
      public boolean ontouch(view v, motionevent event) {
        if (popupwindow != null && popupwindow.isshowing()) {
          popupwindow.dismiss();
          popupwindow = null;
        }
        return false;
      }
    });
    /** 在这里可以实现自定义视图的功能 */
    button btton2 = (button) customview.findviewbyid(r.id.button2);
    button btton3 = (button) customview.findviewbyid(r.id.button3);
    button btton4 = (button) customview.findviewbyid(r.id.button4);
    btton2.setonclicklistener(this);
    btton3.setonclicklistener(this);
    btton4.setonclicklistener(this);
  }
}

activity_main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#000000"
  tools:context=".mainactivity" >
  <button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignparentleft="true"
    android:layout_alignparenttop="true"
    android:gravity="center"
    android:background="#c0c0c0"
    android:text="点击下拉列表" />
</relativelayout>

自定义view的xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#c0c0c0" >
  <button
    android:id="@+id/button2"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignparentleft="true"
    android:layout_alignparenttop="true"
    android:paddingright="70dp"
    android:text="viviens" />
  <button
    android:id="@+id/button3"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignparentleft="true"
    android:layout_below="@+id/button2"
    android:paddingright="70dp"
    android:text="mryang" />
  <button
    android:id="@+id/button4"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignparentleft="true"
    android:layout_below="@+id/button3"
    android:paddingright="70dp"
    android:text="张晓达" />
</relativelayout>

动画效果:

inputodown.xml 进入屏幕

?
1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
  <translate
    android:duration="500"
    android:fromydelta="-100%"
    android:toydelta="0" />
</set>

outdowntoup.xml

?
1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
  <translate
    android:duration="500"
    android:fromydelta="0"
    android:toydelta="-100%" />
</set>

styles.xml

?
1
2
3
4
5
<style name="animationfade">
  <!-- popupwindow左右弹出的效果 -->
  <item name="android:windowenteranimation">@anim/inuptodown</item>
  <item name="android:windowexitanimation">@anim/outdowntoup</item>
</style>

实现效果:

android使用PopupWindow实现页面点击顶部弹出下拉菜单

以上所述就是本文对android使用popupwindow实现页面点击顶部弹出下拉菜单的全部内容,希望大家喜欢。

延伸 · 阅读

精彩推荐