一、popupWindow的常用方法

  1. 1.setContentView(View contentView): 设置PopupWindow显示的View<br /> 2.showAsDropDown(View anchor): 相对某个控件的位置(正左下方),无偏移<br /> 3.showAsDropDown(View anchor,int xoff,int yoff): 相对某个控件的位置,有偏移<br /> 4.setFocusable(boolean focusable) 设置是否获取焦点<br /> 5.setBackgroundDrawable(Drawable background) 设置背景<br /> 6.dismiss() 关闭弹窗<br /> 7.setAnimationStyle(int animationStyle) 设置加载动画<br /> 8.setTouchable(boolean touchable) 设置触摸使能<br /> 9.setOutsideTouchable(boolen touchable) 设置PopupWindow外面的触摸使能

二、popupWindow弹出窗口的实现

  1. 1.布局activity_main.xml代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <Button
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="弹出popupwindow"
  12. android:onClick="btn_one"
  13. />
  14. </LinearLayout>
  1. 2.自定义弹窗布局popup_view.xml代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="#ffff00"
  8. android:orientation="vertical">
  9. <Button
  10. android:id="@+id/btn_hangzhou"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="杭州"
  14. />
  15. <Button
  16. android:id="@+id/btn_xiamen"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="厦门"
  20. />
  21. </LinearLayout>
  1. 3.MainActivity文件代码
  1. package com.example.mypopupwindow;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.Button;
  7. import android.widget.PopupWindow;
  8. import android.widget.Toast;
  9. public class MainActivity extends AppCompatActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. }
  15. public void btn_one(View view) {
  16. //2.创建popupView布局
  17. View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
  18. Button btn_hangzhou = popupView.findViewById(R.id.btn_hangzhou);
  19. Button btn_xiamen=popupView.findViewById(R.id.btn_xiamen);
  20. //1.创建popupWindow实例,将popupView布局放进去,自适应宽高
  21. final PopupWindow popupWindow = new PopupWindow(popupView,
  22. ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
  23. //4.设置背景色
  24. popupWindow.setBackgroundDrawable(getResources().getDrawable(
  25. R.drawable.ic_launcher_background));
  26. //3.显示popupWindow,显示在按钮下方
  27. popupWindow.showAsDropDown(view);
  28. //5.设置按钮点击
  29. btn_hangzhou.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. Toast.makeText(MainActivity.this, "你是住在杭州吗", Toast.LENGTH_SHORT).show();
  33. //6.取消弹窗
  34. popupWindow.dismiss();
  35. }
  36. });
  37. btn_xiamen.setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View v) {
  40. Toast.makeText(MainActivity.this, "你是住在厦门吗", Toast.LENGTH_SHORT).show();
  41. }
  42. });
  43. }
  44. }
  1. 4.效果图:<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1622015319715-238fb331-5840-482c-897f-98737a2f3fd4.png#clientId=ubdd8c49e-8800-4&from=paste&height=439&id=u9d7b62a7&margin=%5Bobject%20Object%5D&name=image.png&originHeight=439&originWidth=776&originalType=binary&size=66450&status=done&style=none&taskId=u29b54904-4e01-42db-b103-5b9b1e86346&width=776)