Android学习08

PopupWindow

PopupWindow用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。

1、弹出框的布局:画一个PopupWindow的布局文件

layout_pop.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_good"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#3CA9C4"
        android:text="好"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#3CA9C4"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#3CA9C4"
        android:text="一般"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#3CA9C4"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#3CA9C4"
        android:text="不好"
        android:gravity="center"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"/>

</LinearLayout>
View Code

2、activity_popup_window.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:orientation="vertical">

    <Button
        android:id="@+id/btn_pop"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="POP"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"/>

</LinearLayout>
View Code

2、Activity的布局中只有一个按钮,按下后会弹出框,PopupWindowActivity代码如下:

package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class PopupWindowActivity extends AppCompatActivity {

    private Button mBtnPop;
    private PopupWindow mPop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popup_window);

        mBtnPop = findViewById(R.id.btn_pop);
        mBtnPop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = getLayoutInflater().inflate(R.layout.layout_pop,null);
                TextView textView = view.findViewById(R.id.tv_good);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPop.dismiss();
                        Toast.makeText(PopupWindowActivity.this, "你选择了好", Toast.LENGTH_SHORT).show();
                    }
                });
                mPop = new PopupWindow(view,mBtnPop.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                mPop.setOutsideTouchable(true);//这里设置显示PopuWindow之后在外面点击是否有效。如果为false的话,那么点击PopuWindow外面并不会关闭PopuWindow
                mPop.setFocusable(true);
                mPop.showAsDropDown(mBtnPop,0,0);//设置显示PopupWindow的位置位于View的左下方,x,y表示坐标偏移量
            }
        });
    }
}
View Code

当有mPop.setFocusable(false);的时候,说明PopuWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失,但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。当设置为popuWindow.setFocusable(true);的时候,加上设置背景代码,点击外面和Back键才会消失。

mPop.setFocusable(true);
需要顺利让PopUpWindow dimiss(即点击PopuWindow之外的地方此或者back键PopuWindow会消失);PopUpWindow的背景不能为空。必须在popuWindow.showAsDropDown();或者其它的显示PopuWindow方法之前设置它的背景不为空;

原文地址:https://www.cnblogs.com/xjmm/p/12284571.html