PopupWindow of Android floating window

Overview

Today, a function is developed, which is to click on the avatar. The user can choose what to do with it, but we need to use the floating window to complete. At the beginning, I used AlertDialog for development, but the effect was relatively ugly, so I switched to PopupWindow for operation. I used to do this before, but I have never taken notes.

What's PopupWindow

This class represents a popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.

How should we use our PopupWindow elegantly?

According to international practice, we first look at your code:

item_layout.xml PopupWindow layout page

 <?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">

     <Button
         android:id="@+id/btn_xixi"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:padding="5dp"
         android:text="Hello"
         android:textSize="18sp" />

     <Button
         android:id="@+id/btn_hehe"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:padding="5dp"
         android:text="World"
         android:textSize="18sp" />
 </LinearLayout>

This is also very simple, we simply put two Buttons.
Here I use the simplest way without any animation operation.
Then we look at our logic code.

public class MainActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.textView = this.findViewById(R.id.main_TextView);
        this.textView.setOnClickListener(view -> initPopupWindow(view));
    }
    void initPopupWindow(View v) {
        //创建内容显示View
        View view = LayoutInflater.from(this).inflate(R.layout.item_popip, null, false);
        Button btn1 = view.findViewById(R.id.btn_xixi);
        Button btn2 = view.findViewById(R.id.btn_hehe);
        //构造一个PopupWindow,参数依次是加载的View,宽,高
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //添加点击PopupWindow外部PopupWindow自动消失
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });
        //设置PopupWindow的显示位置,这里的参数分别是,参照View,X轴的偏移量,Y轴的偏移量
        popupWindow.showAsDropDown(v, 50, 0);
        //设置PopupWindow的View的点击效果
        btn1.setOnClickListener(view12 -> Toast.makeText(MainActivity.this, "这是一个", Toast.LENGTH_SHORT).show());
        btn2.setOnClickListener(view1 -> Toast.makeText(MainActivity.this, "PopupWindow", Toast.LENGTH_SHORT).show());
    }
}

This part is all our code.
Next, let's take a look at what our logic is to achieve:
First, we click on a TextView to display the PopupWindow floating window.
Then we proceed through initPopupWindow.
The v in the method is our display reference, and the View in the method is the displayed content.
The following operations have more detailed comments in the code, you can take a look at yourself.

Guess you like

Origin www.cnblogs.com/cao-1/p/12680483.html