歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:Popupwindow的應用

Android開發教程:Popupwindow的應用

日期:2017/3/1 10:36:25   编辑:Linux編程

PopupWindow 是一種阻塞式的彈出窗口,這就意味著在我們退出這個彈出框之前,程序會一直等待。它可以浮動在當前Activity的任何的位置上。 需要注意的是,PopupWindow必須觸發某個焦點或者某個事件才會顯示出來,不然總會會出現錯誤。

下面是使用PopupWindow 彈出自定義菜單的例子

當我們點擊Menu鍵的時候,會在當前的Activity最下方彈出一個菜單。

效果圖如下:

650) this.width=650;">

PopupMenuDemo.java

  1. package com.lingdududu.popupWindow;
  2. import Android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.drawable.BitmapDrawable;
  5. import android.os.Bundle;
  6. import android.view.Gravity;
  7. import android.view.KeyEvent;
  8. import android.view.LayoutInflater;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.BaseAdapter;
  14. import android.widget.GridView;
  15. import android.widget.ImageView;
  16. import android.widget.LinearLayout;
  17. import android.widget.PopupWindow;
  18. import android.widget.TextView;
  19. import android.widget.LinearLayout.LayoutParams;
  20. public class PopupMenuDemo extends Activity {
  21. private int[] resArray = new int[] {
  22. R.drawable.icon_menu_addto, R.drawable.icon_menu_audioinfo,
  23. R.drawable.icon_menu_findlrc, R.drawable.icon_menu_scan
  24. };
  25. private String[] title = new String[]{
  26. "添加", "信息", "搜索", "查看"
  27. };
  28. private PopupWindow pw = null;
  29. /** Called when the activity is first created. */
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.main);
  34. }
  35. @Override
  36. public boolean onCreateOptionsMenu(Menu menu) {
  37. menu.add("");
  38. return super.onCreateOptionsMenu(menu);
  39. }
  40. /**
  41. * 在此方法中實現自定義菜單欄
  42. */
  43. public boolean onMenuOpened(int featureId, Menu menu) {
  44. if (pw != null) {
  45. if (pw.isShowing()) {
  46. // 就關閉彈出窗口
  47. pw.dismiss();
  48. }
  49. else {
  50. //在指定位置彈出窗口
  51. pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0, 300);
  52. }
  53. }
  54. else {
  55. LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  56. View view = inflater.inflate(R.layout.menu, null);
  57. GridView grid1 = (GridView)view.findViewById(R.id.menuGridChange);
  58. grid1.setAdapter(new ImageAdapter(this));
  59. //生成PopupWindow對象
  60. pw = new PopupWindow(view,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  61. //在指定位置彈出窗口
  62. pw.showAtLocation(findViewById(R.id.tv), Gravity.CENTER, 0, 300);
  63. }
  64. // 此處返回false,系統不會執行onCreateOptionsMenu中添加的菜單
  65. return false;
  66. }
  67. public boolean onKeyDown(int keyCode, KeyEvent event) {
  68. if(keyCode==KeyEvent.KEYCODE_BACK){
  69. pw.dismiss();
  70. return false;
  71. }
  72. return super.onKeyDown(keyCode, event);
  73. }
  74. @Override
  75. public boolean onOptionsItemSelected(MenuItem item) {
  76. return super.onOptionsItemSelected(item);
  77. }
  78. public class ImageAdapter extends BaseAdapter {
  79. private Context context;
  80. public ImageAdapter(Context context) {
  81. this.context = context;
  82. }
  83. @Override
  84. public int getCount() {
  85. return resArray.length;
  86. }
  87. @Override
  88. public Object getItem(int arg0) {
  89. return resArray[arg0];
  90. }
  91. @Override
  92. public long getItemId(int arg0) {
  93. return arg0;
  94. }
  95. @Override
  96. public View getView(int arg0, View arg1, ViewGroup arg2) {
  97. LinearLayout linear = new LinearLayout(context);
  98. LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  99. linear.setOrientation(LinearLayout.VERTICAL);
  100. ImageView iv = new ImageView(context);
  101. iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[arg0])).getBitmap());
  102. LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  103. params2.gravity=Gravity.CENTER;
  104. linear.addView(iv, params2);
  105. TextView tv = new TextView(context);
  106. tv.setText(title[arg0]);
  107. LinearLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  108. params3.gravity=Gravity.CENTER;
  109. linear.addView(tv, params3);
  110. return linear;
  111. }
  112. }
  113. }

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:id="@+id/tv"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="@string/hello"
  12. />
  13. </LinearLayout>

menu.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:id="@+id/tv"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="@string/hello"
  12. />
  13. </LinearLayout>

menu_bg_frame.xml 這個文件是菜單背景的樣式文件,在drawable-hdpi文件下面

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
  3. <solid android:color="#b4000000" />
  4. <stroke android:width="2.0dip" android:color="#b4ffffff" android:dashWidth="3.0dip" android:dashGap="0.0dip" />
  5. <padding android:left="7.0dip" android:top="7.0dip" android:right="7.0dip" android:bottom="7.0dip" />
  6. <corners android:radius="8.0dip" />
  7. </shape>
Copyright © Linux教程網 All Rights Reserved