Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android技術基礎 >> 第48章、彈出窗口PopupWindow(從零開始學Android)

第48章、彈出窗口PopupWindow(從零開始學Android)

編輯:Android技術基礎

PopupWindow 是一種阻塞式的彈出窗口,這就意味著在我們退出這個彈出框之前,程序會一直等待。它可以浮動在當前Activity的任何的位置上。

一、設計界面  

 

  1、首先把icon_menu_addto.png、icon_menu_audioinfo.png、icon_menu_findlrc.png、icon_menu_scan.png圖片復制到res/drawable-hdpi文件夾內。

  \

 

二、布局文件

  1、打開activity_main.xml文件。

  該文件實現主窗體。

  輸入以下代碼:

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


  2、建立res\drawable-hpdi\framestyle.xml文件。

  該文件實現彈出窗口背景樣式:注意圓弧效果。

  輸入以下代碼:

[html] view plain copy  
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <shape   
  3.     android:shape="rectangle"   
  4.     xmlns:android="http://schemas.android.com/apk/res/android">       
  5.       
  6.     <solid android:color="#b4000000" />       
  7.     <stroke android:width="2.0dip" android:color="#b4ffffff" android:dashWidth="3.0dip" android:dashGap="0.0dip" />      
  8.     <padding android:left="7.0dip"   
  9.         android:top="7.0dip"   
  10.         android:right="7.0dip"   
  11.         android:bottom="7.0dip" />       
  12.       
  13.     <corners android:radius="8.0dip" />  
  14. </shape>   

 

  3、建立res\layout\menu.xml文件。

  該文件實現彈出窗口,即菜單效果。

  \

  輸入以下代碼:

[html] view plain copy  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout  
  3.         android:orientation="vertical"  
  4.         android:layout_width="wrap_content"  
  5.         android:layout_height="fill_parent"  
  6.         xmlns:android="http://schemas.android.com/apk/res/android">  
  7.         <GridView android:gravity="center"  
  8.                 android:layout_gravity="center"  
  9.                 android:id="@+id/menuGridChange"  
  10.                 android:background="@drawable/framestyle"   <!--注意此處引用背景樣式:framestyle-->  
  11.                 android:padding="5.0dip"  
  12.                 android:layout_width="fill_parent"  
  13.                 android:layout_height="wrap_content"  
  14.                 android:horizontalSpacing="10.0dip"  
  15.                 android:verticalSpacing="3.0dip"  
  16.                 android:stretchMode="columnWidth"  
  17.                 android:columnWidth="60.0dip"  
  18.                 android:numColumns="auto_fit"  
  19.                 xmlns:android="http://schemas.android.com/apk/res/android" />  
  20. </LinearLayout>  

 

三、程序文件

  打開“src/com.genwoxue.MainActivity.java”文件。

  然後輸入以下代碼:

 

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


四、運行結果

  \

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved