PopupWindow可以实现浮层效果。
主要方法有:可以自定义view,通过LayoutInflator方法;可以出现和退出时显示动画;可以指定显示位置等。
为了将PopupWindow的多个功能展现并力求用简单的代码实现,编写了一个点击按钮左侧弹出菜单的功能,实现出现和退出时显示动画效果并点击其他区域时弹出层自动消失。
Activity文件
- package com.app.test02;
- import android.app.Activity;
- import android.os.Bundle;
- 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;
- public class PopwindowLeft extends Activity {
- // 声明PopupWindow对象的引用
- private PopupWindow popupWindow;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_popupwindow_main);
- // 点击按钮弹出菜单
- Button pop = (Button) findViewById(R.id.popBtn);
- pop.setOnClickListener(popClick);
- }
- // 点击弹出左侧菜单的显示方式
- OnClickListener popClick = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- getPopupWindow();
- // 这里是位置显示方式,在按钮的左下角
- popupWindow.showAsDropDown(v);
- // 这里可以尝试其它效果方式,如popupWindow.showAsDropDown(v,
- // (screenWidth-dialgoWidth)/2, 0);
- // popupWindow.showAtLocation(findViewById(R.id.layout),
- // Gravity.CENTER, 0, 0);
- }
- };
- /**
- * 创建PopupWindow
- */
- protected void initPopuptWindow() {
- // TODO Auto-generated method stub
- // 获取自定义布局文件activity_popupwindow_left.xml的视图
- View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,
- false);
- // 创建PopupWindow实例,200,150分别是宽度和高度
- popupWindow = new PopupWindow(popupWindow_view, 200, 150, true);
- // 设置动画效果
- popupWindow.setAnimationStyle(R.style.AnimationFade);
- // 点击其他地方消失
- popupWindow_view.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- if (popupWindow != null && popupWindow.isShowing()) {
- popupWindow.dismiss();
- popupWindow = null;
- }
- return false;
- }
- });
- // activity_popupwindow_left.xml视图里面的控件
- Button open = (Button) popupWindow_view.findViewById(R.id.open);
- Button save = (Button) popupWindow_view.findViewById(R.id.save);
- Button close = (Button) popupWindow_view.findViewById(R.id.close);
- // activity_popupwindow_left.xml视图里面的控件触发的事件
- // 打开
- open.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- // 这里可以执行相关操作
- Toast.makeText(PopwindowLeft.this, "打开操作", 1000);
- // 对话框消失
- popupWindow.dismiss();
- }
- });
- // 保存
- save.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- // 这里可以执行相关操作
- Toast.makeText(PopwindowLeft.this, "保存操作", 1000);
- popupWindow.dismiss();
- }
- });
- // 关闭
- close.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- // 这里可以执行相关操作
- Toast.makeText(PopwindowLeft.this, "关闭操作", 1000);
- popupWindow.dismiss();
- }
- });
- }
- /***
- * 获取PopupWindow实例
- */
- private void getPopupWindow() {
- if (null != popupWindow) {
- popupWindow.dismiss();
- return;
- } else {
- initPopuptWindow();
- }
- }
- }
PopupWindow主配置文件
- <?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:orientation="vertical" >
- <Button android:id="@+id/popBtn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="弹出左侧菜单" />
- </LinearLayout>
PopupWindow弹出菜单
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@android:color/darker_gray"
- android:orientation="vertical" >
- <Button
- android:id="@+id/open"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@android:color/darker_gray"
- android:text="打开" />
- <Button
- android:id="@+id/save"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@android:color/darker_gray"
- android:text="保存" />
- <Button
- android:id="@+id/close"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@android:color/darker_gray"
- android:text="关闭" />
- </LinearLayout>
弹出动画XML
在res文件夹下,建立anim文件夹。写入如下两个文件。
弹出动画
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>
动画管理
在styles.xml中,添加如下管理代码。
- <style name="AnimationFade">
- <!-- PopupWindow左右弹出的效果 -->
- <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
- <item name="android:windowExitAnimation">@anim/out_righttoleft</item>
- </style>