Android 自定义PopWindow的简单使用

本文详细介绍了如何在Android应用中自定义并使用PopWindow。包括创建自定义布局、设置宽高及位置、自定义动画效果等步骤。

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

下面用一个简单的自定义布局来讲解PopWindow的使用

先看效果图:

这里写图片描述

1.popwidow的类实现:
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.PopupWindow;

public class MyPopWindow extends PopupWindow implements View.OnClickListener{

    private float density = 1.0f;
    private Context mContext;
    public MyPopWindow(Context context) {
        mContext=context;
        initPopupWindow();
        View view = View.inflate(context, R.layout.pop_window, null);
        setContentView(view);
        //设置popwindow的宽高,这个数字是多少就设置多少dp,注意单位是dp
        setHeight((int) (160*density));
        setWidth((int) (100*density));
    }

    //初始化popwindow
    private void initPopupWindow() {
        setAnimationStyle(R.style.popwindowAnim);//设置动画
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new BitmapDrawable());
        density = mContext.getResources().getDisplayMetrics().density;//
    }

    /**
     * 显示popupWindow
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
        } else {
            this.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        //自己设置点击事件。。。
    }
}
2.自定义popwindow的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="110dp"
    android:orientation="vertical"
    android:background="@drawable/up_and_down_wheat"
    android:layout_height="160dp">

    <LinearLayout
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_width="100dp"
        android:orientation="horizontal"
        android:layout_height="50dp">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:src="@drawable/women"
            android:layout_height="30dp" />
        <TextView
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_width="50dp"
            android:textSize="15sp"
            android:text="女生"
            android:layout_height="50dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_marginLeft="10dp"
        android:layout_width="100dp"
        android:orientation="horizontal"
        android:layout_height="50dp">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:src="@drawable/man"
            android:layout_height="30dp" />
        <TextView
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_width="50dp"
            android:textSize="15sp"
            android:text="男生"
            android:layout_height="50dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_marginLeft="10dp"
        android:layout_width="100dp"
        android:orientation="horizontal"
        android:layout_height="50dp">
        <ImageView
            android:layout_gravity="center"
            android:layout_width="30dp"
            android:src="@drawable/money"
            android:layout_height="30dp" />
        <TextView
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_width="50dp"
            android:textSize="15sp"
            android:text="金币"
            android:layout_height="50dp" />
    </LinearLayout>

</LinearLayout>
3.自定义popwindow的动画

(1)自定义style

    <style name="popwindowAnim">
        <item name="android:windowEnterAnimation">@anim/fade_in</item>
        <item name="android:windowExitAnimation">@anim/fade_out</item>
    </style>

(2)自定义入场动画

<?xml version="1.0" encoding="utf-8"?>
<!-- 左上角扩大-->
<scale   xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="0.001"
    android:toXScale="1.0"
    android:fromYScale="0.001"
    android:toYScale="1.0"
    android:pivotX="100%"
    android:pivotY="10%"
    android:duration="200" />

(2)自定义出场动画

<?xml version="1.0" encoding="utf-8"?>
<!-- 左上角缩小 -->
<scale   xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="1.0"
    android:toXScale="0.001"
    android:fromYScale="1.0"
    android:toYScale="0.001"
    android:pivotX="100%"
    android:pivotY="10%"
    android:duration="200" />
4.最后调用
public class MainActivity extends Activity {
    private Context mContext;
    private Button add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        final Button button=findViewById(R.id.btn);
        mContext=this;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyPopWindow myPopWindow=new MyPopWindow(mContext);
                myPopWindow.showPopupWindow(button);
            }
        });
    }

}

总结

1.调用直接实现这个方法然后调用showPopupWindow(view);
2.设置popwindow的高宽

density = mContext.getResources().getDisplayMetrics().density;//
setHeight((int) (160*density));
setWidth((int) (100*density));

3.自定义出入场动画需要自己了解
4.设置popwindow的出现位置(在实现showPopupWindow方法中)

this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
this.showAsDropDown(相对于哪个控件弹出, xoff(相对于这个控件x轴的位移), yoff(相对于这个控件y轴的位移));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

keyboy_rl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值