今天项目中要用到对话框浮层显示,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>








更多相关文章

  1. android sudio 快捷键
  2. Android开机界面logo
  3. android 双击图片放大,再双击缩小效果 【安卓进化九】
  4. file(内部存储与外部存储)
  5. Android(安卓)vold核心篇(VolumeManager)
  6. android 环境配置和安装, Android系统包说明,基本控件,常用代码,
  7. Android中快速为Recyclerview添加头部
  8. Android(安卓)FrameLayout布局中的控件设置居中动态设置
  9. android取得json数据

随机推荐

  1. Android的SDK,NDK以及JNI
  2. Android(安卓)Studio快捷键设置成跟Eclip
  3. Android模拟器环境中安装和删除应用程序
  4. 浅谈android的selector,背景选择器
  5. Android中Launcher对于AppWidget处理的分
  6. android 获取uri的正确文件路径的办法
  7. Android(安卓)NDK带来什么
  8. android 手机存储介质大全
  9. Android(安卓)软键盘弹出隐藏挤压界面等
  10. 如何实现android清理后台时,自己的软件不