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
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView
- android:id="@+id/hello"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="hello"/>
- </LinearLayout>
2、建立res\drawable-hpdi\framestyle.xml文件。
該文件實現彈出窗口背景樣式:注意圓弧效果。
輸入以下代碼:
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <shape
- android:shape="rectangle"
- xmlns:android="http://schemas.android.com/apk/res/android">
-
- <solid android:color="#b4000000" />
- <stroke android:width="2.0dip" android:color="#b4ffffff" android:dashWidth="3.0dip" android:dashGap="0.0dip" />
- <padding android:left="7.0dip"
- android:top="7.0dip"
- android:right="7.0dip"
- android:bottom="7.0dip" />
-
- <corners android:radius="8.0dip" />
- </shape>
3、建立res\layout\menu.xml文件。
該文件實現彈出窗口,即菜單效果。
輸入以下代碼:
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <GridView android:gravity="center"
- android:layout_gravity="center"
- android:id="@+id/menuGridChange"
- android:background="@drawable/framestyle" <!--注意此處引用背景樣式:framestyle-->
- android:padding="5.0dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:horizontalSpacing="10.0dip"
- android:verticalSpacing="3.0dip"
- android:stretchMode="columnWidth"
- android:columnWidth="60.0dip"
- android:numColumns="auto_fit"
- xmlns:android="http://schemas.android.com/apk/res/android" />
- </LinearLayout>
三、程序文件
打開“src/com.genwoxue.MainActivity.java”文件。
然後輸入以下代碼:
[java] view plain copy
- package com.genwoxue.popupwindow;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.PopupWindow;
- import android.widget.TextView;
- import android.widget.LinearLayout.LayoutParams;
-
- public class MainActivity extends Activity {
-
- //定義彈出窗口菜音圖標
- private int[] resArray = new int[] {
- R.drawable.icon_menu_addto,
- R.drawable.icon_menu_audioinfo,
- R.drawable.icon_menu_findlrc,
- R.drawable.icon_menu_scan
- };
-
- //定義彈出窗口菜單文字
- private String[] title = new String[]{
- "添加", "信息", "搜索", "查看"
- };
-
- //聲明窗口PopupWindow
- private PopupWindow pw = null;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add("");
- return super.onCreateOptionsMenu(menu);
- }
-
- public boolean onMenuOpened(int featureId, Menu menu) {
- if (pw != null) {
- if (pw.isShowing()) {
- //關閉彈出窗口
- pw.dismiss();
- }
- else {
- //在指定位置彈出窗口
- pw.showAtLocation(findViewById(R.id.hello), Gravity.CENTER, 0, 300);
- }
- }
- else {
- //定義窗口菜單
- LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = inflater.inflate(R.layout.menu, null);
- GridView grid1 = (GridView)view.findViewById(R.id.menuGridChange);
- grid1.setAdapter(new ImageAdapter(this));
- //生成PopupWindow對象
- pw = new PopupWindow(view,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
- //在指定位置彈出窗口
- pw.showAtLocation(findViewById(R.id.hello), Gravity.CENTER, 0, 300);
- }
-
- // 此處返回false,系統不會執行onCreateOptionsMenu中添加的菜單
- return false;
- }
-
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if(keyCode==KeyEvent.KEYCODE_BACK){
- pw.dismiss();
- return false;
- }
-
- return super.onKeyDown(keyCode, event);
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- return super.onOptionsItemSelected(item);
- }
-
- public class ImageAdapter extends BaseAdapter {
- private Context context;
- public ImageAdapter(Context context) {
- this.context = context;
- }
-
- @Override
- public int getCount() {
- return resArray.length;
- }
-
- @Override
- public Object getItem(int arg0) {
- return resArray[arg0];
- }
-
- @Override
- public long getItemId(int arg0) {
- return arg0;
- }
-
-
- @Override //實現圖標下包含字符效果
- public View getView(int arg0, View arg1, ViewGroup arg2) {
- LinearLayout linear = new LinearLayout(context);
- LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- linear.setOrientation(LinearLayout.VERTICAL);
- ImageView iv = new ImageView(context);
- iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[arg0])).getBitmap());
- LinearLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- params2.gravity=Gravity.CENTER;
- linear.addView(iv, params2);
- TextView tv = new TextView(context);
- tv.setText(title[arg0]);
- LinearLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- params3.gravity=Gravity.CENTER;
- linear.addView(tv, params3);
- return linear;
- }
- }
-
- }
四、運行結果