参考:
http://blog.csdn.net/ztp800201/article/details/13094451
一.主页面的布局
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.example.administrator.popwindowdemo.MainActivity">
<Button
android:onClick="start"
android:text="popWindow!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
popwindow的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pop_window_bg_selector"
android:orientation="horizontal" >
<TextView
android:id="@+id/pop_window_unistall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_uninstall"
android:text="卸载" />
<TextView
android:id="@+id/pop_window_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:drawableTop="@drawable/ic_open"
android:text="打开" />
<TextView
android:id="@+id/pop_window_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:drawableTop="@drawable/ic_share"
android:text="分享" />
<TextView
android:id="@+id/pop_window_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:drawableTop="@drawable/ic_info"
android:text="信息" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private PopupWindow mPopupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view){
//加载popWindow的布局
View root =View.inflate(this,R.layout.pop_window,null);
//创建popwindow的对象,绑定布局,根据父容器,指定popwindow的大小
mPopupWindow = new PopupWindow(root, RelativeLayout.LayoutParams.WRAP_CONTENT,40);
//拿到popwindow布局文件中的控件
TextView uninstall = (TextView) root.findViewById(R.id.pop_window_unistall);
//设置该控件的点击事件
uninstall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "卸载了", Toast.LENGTH_SHORT).show();
mPopupWindow.dismiss();
}
});
//设置popWindow弹出的动画效果
mPopupWindow.setAnimationStyle(R.style.AnimationFade);
//设置点击外部区徐,popWindow消失
mPopupWindow.setFocusable(true);
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable());
//设置popwindow在父容器中显示的位置
mPopupWindow.showAtLocation(view, Gravity.CENTER,0,0);
}
}
值得主页的是点击外部弹出窗口
mPopupWindow.setFocusable(true); mPopupWindow.setOutsideTouchable(true); mPopupWindow.setBackgroundDrawable(new ColorDrawable());
设置动画
在anim设置两个进入和出去的动画
进入的动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%"
android:toXDelta="0"
android:duration="500"
/>
</set>
出去动画
<?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%"
></translate>
</set>
在style文件中管理动画
<style name="AnimationFade"> <!-- PopupWindow左右弹出的效果 --> <item name="android:windowEnterAnimation">@anim/int_animation</item> <item name="android:windowExitAnimation">@anim/out_animation</item> </style>
源码下载