Android的PopupWindow弹出效果

jopen 11年前

MainActivity如下:

package com.cn.testpopupwindow;  import android.app.Activity;  import android.graphics.drawable.BitmapDrawable;  import android.os.Bundle;  import android.view.Gravity;  import android.view.KeyEvent;  import android.view.LayoutInflater;  import android.view.Menu;  import android.view.View;  import android.view.View.OnClickListener;  import android.view.ViewGroup.LayoutParams;  import android.widget.Button;  import android.widget.PopupWindow;  public class MainActivity extends Activity {      private Button button;      private Button button1;      private Button button2;      private Button button3;      private Button button4;      private View popupWindowView;      private PopupWindow popupWindow;      private LayoutInflater inflater;      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          button=(Button) findViewById(R.id.button);          button.setOnClickListener(new ButtonOnClickListener());      }        @Override      public boolean onCreateOptionsMenu(Menu menu) {          getMenuInflater().inflate(R.menu.main, menu);          return true;      }      private class ButtonOnClickListener implements OnClickListener{    public void onClick(View v) {     inflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);     popupWindowView=inflater.inflate(R.layout.popupwindow, null);         popupWindow=new PopupWindow(popupWindowView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,true);     //必须要有这句否则弹出popupWindow后监听不到Back键     popupWindow.setBackgroundDrawable(new BitmapDrawable());     popupWindow.showAtLocation(findViewById(R.id.button), Gravity.NO_GRAVITY, 0, 0);     //让popupWindow获得焦点     popupWindow.setFocusable(true);     //设置动画     popupWindow.setAnimationStyle(R.style.popupWindowAnimation);     popupWindow.update();             //popupWindow中按钮的处理     button1=(Button) popupWindowView.findViewById(R.id.button1);     button2=(Button) popupWindowView.findViewById(R.id.button2);     button3=(Button) popupWindowView.findViewById(R.id.button3);     button4=(Button) popupWindowView.findViewById(R.id.button4);     button1.setOnClickListener(new ButtonsOnClickListener());     button2.setOnClickListener(new ButtonsOnClickListener());     button3.setOnClickListener(new ButtonsOnClickListener());     button4.setOnClickListener(new ButtonsOnClickListener());         }             }         private class ButtonsOnClickListener implements OnClickListener {    public void onClick(View v) {     switch (v.getId()) {     case R.id.button1:                  System.out.println("点击了按钮1");      break;     case R.id.button2:       System.out.println("点击了按钮2");      break;     case R.id.button3:       System.out.println("点击了按钮3");      break;     case R.id.button4:       System.out.println("点击了按钮4");      break;       default:      break;     }      }     }      //监听Back事件   @Override   public boolean onKeyDown(int keyCode, KeyEvent event) {    if (keyCode==KeyEvent.KEYCODE_BACK) {     if (popupWindow!=null&&popupWindow.isShowing()) {      popupWindow.dismiss();       } else {                  MainActivity.this.finish();     }    }    return super.onKeyDown(keyCode, event);   }  }


Main.xml如下:

<RelativeLayout       xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent" >        <Button           android:id="@+id/button"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="test"          android:layout_centerInParent="true"      />       </RelativeLayout>


popupwindow.xml如下:

<?xml version="1.0" encoding="utf-8"?>  <!--注意:在RelativeLayout里面是LinearLayout,且是vertical的-->  <!--第一个Button的 layout_marginTop是相对于父控件而言的-->  <!--但是第二个Button的layout_marginTop是相对于第一个Button而言的  -->  <RelativeLayout       xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:background="#f0838b8b"      android:orientation="vertical"      >            <LinearLayout          android:id="@+id/linerLayout"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:layout_alignParentBottom="true"          android:orientation="vertical"      >                    <Button               android:id="@+id/button1"              android:layout_width="200dip"              android:layout_height="wrap_content"              android:text="Button1"              android:textSize="20sp"              android:layout_gravity="center_horizontal"              android:layout_marginTop="20dip"          />           <Button               android:id="@+id/button2"              android:layout_width="200dip"              android:layout_height="wrap_content"              android:text="Button2"              android:textSize="20sp"              android:layout_gravity="center_horizontal"              android:layout_marginTop="20dip"          />            <Button               android:id="@+id/button3"              android:layout_width="200dip"              android:layout_height="wrap_content"              android:text="Button3"              android:textSize="20sp"              android:layout_gravity="center_horizontal"              android:layout_marginTop="20dip"          />             <Button               android:id="@+id/button4"              android:layout_width="200dip"              android:layout_height="wrap_content"              android:text="Button4"              android:textSize="20sp"              android:layout_gravity="center_horizontal"              android:layout_marginTop="20dip"          />                          </LinearLayout>          </RelativeLayout>


style.xml如下

<resources xmlns:android="http://schemas.android.com/apk/res/android">     <style name="AppTheme" parent="android:Theme.Light" />     <style name="popupWindowAnimation" mce_bogus="1" parent="android:Animation">          <item name="android:windowEnterAnimation">@anim/enter</item>          <item name="android:windowExitAnimation">@anim/exit</item>      </style>  </resources>


enter.xml动画如下:

<?xml version="1.0" encoding="utf-8"?>  <!-- android:fromYDelta:动画开始的点离当前View X坐标上的差值 -->  <!-- 利用100%p表示该动画在当前View的最下方 -->  <set       xmlns:android="http://schemas.android.com/apk/res/android">      <translate          android:duration="1500"          android:fromYDelta="100%p"          android:interpolator="@android:anim/decelerate_interpolator"          android:toYDelta="0" />    </set>


exit.xml动画如下:

<?xml version="1.0" encoding="utf-8"?>  <!-- Alpha=1.0表示不透明,Alpha=0.0表示透明 -->  <set       xmlns:android="http://schemas.android.com/apk/res/android"      android:interpolator="@android:anim/decelerate_interpolator" >      <alpha          android:duration="1000"          android:fromAlpha="1.0"          android:toAlpha="0.0"       />  </set>