平常的popwindow没有实现跟dialog的效果,就是窗口周围还是白色的,这样不能突显出窗口,所以我们决定做一个跟dialog一样的popwindow.实现的原理其实就是在activity的布局上方放一个LinearLayout背景设置成半透明,在平常状态下不显示整个空白控件,单弹出窗口的时候就显示这个控件,窗口关闭的时候就隐藏这个控件。
Activity的布局xml 很简单就是一个用于显示的按钮和一个LinearLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
Activity的布局xml 很简单就是一个用于显示的按钮和一个LinearLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/popWindowBtn"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="@string/action_settings"/>
<LinearLayout
android:id="@+id/layout_all"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90000000"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
</RelativeLayout>
自定义PopWindow.java
R.style.WindowStyle.主要是用于弹出和收起这个窗口的动画风格
anim/pop_down_out.xml
自定义PopWindow.java
package com.example.popwindowdemo;
/**
* @author xu
* 2015/6/5
* 自定义popwindow
*/
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
public class MyPopWindow implements OnDismissListener{
private Context mContext;
private PopupWindow mPopupWindow;
private OnPopListener onPopListener;
public MyPopWindow(Context context) {
// TODO Auto-generated constructor stub
this.mContext = context;
View view = LayoutInflater.from(context).inflate(R.layout.layout_popwindow, null);
mPopupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mPopupWindow.setAnimationStyle(R.style.WindowStyle);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mPopupWindow.setOnDismissListener(this);
}
//向外部暴露接口
public void setOnPopListener(OnPopListener onPopListener){
this.onPopListener = onPopListener;
}
//关掉这个窗口
public void ondismiss(){
if(mPopupWindow != null)
mPopupWindow.dismiss();
}
/*设置窗口的位置*/
public void setWindPosition(View parent){
if(mPopupWindow != null)
{
mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.update();
}
}
@Override
public void onDismiss() {
// TODO Auto-generated method stub
onPopListener.onLisener();
}
}
<style name="WindowStyle" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/pop_up_in</item>
<item name="android:windowExitAnimation">@anim/pop_down_out</item>
</style>
anim/pop_up_in.xml
<?xml version="1.0" encoding="utf-8"?>
anim/pop_up_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="100%"
android:toYDelta="0.0"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0.0"
android:toYDelta="100%"
android:duration="200"/>
</set>