Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)

时间:2023-03-09 02:54:45
Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)

PopupWindow是一个可以用来显示一个任意的视图的弹出窗口,他需要完全依赖layout布局。

它没什么界面,在弹出的窗口中完全显示布局中的控件。

Android进阶2之PopupWindow弹窗(有点悬浮窗的感觉)

上面两个美女头就是弹窗PopupWindow显示的内容。是两个Button。

具体实现:

注意:那三个Button不能和普通的Button一样通过findViewById()方法获得,必须首先说的Button所在的视图,View popview = layoutInflater.inflate(R.layout.poplayout, null);

我的Button在poplayout.xml中。最后通过button1 = (Button) popview.findViewById(R.id.button1)获得。

另外一点就是:不要在oncreate()中获得Button,而是像我一样在onclick方法下获得,和popupwindow一起。这一点不一定正确。

为什么要提呢?因为我在oncreate()中获得Button时,button的点击事件不能用,我也不是很清楚。那位大牛要是知道的话,可以告诉我一下。

showAtLocation(findViewById(R.id.edit_layout), Gravity.BOTTOM,0, 0);

设置弹窗的位置:

第一个参数是弹窗父控件的布局;

第二个参数是位置如左,右,上部,下部等;

第三个参数是X方向的偏移量;

第四个参数是Y方向的偏移量。

  1. package xiaosi.popwindow;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.WindowManager.LayoutParams;
  9. import android.widget.Button;
  10. import android.widget.PopupWindow;
  11. import android.widget.Toast;
  12. public class PopwindowActivity extends Activity implements OnClickListener
  13. {
  14. /** Called when the activity is first created. */
  15. private Button button = null;
  16. private Button button1 = null;
  17. private Button button2 = null;
  18. private Button button3 = null;
  19. @Override
  20. public void onCreate(Bundle savedInstanceState)
  21. {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. button = (Button) findViewById(R.id.button);
  25. button.setOnClickListener(this);
  26. }
  27. public void onClick(View arg0)
  28. {
  29. if (arg0.getId() == R.id.button)
  30. {
  31. LayoutInflater layoutInflater = (LayoutInflater) (PopwindowActivity.this)
  32. .getSystemService(LAYOUT_INFLATER_SERVICE);
  33. // 获取自定义布局文件poplayout.xml的视图
  34. View popview = layoutInflater.inflate(R.layout.poplayout, null);
  35. PopupWindow popWindow = new PopupWindow(popview,
  36. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  37. //规定弹窗的位置
  38. popWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM,
  39. 0, 0);
  40. //PopupWindow里的两个Button
  41. button1 = (Button) popview.findViewById(R.id.button1);
  42. button1.setOnClickListener(this);
  43. button2 = (Button) popview.findViewById(R.id.button2);
  44. button2.setOnClickListener(this);
  45. }
  46. else if (arg0.getId() == R.id.button1)
  47. {
  48. Toast.makeText(PopwindowActivity.this, "button1", Toast.LENGTH_LONG)
  49. .show();
  50. }
  51. else if (arg0.getId() == R.id.button2)
  52. {
  53. Toast.makeText(PopwindowActivity.this, "button2", Toast.LENGTH_LONG)
  54. .show();
  55. }
  56. }
  57. }

poplayout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:gravity="center_horizontal"
  6. android:orientation="horizontal" >
  7. <Button
  8. android:id="@+id/button1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:background="@drawable/colorframe_1" />
  12. <Button
  13. android:id="@+id/button2"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:background="@drawable/colorframe_3" />
  17. </LinearLayout>

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/main"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical"
  7. android:background="@drawable/background">
  8. <Button
  9. android:id="@+id/button"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="点击查看效果" />
  13. <ImageView
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:src="@drawable/a" />
  17. </LinearLayout>

以上只是用来学习之用,拿出来和大家一起分享一下。

有想要源代码的给我留言。

http://blog.****.net/sjf0115/article/details/7339914