滑动菜单方案6:使用PopupWindows实现透明的弹出菜单

main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button 
        android:id="@+id/button"
        android:text="popupWindow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
</RelativeLayout>


styles.xml如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <style name="popupAnimation" parent="android:Animation">
4         <item name="android:windowEnterAnimation">@anim/in</item>
5         <item name="android:windowExitAnimation">@anim/out</item>
6     </style>
7 </resources>


popupwindow.xml如下:

01 <?xml version="1.0" encoding="utf-8"?>
02 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:layout_width="match_parent"
04     android:layout_height="match_parent"
05     android:background="#b5555555" >
06  
07     <LinearLayout
08         android:layout_width="match_parent"
09         android:layout_height="wrap_content"
10         android:layout_alignParentBottom="true"
11         android:layout_marginBottom="12dip"
12         android:paddingLeft="10dip"
13         android:paddingRight="10dip"
14         android:orientation="vertical" >
15         <Button
16             android:id="@+id/confirmButton"
17             android:layout_width="fill_parent"
18             android:layout_height="wrap_content"
19             android:layout_gravity="center"
20             android:text="确定"/>
21  
22         <Button
23             android:id="@+id/cancleButton"
24             android:layout_marginTop="12dip"
25             android:layout_width="fill_parent"
26             android:layout_height="wrap_content"
27             android:layout_gravity="center"
28             android:text="取消" />
29     </LinearLayout>
30  
31 </RelativeLayout>


in.xml如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3     <translate
4         android:fromYDelta="5000"
5         android:toYDelta="0"
6         android:duration="1500"
7     />
8 </set>


out.xml如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3     <translate
4         android:fromYDelta="0"
5         android:toYDelta="5000"
6         android:duration="1500"
7     />
8 </set>

 

PopupWindowTestActivity.java如下:

01 import android.app.Activity;
02 import android.graphics.drawable.BitmapDrawable;
03 import android.os.Bundle;
04 import android.view.Gravity;
05 import android.view.LayoutInflater;
06 import android.view.View;
07 import android.view.View.OnClickListener;
08 import android.view.ViewGroup.LayoutParams;
09 import android.widget.Button;
10 import android.widget.PopupWindow;
11 /**
12  * Demo描述:
13  * 仿Iphone从屏幕底部弹出半透明的PopupWindow
14  */
15 public class PopupWindowTestActivity extends Activity {
16     private Button button;
17     private Button confirmButton;
18     private Button cancleButton;
19     private PopupWindow popupWindow;
20     private View popupWindowView;
21     @Override
22     public void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.main);
25         init();
26     }
27     private void init(){
28           button=(Button) findViewById(R.id.button);
29           button.setOnClickListener(new ButtonOnClickListener());
30     }
31  
32     private class ButtonOnClickListener implements OnClickListener {
33         @Override
34         public void onClick(View v) {
35             switch (v.getId()) {
36             case R.id.button:
37                 LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
38                 popupWindowView = inflater.inflate(R.layout.popupwindow, null);
39                 popupWindow = new PopupWindow(popupWindowView,LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,true);
40                 popupWindow.setBackgroundDrawable(new BitmapDrawable());
41                 //设置PopupWindow的弹出和消失效果
42                 popupWindow.setAnimationStyle(R.style.popupAnimation);
43                 confirmButton = (Button) popupWindowView.findViewById(R.id.confirmButton);
44                 confirmButton.setOnClickListener(new ButtonOnClickListener());
45                 cancleButton = (Button) popupWindowView.findViewById(R.id.cancleButton);
46                 cancleButton.setOnClickListener(new ButtonOnClickListener());
47                 popupWindow.showAtLocation(confirmButton, Gravity.CENTER, 00);
48                 break;
49             case R.id.confirmButton:
50                 System.out.println("点击了确定按钮");
51                 break;
52             case R.id.cancleButton:
53                 popupWindow.dismiss();
54                 break;
55             default:
56                 break;
57             }
58  
59         }}
60 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值