单击按钮下拉菜单

本文介绍了一个具体的 Android 应用案例,展示了如何使用 PopupWindow 控件,并结合动画效果实现下拉菜单功能。该应用通过自定义按钮触发 PopupWindow 的显示与隐藏,并为 PopupWindow 添加了进入和退出的动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    package com.test.ui;  
      
    import android.app.Activity;  
    import android.graphics.drawable.BitmapDrawable;  
    import android.os.Bundle;  
    import android.view.Gravity;  
    import android.view.KeyEvent;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.view.ViewGroup.LayoutParams;  
    import android.widget.Button;  
    import android.widget.PopupWindow;  
    import android.widget.Toast;  
      
    public class TestActivity extends Activity {  
        private Button but_menu;  
        private Button open_id;  
        private Button save_id;  
        View contentView;  
        private PopupWindow m_popupWindow;  
      
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            init();  
            setListener();  
      
        }  
      
        private void init() {  
            contentView = getLayoutInflater().inflate(R.layout.popupmenu, null,  
                    true);  
            but_menu = (Button) findViewById(R.id.but_menu);  
            open_id = (Button) contentView.findViewById(R.id.btn_popup_information);  
            save_id = (Button) contentView.findViewById(R.id.btn_popup_quote);  
            // PopupWindow弹出的窗口显示的view,第二和第三参数:分别表示此弹出窗口的大小  
            m_popupWindow = new PopupWindow(contentView, LayoutParams.FILL_PARENT,  
                    LayoutParams.WRAP_CONTENT, true);  
      
            m_popupWindow.setBackgroundDrawable(new BitmapDrawable());// 有了这句才可以点击返回(撤销)按钮dismiss()popwindow  
            m_popupWindow.setOutsideTouchable(true);  
            m_popupWindow.setAnimationStyle(R.style.PopupAnimation);  
        }  
      
        private void setListener() {  
            contentView.setOnClickListener(new View.OnClickListener() {  
                public void onClick(View v) {  
                    m_popupWindow.dismiss();  
                }  
            });  
            // m_popupWindow = new PopupWindow();  
            but_menu.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                    try {  
                        if (m_popupWindow.isShowing()) {  
      
                            m_popupWindow.dismiss();  
                        }  
                        m_popupWindow.showAsDropDown(v);  
      
                    } catch (Exception e) {  
                        Toast.makeText(TestActivity.this, e.getMessage(),  
                                Toast.LENGTH_SHORT);  
                    }  
                }  
            });  
            open_id.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                    m_popupWindow.dismiss();  
                    Toast.makeText(TestActivity.this, "打开被触发", Toast.LENGTH_SHORT)  
                            .show();  
                }  
      
            });  
            save_id.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                    m_popupWindow.dismiss();  
                    Toast.makeText(TestActivity.this, "保存被触发", Toast.LENGTH_SHORT)  
                            .show();  
                }  
      
            });  
        }  
      
        public boolean onKeyDown(int keyCode, KeyEvent event) {  
            if (keyCode == KeyEvent.KEYCODE_BACK) {  
                if (m_popupWindow != null && m_popupWindow.isShowing()) {  
                    m_popupWindow.dismiss();  
                    return true;  
                }  
            }  
            return super.onKeyDown(keyCode, event);  
        }  
    }  

    popupmenu.xml  
      
    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/lin_main"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:paddingRight="120dip" >  
      
        <LinearLayout  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:background="@drawable/popup_search"  
            android:orientation="vertical" >  
      
            <Button  
                android:id="@+id/btn_popup_information"  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_weight="1"  
                android:background="@drawable/goods_bg"  
                android:text="打开"  
                android:textColor="#000000"  
                android:textSize="16sp" />  
      
            <Button  
                android:id="@+id/btn_popup_quote"  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_marginTop="5dip"  
                android:layout_weight="1"  
                android:background="@drawable/goods_bg"  
                android:text="保存"  
                android:textColor="#000000"  
                android:textSize="16sp" />  
      
            <Button  
                android:id="@+id/btn_popup_product"  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_marginTop="5dip"  
                android:layout_weight="1"  
                android:background="@drawable/goods_bg"  
                android:text="关于我们"  
                android:textColor="#000000"  
                android:textSize="16sp" />  
      
            <Button  
                android:id="@+id/btn_popup_buy"  
                android:layout_width="fill_parent"  
                android:layout_height="wrap_content"  
                android:layout_marginTop="5dip"  
                android:layout_weight="1"  
                android:background="@drawable/shop_bg"  
                android:text="关闭"  
                android:textColor="#000000"  
                android:textSize="16sp" />  
        </LinearLayout>  
      
    </LinearLayout>  


    下面的是popwindow出现和退出的动画特效,这种东西网上有很多,就拿出这个吧.  
    values/style.xml  
    <?xml version="1.0" encoding="utf-8"?>  
    <resources>  
      
        <style name="PopupAnimation" parent="android:Animation">  
            <item name="android:windowEnterAnimation">@anim/popup_search_show</item>  
            <item name="android:windowExitAnimation">@anim/popup_search_hide</item>  
        </style>  
      
    </resources>anim/popup_search_show.xml  
    <?xml version="1.0" encoding="utf-8"?>  
    <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator">  
      
        <scale android:duration="500" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="0.100000024%" android:pivotY="0.0" android:toXScale="0.0" android:toYScale="0.0"></scale>  
      
    </set>  

    anim/popup_search_hide.xml  
      
    <?xml version="1.0" encoding="utf-8"?>  
    <set xmlns:android="http://schemas.android.com/apk/res/android"  
        android:interpolator="@android:anim/decelerate_interpolator" >  
      
        <scale  
            android:duration="500"  
            android:fromXScale="0.0"  
            android:fromYScale="0.0"  
            android:pivotX="0.100000024%"  
            android:pivotY="0.0"  
            android:toXScale="1.0"  
            android:toYScale="1.0" >  
        </scale>  
      
    </set>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值