先看效果图:

分解一下:
1. 利用Shaper设置一个半透明圆角背景
2. 定义Menu布局,主要就GridView,把图标都放在这个GridView
3. Menu事件, 通过PopupWindow或者AlertDialog或者透明Activity显示到页面即可。
4. 按钮的监听事件,实例中没加。需要的话自己在Adapter里加
半透明圆角背景:
1 |
<?xml
version= "1.0" encoding= "UTF-8" ?> |
2 |
<shape
android:shape= "rectangle" |
3 |
xmlns:android= "http://schemas.android.com/apk/res/android" > |
4 |
<solid
android:color= "#b4000000" /> |
5 |
<stroke
android:width= "2.0dip" android:color= "#b4ffffff" android:dashWidth= "3.0dip" android:dashGap= "0.0dip" /> |
6 |
<padding
android:left= "7.0dip" android:top= "7.0dip" android:right= "7.0dip" android:bottom= "7.0dip" /> |
7 |
<corners
android:radius= "8.0dip" /> |
Menu布局:
01 |
<?xml
version= "1.0" encoding= "UTF-8" ?> |
05 |
android:orientation= "vertical" |
07 |
android:layout_width= "wrap_content" |
09 |
android:layout_height= "fill_parent" |
11 |
xmlns:android= "http://schemas.android.com/apk/res/android" > |
13 |
<GridView
android:gravity= "center" |
15 |
android:layout_gravity= "center" |
17 |
android:id= "@+id/menuGridChange" |
19 |
android:background= "@drawable/menu_bg_frame" |
21 |
android:padding= "5.0dip" |
23 |
android:layout_width= "fill_parent" |
25 |
android:layout_height= "wrap_content" |
27 |
android:horizontalSpacing= "10.0dip" |
29 |
android:verticalSpacing= "3.0dip" |
31 |
android:stretchMode= "columnWidth" |
33 |
android:columnWidth= "60.0dip" |
35 |
android:numColumns= "auto_fit" |
37 |
xmlns:android= "http://schemas.android.com/apk/res/android" /> |
主要类:
002 |
import android.app.Activity; |
003 |
import android.app.AlertDialog; |
004 |
import android.app.AlertDialog.Builder; |
005 |
import android.content.Context; |
006 |
import android.graphics.drawable.BitmapDrawable; |
007 |
import android.os.Bundle; |
008 |
import android.util.Log; |
009 |
import android.view.ContextMenu; |
010 |
import android.view.Gravity; |
011 |
import android.view.LayoutInflater; |
012 |
import android.view.Menu; |
013 |
import android.view.MenuItem; |
014 |
import android.view.View; |
015 |
import android.view.ViewGroup; |
016 |
import android.view.ContextMenu.ContextMenuInfo; |
017 |
import android.widget.BaseAdapter; |
018 |
import android.widget.GridView; |
019 |
import android.widget.ImageView; |
020 |
import android.widget.LinearLayout; |
021 |
import android.widget.PopupWindow; |
022 |
import android.widget.TextView; |
023 |
import android.widget.LinearLayout.LayoutParams; |
024 |
public class MenuTest extends Activity
{ |
025 |
private String
TAG = this .getClass().getSimpleName(); |
026 |
private int []
resArray = new int []
{ |
027 |
R.drawable.icon_menu_addto,
R.drawable.icon_menu_audioinfo, |
028 |
R.drawable.icon_menu_findlrc,
R.drawable.icon_menu_scan |
030 |
private String[]
title = new String[]{ |
031 |
"添加歌曲" , "歌曲信息" , "查找歌词" , "搜索歌词" |
033 |
private static boolean show_flag
= false ; |
034 |
private PopupWindow
pw = null ; |
035 |
/**
Called when the activity is first created. */ |
037 |
public void onCreate(Bundle
savedInstanceState) { |
038 |
super .onCreate(savedInstanceState); |
039 |
setContentView(R.layout.main); |
042 |
public boolean onCreateOptionsMenu(Menu
menu) { |
043 |
Log.e(TAG, "------
onCreateOptionsMenu ------" ); |
051 |
LayoutInflater
inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
052 |
View
view = inflater.inflate(R.layout.menu, null ); |
053 |
GridView
grid1 = (GridView)view.findViewById(R.id.menuGridChange); |
054 |
grid1.setAdapter( new ImageAdapter( this )); |
056 |
pw
= new PopupWindow(view,LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT); |
058 |
pw.showAtLocation(findViewById(R.id.tv),
Gravity.CENTER, 0 , 300 ); |
062 |
public boolean onOptionsItemSelected(MenuItem
item) { |
063 |
return super .onOptionsItemSelected(item); |
065 |
public class ImageAdapter extends BaseAdapter
{ |
066 |
private Context
context; |
067 |
public ImageAdapter(Context
context) { |
068 |
this .context
= context; |
071 |
public int getCount()
{ |
072 |
return resArray.length; |
075 |
public Object
getItem( int arg0)
{ |
076 |
return resArray[arg0]; |
079 |
public long getItemId( int arg0)
{ |
083 |
public View
getView( int arg0,
View arg1, ViewGroup arg2) { |
084 |
LinearLayout
linear = new LinearLayout(context); |
085 |
LinearLayout.LayoutParams
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT); |
086 |
linear.setOrientation(LinearLayout.VERTICAL); |
087 |
ImageView
iv = new ImageView(context); |
088 |
iv.setImageBitmap(((BitmapDrawable)context.getResources().getDrawable(resArray[arg0])).getBitmap()); |
089 |
LinearLayout.LayoutParams
params2 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT); |
090 |
params2.gravity=Gravity.CENTER; |
091 |
linear.addView(iv,
params2); |
092 |
TextView
tv = new TextView(context); |
093 |
tv.setText(title[arg0]); |
094 |
LinearLayout.LayoutParams
params3 = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT); |
095 |
params3.gravity=Gravity.CENTER; |
096 |
linear.addView(tv,
params3); |