package com.loaderman.popupwindowdemo;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
private Button btnPop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPop = (Button) findViewById(R.id.btn_Pop);
}
public void showPopupWindow(View v) {
View view = View.inflate(this, R.layout.layout_pop, null);
PopupWindow popup = new PopupWindow(view, v.getWidth(), 150, true);//设置尺寸及获取焦点
//必须设置背景色,不然不会消失
popup.setBackgroundDrawable(new ColorDrawable(Color.GRAY));//设置背景颜色
// 设置进入退出动画
popup.setAnimationStyle(R.style.PopAnimation);
// 显示在某个控件的正下方
popup.showAsDropDown(btnPop, 0, 0);
//显示在屏幕的位置
// popup.showAtLocation(rlRoot, Gravity.LEFT + Gravity.TOP, 0,0);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.loaderman.popupwindowdemo.MainActivity">
<Button
android:onClick="showPopupWindow"
android:id="@+id/btn_Pop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我显示PopupWindow"/>
</RelativeLayout>
layout_pop.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:text="测试一"
android:textColor="#fff"
android:layout_height="wrap_content"/>
<View
android:layout_width="match_parent"
android:background="#ccc"
android:layout_height="1dp"/>
<TextView
android:textColor="#fff"
android:padding="5dp"
android:layout_width="match_parent"
android:text="测试一"
android:layout_height="wrap_content"/>
<View
android:layout_width="match_parent"
android:background="#ccc"
android:layout_height="1dp"/>
<TextView
android:textColor="#fff"
android:padding="5dp"
android:layout_width="match_parent"
android:text="测试一"
android:layout_height="wrap_content"/>
</LinearLayout>
在values/style中:
<!-- PopupWindow动画 -->
<style name="PopAnimation">
<item name="android:windowEnterAnimation">@anim/pop_enter</item>
<item name="android:windowExitAnimation">@anim/pop_exit</item>
</style>
在res/anim文件下创建进入退出动画
pop_enter.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="400"
android:fromXDelta="100%p"
android:interpolator="@android:interpolator/overshoot"
android:toXDelta="0" />
<alpha
android:duration="400"
android:fromAlpha="0.2"
android:toAlpha="1.0" />
</set>
pop_exit.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="400"
android:fromXDelta="0"
android:interpolator="@android:interpolator/anticipate"
android:toXDelta="100%p" />
<alpha
android:duration="400"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
效果图:
