仿微信右上角点击加号弹出PopupWindow

一。要弹出的布局,随便设计

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/my_phone">


    <TextView
        android:id="@+id/complain_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginTop="@dimen/sky_dp_size_10"
        android:layout_marginLeft="10dp"
        android:gravity="center"
        android:text="投诉"
        android:layout_marginRight="10dp"
        android:textColor="@android:color/black"
        android:textSize="18sp" />
    <TextView
        android:id="@+id/expect_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="@dimen/sky_dp_size_10"
        android:gravity="center"
        android:text="屏蔽"
        android:layout_marginRight="10dp"
        android:textColor="@android:color/black"
        android:textSize="18sp" />
</LinearLayout>

二。自定义Popubwindow继承Popubwindow

public class PopWinShare extends PopupWindow {
    private View mainView;
    private TextView layoutShare, layoutCopy;

    public PopWinShare(Activity paramActivity, View.OnClickListener paramOnClickListener, int paramInt1, int paramInt2) {
        super(paramActivity);
        //窗口布局
        mainView = LayoutInflater.from(paramActivity).inflate(R.layout.popwin_share, null);
        //分享布局
        layoutShare = ((TextView) mainView.findViewById(R.id.complain_tv));
        //复制布局
        layoutCopy = (TextView) mainView.findViewById(R.id.expect_tv);
        //设置每个子布局的事件监听器
        if (paramOnClickListener != null) {
            layoutShare.setOnClickListener(paramOnClickListener);
            layoutCopy.setOnClickListener(paramOnClickListener);
        }
        setContentView(mainView);
        //设置宽度
        setWidth(paramInt1);
        //设置高度
        setHeight(paramInt2);
        //设置显示隐藏动画
        setAnimationStyle(R.style.AnimTools);
        //设置背景透明
        setBackgroundDrawable(new ColorDrawable(0));
    }

}
三。弹出动画样式
<style name="AnimTools" parent="@android:style/Animation">
    <item name="android:windowEnterAnimation">@anim/push_in</item>
    <item name="android:windowExitAnimation">@anim/push_out</item>
</style>
(1)新建anim文件新建push_in和push_out
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.0"
    android:toXScale="1.0"
    android:fromYScale="0"
    android:toYScale="1.0"
    android:pivotX="0"
    android:pivotY="10%"
    android:duration="200" >

</scale>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.0"
    android:toXScale="1.0"
    android:fromYScale="1.0"
    android:toYScale="0"
    android:pivotX="0"
    android:pivotY="10%"
    android:duration="200" >

</scale>
四。使用
 if (popWinShare == null) {
                    //自定义的单击事件
                    OnClickLintener paramOnClickListener = new OnClickLintener();
                    popWinShare = new PopWinShare(PersonalHomepageActivity.this, paramOnClickListener, 120, 130);
                    //监听窗口的焦点事件,点击窗口外面则取消显示
                    popWinShare.getContentView().setOnFocusChangeListener(new View.OnFocusChangeListener() {

                        @Override
                        public void onFocusChange(View v, boolean hasFocus) {
                            if (!hasFocus) {
                                popWinShare.dismiss();
                            }
                        }
                    });
                }
//设置默认获取焦点
                popWinShare.setFocusable(true);
//以某个控件的x和y的偏移量位置开始显示窗口
                popWinShare.showAsDropDown(tousuTv, 0, 0);
//如果窗口存在,则更新
                popWinShare.update();
(1)按钮的点击事件
class OnClickLintener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complain_tv:
                Intent intent = new Intent(PersonalHomepageActivity.this, ComplainActivity.class);
                intent.putExtra("about_user_id", friend_id);
                startActivity(intent);
                break;
            case R.id.expect_tv:
                getExpect();
                break;

            default:
                break;
        }

    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。 要实现类似微信聊天消息长按弹出菜单的效果,可以使用 PopupWindow 组件来实现。下面是实现的大致步骤: 1. 在布局文件中定义一个包含要弹出的菜单项的布局文件,例如一个 LinearLayout。 2. 在代码中创建 PopupWindow 对象,并设置其内容为上一步中定义的布局文件。 3. 设置 PopupWindow 的大小、位置、背景等属性。 4. 监听长按事件,在长按事件中弹出 PopupWindow。 5. 监听 PopupWindow 中菜单项的点击事件,处理相应的逻辑。 具体实现细节可以参考以下代码: ``` // 定义 PopupWindow PopupWindow popupWindow = new PopupWindow(context); View contentView = LayoutInflater.from(context).inflate(R.layout.menu_layout, null); popupWindow.setContentView(contentView); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 监听长按事件 view.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { // 显示 PopupWindow int[] location = new int[2]; v.getLocationOnScreen(location); popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] - popupWindow.getHeight()); return true; } }); // 监听菜单项的点击事件 TextView menuItem = contentView.findViewById(R.id.menu_item); menuItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理菜单项的逻辑 popupWindow.dismiss(); } }); ``` 希望这个回答能够帮到你,如果还有其他问题,可以继续问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值