android popupwindow从下弹出,显示在某控件上方

本文介绍了如何在Android中实现PopupWindow从下方弹出并在特定控件上方显示的动画效果。通过在anim文件夹下创建动画资源,设置不同定位方式如绝对位置、相对控件一半和相对父控件一半,实现灵活的PopupWindow展示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值