您的位置:首页 > 运维架构

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

2013-12-31 00:55 375 查看
main.xml如下:
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns: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,
0
,
0
);
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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: