单击按钮下拉菜单

[java]  view plain copy print ?
  1. package com.test.ui;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.BitmapDrawable;  
  5. import android.os.Bundle;  
  6. import android.view.Gravity;  
  7. import android.view.KeyEvent;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.widget.Button;  
  12. import android.widget.PopupWindow;  
  13. import android.widget.Toast;  
  14.   
  15. public class TestActivity extends Activity {  
  16.     private Button but_menu;  
  17.     private Button open_id;  
  18.     private Button save_id;  
  19.     View contentView;  
  20.     private PopupWindow m_popupWindow;  
  21.   
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         init();  
  26.         setListener();  
  27.   
  28.     }  
  29.   
  30.     private void init() {  
  31.         contentView = getLayoutInflater().inflate(R.layout.popupmenu, null,  
  32.                 true);  
  33.         but_menu = (Button) findViewById(R.id.but_menu);  
  34.         open_id = (Button) contentView.findViewById(R.id.btn_popup_information);  
  35.         save_id = (Button) contentView.findViewById(R.id.btn_popup_quote);  
  36.         // PopupWindow弹出的窗口显示的view,第二和第三参数:分别表示此弹出窗口的大小  
  37.         m_popupWindow = new PopupWindow(contentView, LayoutParams.FILL_PARENT,  
  38.                 LayoutParams.WRAP_CONTENT, true);  
  39.   
  40.         m_popupWindow.setBackgroundDrawable(new BitmapDrawable());// 有了这句才可以点击返回(撤销)按钮dismiss()popwindow  
  41.         m_popupWindow.setOutsideTouchable(true);  
  42.         m_popupWindow.setAnimationStyle(R.style.PopupAnimation);  
  43.     }  
  44.   
  45.     private void setListener() {  
  46.         contentView.setOnClickListener(new View.OnClickListener() {  
  47.             public void onClick(View v) {  
  48.                 m_popupWindow.dismiss();  
  49.             }  
  50.         });  
  51.         // m_popupWindow = new PopupWindow();  
  52.         but_menu.setOnClickListener(new OnClickListener() {  
  53.             public void onClick(View v) {  
  54.                 try {  
  55.                     if (m_popupWindow.isShowing()) {  
  56.   
  57.                         m_popupWindow.dismiss();  
  58.                     }  
  59.                     m_popupWindow.showAsDropDown(v);  
  60.   
  61.                 } catch (Exception e) {  
  62.                     Toast.makeText(TestActivity.this, e.getMessage(),  
  63.                             Toast.LENGTH_SHORT);  
  64.                 }  
  65.             }  
  66.         });  
  67.         open_id.setOnClickListener(new OnClickListener() {  
  68.             public void onClick(View v) {  
  69.                 m_popupWindow.dismiss();  
  70.                 Toast.makeText(TestActivity.this"打开被触发", Toast.LENGTH_SHORT)  
  71.                         .show();  
  72.             }  
  73.   
  74.         });  
  75.         save_id.setOnClickListener(new OnClickListener() {  
  76.             public void onClick(View v) {  
  77.                 m_popupWindow.dismiss();  
  78.                 Toast.makeText(TestActivity.this"保存被触发", Toast.LENGTH_SHORT)  
  79.                         .show();  
  80.             }  
  81.   
  82.         });  
  83.     }  
  84.   
  85.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  86.         if (keyCode == KeyEvent.KEYCODE_BACK) {  
  87.             if (m_popupWindow != null && m_popupWindow.isShowing()) {  
  88.                 m_popupWindow.dismiss();  
  89.                 return true;  
  90.             }  
  91.         }  
  92.         return super.onKeyDown(keyCode, event);  
  93.     }  
  94. }  
[html]  view plain copy print ?
  1. popupmenu.xml  
  2.   
  3. <?xml version="1.0" encoding="utf-8"?>  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:id="@+id/lin_main"  
  6.     android:layout_width="wrap_content"  
  7.     android:layout_height="wrap_content"  
  8.     android:paddingRight="120dip" >  
  9.   
  10.     <LinearLayout  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:background="@drawable/popup_search"  
  14.         android:orientation="vertical" >  
  15.   
  16.         <Button  
  17.             android:id="@+id/btn_popup_information"  
  18.             android:layout_width="fill_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_weight="1"  
  21.             android:background="@drawable/goods_bg"  
  22.             android:text="打开"  
  23.             android:textColor="#000000"  
  24.             android:textSize="16sp" />  
  25.   
  26.         <Button  
  27.             android:id="@+id/btn_popup_quote"  
  28.             android:layout_width="fill_parent"  
  29.             android:layout_height="wrap_content"  
  30.             android:layout_marginTop="5dip"  
  31.             android:layout_weight="1"  
  32.             android:background="@drawable/goods_bg"  
  33.             android:text="保存"  
  34.             android:textColor="#000000"  
  35.             android:textSize="16sp" />  
  36.   
  37.         <Button  
  38.             android:id="@+id/btn_popup_product"  
  39.             android:layout_width="fill_parent"  
  40.             android:layout_height="wrap_content"  
  41.             android:layout_marginTop="5dip"  
  42.             android:layout_weight="1"  
  43.             android:background="@drawable/goods_bg"  
  44.             android:text="关于我们"  
  45.             android:textColor="#000000"  
  46.             android:textSize="16sp" />  
  47.   
  48.         <Button  
  49.             android:id="@+id/btn_popup_buy"  
  50.             android:layout_width="fill_parent"  
  51.             android:layout_height="wrap_content"  
  52.             android:layout_marginTop="5dip"  
  53.             android:layout_weight="1"  
  54.             android:background="@drawable/shop_bg"  
  55.             android:text="关闭"  
  56.             android:textColor="#000000"  
  57.             android:textSize="16sp" />  
  58.     </LinearLayout>  
  59.   
  60. </LinearLayout>  

[html]  view plain copy print ?
  1. 下面的是popwindow出现和退出的动画特效,这种东西网上有很多,就拿出这个吧.  
  2. values/style.xml  
  3. <?xml version="1.0" encoding="utf-8"?>  
  4. <resources>  
  5.   
  6.     <style name="PopupAnimation" parent="android:Animation">  
  7.         <item name="android:windowEnterAnimation">@anim/popup_search_show</item>  
  8.         <item name="android:windowExitAnimation">@anim/popup_search_hide</item>  
  9.     </style>  
  10.   
  11. </resources>anim/popup_search_show.xml  
  12. <?xml version="1.0" encoding="utf-8"?>  
  13. <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator">  
  14.   
  15.     <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>  
  16.   
  17. </set>  

[html]  view plain copy print ?
  1. anim/popup_search_hide.xml  
  2.   
  3. <?xml version="1.0" encoding="utf-8"?>  
  4. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:interpolator="@android:anim/decelerate_interpolator" >  
  6.   
  7.     <scale  
  8.         android:duration="500"  
  9.         android:fromXScale="0.0"  
  10.         android:fromYScale="0.0"  
  11.         android:pivotX="0.100000024%"  
  12.         android:pivotY="0.0"  
  13.         android:toXScale="1.0"  
  14.         android:toYScale="1.0" >  
  15.     </scale>  
  16.   
  17. </set>  

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值