popupwindow弹出时从下往上,隐藏时从上往下,并且显示在指定控件上方
View inflate = LayoutInflater.from(getContext()).inflate(R.layout.popupwindow_shop, null);
LinearLayout pop_del = inflate.findViewById(R.id.pop_shop_delete);
ListView lst = inflate.findViewById(R.id.pop_shop_lst);
window = new PopupWindow(inflate, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
window.setAnimationStyle(R.style.pop_shop_anim);
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
window.setBackgroundDrawable(dw);
window.setOutsideTouchable(true);
showUp(inflate);
window.update();
private void showUp(View v) {
v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int measuredHeight = v.getMeasuredHeight();
int[] location = new int[2];
linear.getLocationOnScreen(location);
window.showAtLocation(linear, Gravity.NO_GRAVITY, 0, location[1] - measuredHeight);
}
popupwindow_shop.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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#bfbfbf"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/pop_shop_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/px10_dp"
android:layout_marginLeft="@dimen/px20_dp"
android:layout_marginTop="@dimen/px10_dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="@dimen/px10_dp"
android:src="@drawable/lajixiang" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qingkong"
android:textSize="@dimen/px20_sp"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
<ListView
android:id="@+id/pop_shop_lst"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
动画:(在res下创建名为anim的文件夹,在anim文件夹下创建文件)
pop_show
当值设置为"50"时,表示使用绝对位置定位
当值设置为"50%"时,表示使用相对于控件本身的一半定位
当值设置为"50%P"时,表示使用相对于父控件的一半定位
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
pop_hidden<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
在values-styles下添加
<style name="pop_shop_anim">
<!-- 显示 -->
<item name="android:windowEnterAnimation">@anim/pop_show</item>
<!-- 消失 -->
<item name="android:windowExitAnimation">@anim/pop_hidden</item>
</style>