今天項目中要用到對話方塊浮層顯示,PopupWindow可以實現該功能。可以自訂view,通過LayoutInflator方法載入自訂的布局。而且載入和退出時都可以自訂動畫效果。還能指定顯示的位置。如下所示:
MainActivity代碼:
package com.example.popwindow;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.PopupWindow;import android.widget.Toast;/** * * @author WangJintao * * @date 2013-5-6 */public class MainActivity extends Activity implements OnClickListener {Button button, button1, button2, button3;PopupWindow popupWindow;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button:getPopWin();popupWindow.showAsDropDown(v);break;case R.id.button1:Toast.makeText(MainActivity.this, "button1", Toast.LENGTH_LONG).show();popupWindow.dismiss();break;case R.id.button2:Toast.makeText(MainActivity.this, "button2", Toast.LENGTH_LONG).show();popupWindow.dismiss();break;case R.id.button3:Toast.makeText(MainActivity.this, "button3", Toast.LENGTH_LONG).show();popupWindow.dismiss();break;default:break;}}private void getPopWin() {if (popupWindow != null) {popupWindow.dismiss();} else {initPopWin();}}private void initPopWin() {View popupWindow_view = getLayoutInflater().inflate(R.layout.pop_win,null, false);popupWindow = new PopupWindow(popupWindow_view, 200, 300, true);popupWindow.setAnimationStyle(R.style.AnimationFade);popupWindow_view.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if (popupWindow != null && popupWindow.isShowing()) {popupWindow.dismiss();popupWindow = null;}return false;}});button1 = (Button) popupWindow_view.findViewById(R.id.button1);button2 = (Button) popupWindow_view.findViewById(R.id.button2);button3 = (Button) popupWindow_view.findViewById(R.id.button3);button1.setOnClickListener(this);button2.setOnClickListener(this);button3.setOnClickListener(this);}}
activity_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" tools:context=".MainActivity" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="PopWindow" /></RelativeLayout>
彈出的布局:
pop_win.xml代碼
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/darker_gray" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button1" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button2" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button3" /></LinearLayout>
style代碼:
<!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> <style name="AnimationFade"> <!-- PopupWindow左右彈出的效果 --> <item name="android:windowEnterAnimation">@anim/in_lefttoright</item> <item name="android:windowExitAnimation">@anim/out_righttoleft</item> </style>
動畫代碼:
in_lefttoright.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 定義從左向右進入的動畫 --> <translate android:duration="500" android:fromXDelta="-100%" android:toXDelta="0" /></set>
out_righttoleft.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 定義從右向左動畫退齣動畫 --> <translate android:duration="500" android:fromXDelta="0" android:toXDelta="-100%" /></set>