当前位置: 移动技术网 > 移动技术>移动开发>Android > android使用PopupWindow实现页面点击顶部弹出下拉菜单

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

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

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

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

mainactivity.java

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

<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

<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 进入屏幕

<?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

<?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

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

实现效果:


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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网