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

 更新时间:2015年08月19日 11:03:08   作者:Never-say-Never  
这篇文章主要给大家介绍android使用PopupWindow实现页面点击顶部弹出下拉菜单,实现此功能主要通过PopupWindow方法,代码也很简单,需要的朋友可以参考下

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

相关文章

  • Matrix的set,pre,post调用顺序详解

    Matrix的set,pre,post调用顺序详解

    下面小编就为大家带来一篇Matrix的set,pre,post调用顺序详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Android长按imageview把图片保存到本地的实例代码

    Android长按imageview把图片保存到本地的实例代码

    本文通过代码给大家介绍了Android长按imageview把图片保存到本地的实现方法,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-12-12
  • Android操作SQLite基本用法

    Android操作SQLite基本用法

    这篇文章主要介绍了Android操作SQLite基本用法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-12-12
  • Android侧滑导航栏的实例代码

    Android侧滑导航栏的实例代码

    这篇文章主要介绍了Android侧滑导航栏的实例代码的相关资料,需要的朋友可以参考下
    2017-01-01
  • Android拖拽助手ViewDragHelper的创建与使用实例

    Android拖拽助手ViewDragHelper的创建与使用实例

    ViewDragHelper是针对 ViewGroup 中的拖拽和重新定位 views 操作时提供了一系列非常有用的方法和状态追踪,下面这篇文章主要给大家介绍了关于Android拖拽助手ViewDragHelper的创建与使用的相关资料,需要的朋友可以参考下
    2022-05-05
  • TabLayout+ViewPager2的简单使用详解

    TabLayout+ViewPager2的简单使用详解

    这篇文章主要为大家详细介绍了TabLayout+ViewPager2的简单使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • Android Studio Gradle插件版本与Gradle版本之间的对应关系

    Android Studio Gradle插件版本与Gradle版本之间的对应关系

    今天小编就为大家分享一篇关于Android Studio Gradle插件版本与Gradle版本之间的对应关系,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Android使用CountDownTimer类实现倒计时闹钟

    Android使用CountDownTimer类实现倒计时闹钟

    这篇文章主要为大家详细介绍了Android使用CountDownTimer类实现倒计时闹钟,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Android实现单页面浮层可拖动view的一种方法

    Android实现单页面浮层可拖动view的一种方法

    本篇文章主要介绍了Android实现单页面浮层可拖动view的一种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Android  Handler 机制实现原理分析

    Android Handler 机制实现原理分析

    本文主要介绍 Android Handle机制实现的原理,这里整理了详细的关于Handler的资料以及工作流程和实际应用,有兴趣的小伙伴可以参考下
    2016-08-08

最新评论