使用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>  

实现效果:


demo地址:
http://download.csdn.net/detail/mad1989/5518035

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,457评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,207评论 4 61
  • 明天大好的周末,又得去上课,哎,不开心。今天想聊的主题了不是开心的问题,而是学习学什么。 一般我们学的内容都是跟工...
    丿子木丨阅读 1,488评论 0 0
  • 你可以不理解别人的想法和观点,但要尊重事实的存在,不能把自己的观点扭曲成它就是事实,你必须怎么怎么样什么的… 你有...
    鑫妈Ma阅读 3,998评论 0 0
  • 吃完早餐之后,我留下来收拾餐具,丹娜去了隔壁帮戴维斯收拾屋子。说实话我对戴维斯的映像不是很好,他虽然绅士,但是又过...
    AgLian阅读 1,449评论 0 0