Android 仿微信popuwindow弹窗,调用只需要一个方法,以及4.4系统popuwindow外部不响应解决办法

对于popuwindow大家都熟悉,他给Dialog的区别就是外部可以点击消失,一些用户提示、用户指引基本上用的都是这个控件,我用这个控件实现了微信分享的下部弹窗,效果挺好,使用起来也比较流畅,布局样式,都可以根据自己的需求进行更改,很不错的一个方法,希望能帮到大家,下面给大家展示一下效果图,看看是不是大家所需要的。

点击前:


点击后:


popu_window.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:id="@+id/pop_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        >
        <ImageView
            android:id="@+id/mImg2"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:layout_marginBottom="5dp"
            android:src="@mipmap/ic_launcher" />
        <View
            android:layout_width="1dip"
            android:layout_height="match_parent"
            android:background="#C5C4C4"
            android:layout_gravity="center_horizontal"
            />
        <ImageView
            android:id="@+id/mImg1"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical">
<TextView
    android:id="@+id/mTv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="点我弹窗"
    />
    <View
        android:id="@+id/mView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></View>

</RelativeLayout>

主界面实现代码:

MainActivity

import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView mTv;
    // 声明PopupWindow
    private PopupWindow popupWindow;
    // 声明PopupWindow对应的视图
    private View popupView;

    // 声明平移动画
    private TranslateAnimation animation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTv=findViewById(R.id.mTv);
        mTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initPopuWindow();
            }
        });
    }
    /**
     * 弹出popupWindow更改头像
     */
    private void initPopuWindow() {
        if (popupWindow == null) {
            popupView = View.inflate(this, R.layout.popu_window, null);
            // 参数2,3:指明popupwindow的宽度和高度
            popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT);

            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {

                }
            });

            // 设置背景图片, 必须设置,不然动画没作用
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setFocusable(true);

            // 设置点击popupwindow外屏幕其它地方消失
            popupWindow.setOutsideTouchable(true);

            // 平移动画相对于手机屏幕的底部开始,X轴不变,Y轴从1变0
            animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                    Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0);
            animation.setInterpolator(new AccelerateInterpolator());
            animation.setDuration(200);

            popupView.findViewById(R.id.mImg1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            popupView.findViewById(R.id.mImg2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }

        // 在点击之后设置popupwindow的销毁
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();

        }
        // 设置popupWindow的显示位置,此处是在手机屏幕底部且水平居中的位置
        popupWindow.showAtLocation(MainActivity.this.findViewById(R.id.mView), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        popupView.startAnimation(animation);
    }
}
 
本来写了一个popuwindow,好方便啊,一个方法就解决,真是好开心好开心,然而并不是那样,我老大拿出了一个历史
悠久的古老手机,本来手机运行的好好的,而他那手机不能点击了,这个急死我了,我试过了很多方法,比如加最外面
的点击事件,都无法相应,焦点什么的都不让popuwindow获取都不行,浪费我一两个小时,时各种方法,最后解决了。

当时好想说一句还有谁

pop.getContentView().setOnTouchListener(new View.OnTouchListener(){
//本来直接用onTouchListener使用也是没相应的,必须在getContentView();里面获取才可以实现点击
        
         public boolean onTouch(View v, MotionEvent event){
                pop.dismiss();
                return true;
           }
 });
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值