The basic use of PopupWindow (floating frame) of Android

Introduction to this section:

This section brings you the last UI control for displaying information—PopupWindow (floating box). If you want to know what it looks like, you can open QQ on your mobile phone, long press an item in the list, At this time, a small black dialog box pops up, this is PopupWindow, and the difference from the AlertDialog dialog box is that its position can be arbitrary;

In addition, AlertDialog is a non-blocking thread, while PopupWindow is a blocking thread! And the official has such a sentence to introduce PopupWindow:

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.

It roughly means: a pop-up window control that can be used to display any View, and will float on top of the current activity

Let's learn about this control~

Official documentation: PopupWindow


1. Interpretation of relevant methods


1) Several commonly used construction methods

We can see in the documentation that there are nine construction methods of PopupWindow provided to us, and here are only a few construction methods that are used more in actual development:

  • public PopupWindow (Context context)
  • public PopupWindow(View contentView, int width, int height)
  • public PopupWindow(View contentView)
  • public PopupWindow(View contentView, int width, int height, boolean focusable)

There is no need to explain the parameters, contentView is the View displayed by PopupWindow, whether focusable shows the focus


2) Some commonly used methods

Here are a few more commonly used methods, and others can be consulted in the documentation:

  • setContentView (View contentView): Set the View displayed by PopupWindow
  • getContentView (): Get the View displayed by PopupWindow
  • showAsDropDown(View anchor) : relative to the position of a control (just below the left), no offset
  • showAsDropDown(View anchor, int xoff, int yoff) : Relative to the position of a certain control, there is an offset
  • showAtLocation(View parent, int gravity, int x, int y) : Relative to the position of the parent control (such as the central Gravity.CENTER, the lower Gravity.BOTTOM, etc.), you can set offset or no offset PS: parent this parameter as long as It is the view in the activity!
  • setWidth/setHeight : Set the width and height, and you can also specify the width and height in the construction method. In addition to writing specific values, you can also use WRAP_CONTENT or MATCH_PARENT. The width and height properties of popupWindow directly correspond to the first layer of View.
  • setFocusable(true) : Set focus, after PopupWindow pops up, all touch screen and physical keys are handled by PopupWindows. The response to any other event must occur after the PopupWindow disappears (except for system-level events such as home). For example, when such a PopupWindow appears, press the back key first to make the PopupWindow disappear, and then press the second time to exit the activity. To be precise, if you want to exit the activity, you must first let the PopupWindow disappear, because it is not necessary to press the back PopupWindow under any circumstances. will disappear, it must be the background of the PopupWindow.
  • setAnimationStyle(int): set the animation effect

2. Use the code example

Running effect diagram :

Realize the key code :

First paste the animation file: anim_pop.xml :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="2000">
    </alpha>
</set> 

Next is the layout of the popupWindow: item_popip.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:background="@drawable/ic_pop_bg"
    android:orientation="vertical">

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

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

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button btn_show;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        btn_show = (Button) findViewById(R.id.btn_show);
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initPopWindow(v);
            }
        });
    }


    private void initPopWindow(View v) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_popup, null, false);
        Button btn_xixi = (Button) view.findViewById(R.id.btn_xixi);
        Button btn_hehe = (Button) view.findViewById(R.id.btn_hehe);
        //1. Construct a PopupWindow, the parameters are the loaded View, width and height in turn
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popWindow.setAnimationStyle(R.anim.anim_pop); //Set loading animation

        //In order to click on the non-PopupWindow area, the PopupWindow will disappear, if there is no following
        //In terms of code, you will find that when you display the PopupWindow, no matter how many times you press the back button
        //PopupWindow will not be closed, and the program cannot be exited, adding the following code can solve this problem
        popWindow.setTouchable(true);
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // If true is returned here, the touch event will be intercepted
                // After interception, the onTouchEvent of PopupWindow is not called, so clicking on the external area cannot dismiss
            }
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); //It is only valid to set a background for popWindow


        //Set the position displayed by the popupWindow, the parameters are the reference View, the offset of the x-axis, and the offset of the y-axis in turn
        popWindow.showAsDropDown(v, 50, 0);

        //Set the event of the button in popupWindow
        btn_xixi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "You clicked~", Toast.LENGTH_SHORT).show();
            }
        });
        btn_hehe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "You clicked~", Toast.LENGTH_SHORT).show();
                popWindow.dismiss();
            }
        });
    }
}

Guess you like

Origin blog.csdn.net/leyang0910/article/details/131175273