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" ?> |
3 | < style name = "popupAnimation" parent = "android:Animation" > |
4 | < item name = "android:windowEnterAnimation" >@anim/in</ item > |
5 | < item name = "android:windowExitAnimation" >@anim/out</ item > |
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" > |
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" > |
16 | android:id = "@+id/confirmButton" |
17 | android:layout_width = "fill_parent" |
18 | android:layout_height = "wrap_content" |
19 | android:layout_gravity = "center" |
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" |
in.xml如下:
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
2 | < set xmlns:android = "http://schemas.android.com/apk/res/android" > |
4 | android:fromYDelta = "5000" |
6 | android:duration = "1500" |
out.xml如下:
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
2 | < set xmlns:android = "http://schemas.android.com/apk/res/android" > |
5 | android:toYDelta = "5000" |
6 | android:duration = "1500" |
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; |
13 | * 仿Iphone从屏幕底部弹出半透明的PopupWindow |
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; |
22 | public void onCreate(Bundle savedInstanceState) { |
23 | super .onCreate(savedInstanceState); |
24 | setContentView(R.layout.main); |
28 | button=(Button) findViewById(R.id.button); |
29 | button.setOnClickListener( new ButtonOnClickListener()); |
32 | private class ButtonOnClickListener implements OnClickListener { |
34 | public void onClick(View v) { |
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()); |
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 ); |
49 | case R.id.confirmButton: |
50 | System.out.println( "点击了确定按钮" ); |
52 | case R.id.cancleButton: |
53 | popupWindow.dismiss(); |