iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >android使用PopupWindow实现页面点击顶部弹出下拉菜单
  • 576
分享到

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

菜单popupwindow下拉菜单Android 2022-06-06 09:06:30 576人浏览 薄情痞子
摘要

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

实现此功能没有太多的技术难点,主要通过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实现页面点击顶部弹出下拉菜单的全部内容,希望大家喜欢。

您可能感兴趣的文章:android PopupWindow 和 Activity弹出窗口实现方式android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍Android Animation实战之屏幕底部弹出PopupWindowAndroid编程实现popupwindow弹出后屏幕背景变成半透明效果Android入门之PopupWindow用法实例解析Android实现底部弹出PopupWindow背景逐渐变暗效果Android之用PopupWindow实现弹出菜单的方法详解Android中PopupWindow响应返回键并关闭的2种方法android自定义popupwindow仿微信右上角弹出菜单效果android popupwindow用法详解


--结束END--

本文标题: android使用PopupWindow实现页面点击顶部弹出下拉菜单

本文链接: https://www.lsjlt.com/news/26635.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作