Popup Windows Usage Details

Strongly Recommend Articles: Welcome to Collection
Android Dry Goods Sharing

Read for five minutes, ten o'clock a day, and study with you for life. Here's Android, a programmer.

This article mainly introduces some knowledge points in Android development. By reading this article, you will gain the following contents:

  1. Popup Windows Inheritance
  2. Popup Windows Usage
  3. Implementation of Popup Windows at the Bottom of Popup Windows

PopupWindow Is a can inActivity Any display on itViewControl of. stayAndroidFrequently used, the effect followsDialog The effect is similar, but the difference is that you can control the position of the display, such as the bottom display.

1. PopupWindow brief introduction

PopupWindow The relationship of succession is as follows:

java.lang.Object
   ↳
     android.widget.PopupWindow

2. Usage

It mainly calls the construction method of PopWindow, transforms Layout into View through LayoutInflater, and passes View over. It can be realized by referring to the source code of PopupWindow. The source code path is as follows: framework/base/core/java/android/widget/PopupWindow.java.

3. Realization of bottom Popup Windows

    1. Popup Windows Implementation Effect

    1. PopWindow Implementation Class
public class PopWindowMethods extends Activity {
    private View mPopView;
    private PopupWindow mPopupWindow;
    private Button btn_pop_ok;
    private Button btn_pop_cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popwindow);

        InitPopWindow();
        InitView();
        InitClick();
    }

    /**
     * 
     */
    private void InitClick() {
        // TODO Auto-generated method stub
        btn_pop_ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "ok", 0).show();
            }
        });

        btn_pop_cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "cancel", 0).show();
            }
        });
    }

    /**
     * 
     */
    private void InitPopWindow() {
        // TODO Auto-generated method stub
        // Convert the layout file into a View object, popupview content view
        mPopView = getLayoutInflater().inflate(R.layout.popwindow_layout, null);
        // Place the converted View in a new popuwindow object
        mPopupWindow = new PopupWindow(mPopView,
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        // Click popuwindow to disappear
        mPopupWindow.setOutsideTouchable(true);
        // mpopupWindow.setBackgroundDrawable(background);

    }

    /**
     * 
     */
    private void InitView() {
        btn_pop_ok = (Button) mPopView.findViewById(R.id.btn_pop_ok);
        btn_pop_cancel = (Button) mPopView.findViewById(R.id.btn_pop_cancel);

    }

    @SuppressLint("NewApi")
    public void ShowPopWindow(View view) {
        if (mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        } else {
            // Set the bottom or drop-down of Popup Windows display, etc.
            // Display at a certain location
            mPopupWindow.showAtLocation(mPopView, Gravity.BOTTOM, 0, 30);
            // Display as a drop-down view
            // mPopupWindow.showAsDropDown(mPopView, Gravity.CENTER, 200, 300);
        }

        // Toast.makeText(
        // getApplicationContext(),
        // "Launcher:"
        // + PackageUtils.isLauncherAPK(getApplicationContext(),
        // "com.miui.home"),
        // 0).show();
    }
}
    1. Popup Windows Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    android:background="@drawable/popwindow_bg" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_pop_tittle"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:gravity="center"
            android:text="PopWindow Tittle" />

        <TextView
            android:id="@+id/tv_pop_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="left|center_horizontal"
            android:padding="20dp"
            android:text="Do you want to quit? Popwindows ? " />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_margin="3dp"
            android:background="@color/white_line" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_pop_ok"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="@drawable/custom_btn_white_selector"
                android:gravity="center"
                android:text="OK" />

            <Button
                android:id="@+id/btn_pop_cancel"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:layout_margin="10dp"
                android:layout_marginBottom="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="@drawable/custom_btn_white_selector"
                android:gravity="center"
                android:text="Cancel" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

So far, the use of PopWindow is basically over.

So far, this article is over. If there are any mistakes, you are welcome to make suggestions and corrections. At the same time look forward to your attention, thank you for reading, thank you!

Tags: Android Windows Java MIUI

Posted on Wed, 09 Oct 2019 05:54:54 -0700 by crouse