绝密Android弹框popuwindow同时屏幕背景变暗 Android工程师
Android弹框popuwindow同时屏幕背景变暗
最近做项目,希望有个弹框并且能让背景在弹框出来时背景变暗,popuwindow显示变亮,当弹框消失的时候背景变亮,现在我们来看下代码。
主页面布局只添加了一个Button,然后设置Button的点击事件来弹出PopupWindow。
PopupWindow布局添加了一个TextView和三个Button,代码如下:
<?xml version="1.0" encoding="utf-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:layout_width="wrap_content"
04. android:layout_height="wrap_content"
05. android:orientation="vertical" >
07. <TextView
08. android:id="@+id/textView1"
09. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:layout_margin="10dp"
12. android:text="这是一个PopupWindow" />
14. <Button
15. android:id="@+id/bt_item1"
16. android:layout_width="match_parent"
17. android:layout_height="wrap_content"
18. android:layout_margin="10dp"
19. android:text="Item1" />
21. <Button
22. android:id="@+id/bt_item2"
23. android:layout_width="match_parent"
24. android:layout_height="wrap_content"
25. android:layout_margin="10dp"
26. android:text="Item2" />
28. <Button
29. android:id="@+id/bt_item3"
30. android:layout_width="match_parent"
31. android:layout_height="wrap_content"
32. android:layout_margin="10dp"
33. android:text="Item3" />
35.</LinearLayout>
最后,我们还需要在res/values文件夹下的style.xml文件里添加如下代码,用来设置PopupWindow显示/隐藏的动画效果
<!-- PopupWindow弹出/隐藏动画 -->
02. <style name="MyPopupWindow_anim_style">
03. <item name="android:windowEnterAnimation">@anim/popupwindow_show_anim</item>
04. <item name="android:windowExitAnimation">@anim/popupwindow_hidden_anim</item>
05. </style>
MainActivity代码,主要是设置PopupWindow的一些参数,具体见注释:
public class MainActivity extends Activity {
03. private Button bt;
05. @Override
06. protected void onCreate(Bundle savedInstanceState) {
07. super.onCreate(savedInstanceState);
08. setContentView(R.layout.activity_main);
09.
10. bt = (Button) findViewById(R.id.button1);
11. bt.setOnClickListener(new OnClickListener() {
13. @Override
14. public void onClick(View v) {
15. showpopupWindow(v);// 显示PopupWindow
16. }
17. });
18. }
20. @SuppressLint("InlinedApi")
21. private void showpopupWindow(View v) {
22. Button btItem1, btItem2, btItem3;
24. LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
25. View view = layoutInflater.inflate(R.layout.popwindow_layout, null);
27. final PopupWindow popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
28. // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
29. popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popupwindow_background));
30. popupWindow.setOutsideTouchable(true);
31. popupWindow.setAnimationStyle(R.style.MyPopupWindow_anim_style);
33. btItem1 = (Button) view.findViewById(R.id.bt_item1);
34. btItem1.setOnClickListener(new OnClickListener() {
35. @Override
36. public void onClick(View v) {
37. Toast.makeText(MainActivity.this, "Item1被点击", Toast.LENGTH_SHORT).show();
38. popupWindow.dismiss();
39. }
40. });
41. btItem2 = (Button) view.findViewById(R.id.bt_item2);
42. btItem2.setOnClickListener(new OnClickListener() {
43. @Override
44. public void onClick(View v) {
45. Toast.makeText(MainActivity.this, "Item2被点击", Toast.LENGTH_SHORT).show();
46. popupWindow.dismiss();
47. }
48. });
49. btItem3 = (Button) view.findViewById(R.id.bt_item3);
50. btItem3.setOnClickListener(new OnClickListener() {
51. @Override
52. public void onClick(View v) {
53. Toast.makeText(MainActivity.this, "Item3被点击", Toast.LENGTH_SHORT).show();
54. popupWindow.dismiss();
55. }
56. });
58. // PopupWindow弹出位置
59. popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
60.
61. backgroundAlpha(0.5f);
62. popupWindow.setOnDismissListener(new OnDismissListener() {
63.
64. @Override
65. public void onDismiss() {
66. backgroundAlpha(1f);
67. }
68. });
69. }
71. // 设置屏幕透明度
72. public void backgroundAlpha(float bgAlpha) {
73. WindowManager.LayoutParams lp = getWindow().getAttributes();
74. lp.alpha = bgAlpha; // 0.0~1.0
75. getWindow().setAttributes(lp);
76. }
78.}


1914篇文章