Android developers use Popwindow

Android development is the use of Popwindow

Learning From: Baidu

Overview

Today, nothing to do, search using Android wave of pop, and then gave me a push Android Popwindow used to use. I looked at the effect of what I need to say. Then I learned a bit, but not very well in the development process, I have been in front of the crash, flash back. But now finally to get.

How to use Popwindow

First of all we need to know it is that we are Popwindownot a control, he is similar AlertDialog.
Then we look at the layout of the text file we need to show:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/MainColor"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World Hello Android"
        android:textColor="#fff"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

So that our layout file is complete, then let me see, how to use our Popwindow.

PopupWindow popupWindow;

void InitPOP() {
    View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.my_information, null);
    if (popupWindow == null) {
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setHeight(400);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    }
    view.setOnClickListener(v -> {
        popupWindow.dismiss();
    });
    if (popupWindow.isShowing()) {
        popupWindow.dismiss();
    } else
        popupWindow.showAtLocation(LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null), Gravity.BOTTOM, 0, 0);
}

So that our Popwindow now created, where I need him out of the display is to show the bottom of the form. But just him, so I added from the bottom of the display where the display.
Then is when we use Popwindow, we often we go to set two properties:

popupWindow.setTouchable(true);
popupWindow.setFocusable(true);

So that our Popwindow setting is finished.
Is very simple.

Guess you like

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