Android 自定义Popwindow

前言

没事帮朋友写一个Popwindow本来以为是很简单是事,但是做的时候遇到很多问题(差点气的我吐血)所以写篇博客记录一下,也算是给自己手高眼低的一个教训。
这里写图片描述

看到UI的时候基本上就知道怎么实现了:1, 自定义一个Popwindow的布局 2,按钮的逻辑 然后以为基本上就搞定了。
但是自己的大意忘记了Popwindow常见的几个坑:一定要设置背景颜色不然他会遮住全部让你获取不到其他控件的焦点

 popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.a));

如果你想要点击外面弹框不消失的话不要忘记加上:

 popupWindow.setFocusable(false);

另外就是当你给Popwindow里面的控件设置点击事件的时候:

implements View.OnClickListener//是做不到的因为你Popwindow没有获取到Activity的实例而是根据你TextView

还有就是你flag值的设置,当你点击弹框消失的时候记得把

flag=0

下面基本上就没什么大问题,贴一下代码 :
pop.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="140px"
    android:layout_height="258px"
    android:background="@mipmap/a"
    android:orientation="vertical"
    >
    <TextView
        android:layout_marginTop="6dp"
        android:id="@+id/one"
        android:layout_width="match_parent"
        android:layout_height="80px"
        android:textSize="15sp"
        android:text="全部"
        android:gravity="center"
        android:textColor="#222"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:background="#68746869"
        />
    <TextView
        android:id="@+id/two"
        android:layout_width="match_parent"
        android:layout_height="80px"
        android:textSize="15sp"
        android:text="买入"
        android:gravity="center"
        android:textColor="#222"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#68746869"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        />
    <TextView
        android:id="@+id/three"
        android:layout_width="match_parent"
        android:layout_height="80px"
        android:textSize="15sp"
        android:text="卖出"
        android:gravity="center"
        android:textColor="#222"
        />
</LinearLayout>
</LinearLayout>

PopActivity

public class PopActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView textView;
    private TextView one;
    private TextView two;
    private TextView three;
    private PopupWindow popupWindow;
    private int flag=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop);
        textView=findViewById(R.id.type);
        View inflate= LayoutInflater.from(this).inflate(R.layout.pop,null,false);
        popupWindow = new PopupWindow(inflate, 140, 258, true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.a));
        popupWindow.setFocusable(false);


        one=inflate.findViewById(R.id.one);
        two=inflate.findViewById(R.id.two);
        three=inflate.findViewById(R.id.three);

        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("全部");
                one.setTextColor(Color.parseColor("#52b9a8"));
                two.setTextColor(Color.parseColor("#222222"));
                three.setTextColor(Color.parseColor("#222222"));
                popupWindow.dismiss();
                flag=0;
            }
        });

        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("买入");
                one.setTextColor(Color.parseColor("#222222"));
                two.setTextColor(Color.parseColor("#52b9a8"));
                three.setTextColor(Color.parseColor("#222222"));
                popupWindow.dismiss();
                flag=0;
            }
        });

        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("卖出");
                one.setTextColor(Color.parseColor("#222222"));
                two.setTextColor(Color.parseColor("#222222"));
                three.setTextColor(Color.parseColor("#52b9a8"));
                popupWindow.dismiss();
                flag=0;
            }
        });

        textView.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.type:
                if (flag==0){
                    popupWindow.showAsDropDown(v, -5, 0);
                    flag=1;
                }else {
                    popupWindow.dismiss();
                    flag=0;

                }
               break;
        }
    }


}

到这个这个弹窗就实现啦!
效果图:
这里写图片描述

这里给一下我用到的背景图
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qiaoshi96_bk/article/details/79794299
今日推荐