Android如何实现uc浏览器一样的菜单
【IT168技术】菜单开发也是程序员经常需要关注的,如何使菜单变得更加方便用户使用是每一个开发者都要面临的实际问题,今天给Android开发者介绍一下如何开发出像UC浏览器一样的菜单。使用AlertDialog生成菜单,利用setView()方法设置菜单视图。
布局如下:
1、菜单布局
利用GridView
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < ?xml version = " 1.0 " encoding = " utf-8 " ? >
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:layout_width = " wrap_content " android:layout_height = " wrap_content "
android:orientation = " vertical " >
< GridView android:id = " @+id/menu "
android:layout_width = " fill_parent "
android:layout_height = " fill_parent "
android:numColumns = " 2 "
android:verticalSpacing = " 5dip "
android:horizontalSpacing = " 5dip "
android:stretchMode = " columnWidth "
android:gravity = " center " ></ GridView >
</ LinearLayout >
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < ?xml version = " 1.0 " encoding = " utf-8 " ? >
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:layout_width = " wrap_content " android:layout_height = " wrap_content "
android:orientation = " vertical " >
< GridView android:id = " @+id/menu "
android:layout_width = " fill_parent "
android:layout_height = " fill_parent "
android:numColumns = " 2 "
android:verticalSpacing = " 5dip "
android:horizontalSpacing = " 5dip "
android:stretchMode = " columnWidth "
android:gravity = " center " ></ GridView >
</ LinearLayout >
2、每一个item的布局
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < ?xml version = " 1.0 " encoding = " utf-8 " ? >
< RelativeLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:id = " @+id/RelativeLayout_Item " android:layout_width = " wrap_content "
android:layout_height = " wrap_content " android:paddingBottom = " 5dip " >
< ImageView android:id = " @+id/item_image "
android:layout_centerHorizontal = " true " android:layout_width = " wrap_content "
android:layout_height = " wrap_content " ></ ImageView >
< TextView android:layout_below = " @id/item_image " android:id = " @+id/item_text "
android:layout_centerHorizontal = " true " android:layout_width = " wrap_content "
android:layout_height = " wrap_content " android:text = " 选项 " ></ TextView >
</ RelativeLayout >
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < ?xml version = " 1.0 " encoding = " utf-8 " ? >
< RelativeLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:id = " @+id/RelativeLayout_Item " android:layout_width = " wrap_content "
android:layout_height = " wrap_content " android:paddingBottom = " 5dip " >
< ImageView android:id = " @+id/item_image "
android:layout_centerHorizontal = " true " android:layout_width = " wrap_content "
android:layout_height = " wrap_content " ></ ImageView >
< TextView android:layout_below = " @id/item_image " android:id = " @+id/item_text "
android:layout_centerHorizontal = " true " android:layout_width = " wrap_content "
android:layout_height = " wrap_content " android:text = " 选项 " ></ TextView >
</ RelativeLayout >
主程序代码如下:
代码比较简单,注释非常详细
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> package com.cloay.down.utils;
import java.util.ArrayList;
import java.util.HashMap;
import com.cloay.down.R;
import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
/**
* 菜单工具类
* MenuUtil.java
* @author cloay
* 2011 - 10 - 25
*/
public class MenuUtil {
private static AlertDialog menuDialog; // menu菜单Dialog
private static GridView menuGrid;
/** 菜单图片 **/
static int [] menu_image_array = { R.drawable.menu_open_in_background, R.drawable.menu_redownload, R.drawable.menu_detail, R.drawable.menu_delete };
/** 菜单文字 **/
static String [] menu_name_array = { " 打开 " , " 重新下载 " , " 详细 " , " 删除 " };
public static void ShowMenuDialog(final Context context){
View menuView = View.inflate(context, R.layout.menu, null );
menuDialog = new AlertDialog.Builder(context)
.setView(menuView)
.create();
menuDialog.show();
menuGrid = (GridView) menuView.findViewById(R.id.menu);
menuGrid.setAdapter(getMenuAdapter(context, menu_name_array, menu_image_array));
menuGrid.setOnItemClickListener( new OnItemClickListener() {
// 监听menu按钮事件
@Override
public void onItemClick(AdapterView < ? > parent, View view,
int position, long id) {
switch(position){
case 0 : // open file
break;
case 1 : // redownload 重新下载
break;
case 2 : // file details
break;
case 3 : // delete file
break;
}
}
});
}
/**
* 为menuGrid设置Adapter
* @param context
* @param menuNameArray
* @param imageResourceArray
* @return
*/
private static SimpleAdapter getMenuAdapter(Context context, String [] menuNameArray,
int [] imageResourceArray) {
ArrayList < HashMap < String , Object >> data = new ArrayList < HashMap < String , Object >> ();
for ( int i = 0 ; i < menuNameArray.length; i ++ ) {
HashMap < String , Object > map = new HashMap < String , Object > ();
map.put( " itemImage " , imageResourceArray[i]);
map.put( " itemText " , menuNameArray[i]);
data.add(map);
}
SimpleAdapter simperAdapter = new SimpleAdapter(context, data,
R.layout.item_menu, new String [] { " itemImage " , " itemText " },
new int [] { R.id.item_image, R.id.item_text });
return simperAdapter;
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> package com.cloay.down.utils;
import java.util.ArrayList;
import java.util.HashMap;
import com.cloay.down.R;
import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
/**
* 菜单工具类
* MenuUtil.java
* @author cloay
* 2011 - 10 - 25
*/
public class MenuUtil {
private static AlertDialog menuDialog; // menu菜单Dialog
private static GridView menuGrid;
/** 菜单图片 **/
static int [] menu_image_array = { R.drawable.menu_open_in_background, R.drawable.menu_redownload, R.drawable.menu_detail, R.drawable.menu_delete };
/** 菜单文字 **/
static String [] menu_name_array = { " 打开 " , " 重新下载 " , " 详细 " , " 删除 " };
public static void ShowMenuDialog(final Context context){
View menuView = View.inflate(context, R.layout.menu, null );
menuDialog = new AlertDialog.Builder(context)
.setView(menuView)
.create();
menuDialog.show();
menuGrid = (GridView) menuView.findViewById(R.id.menu);
menuGrid.setAdapter(getMenuAdapter(context, menu_name_array, menu_image_array));
menuGrid.setOnItemClickListener( new OnItemClickListener() {
// 监听menu按钮事件
@Override
public void onItemClick(AdapterView < ? > parent, View view,
int position, long id) {
switch(position){
case 0 : // open file
break;
case 1 : // redownload 重新下载
break;
case 2 : // file details
break;
case 3 : // delete file
break;
}
}
});
}
/**
* 为menuGrid设置Adapter
* @param context
* @param menuNameArray
* @param imageResourceArray
* @return
*/
private static SimpleAdapter getMenuAdapter(Context context, String [] menuNameArray,
int [] imageResourceArray) {
ArrayList < HashMap < String , Object >> data = new ArrayList < HashMap < String , Object >> ();
for ( int i = 0 ; i < menuNameArray.length; i ++ ) {
HashMap < String , Object > map = new HashMap < String , Object > ();
map.put( " itemImage " , imageResourceArray[i]);
map.put( " itemText " , menuNameArray[i]);
data.add(map);
}
SimpleAdapter simperAdapter = new SimpleAdapter(context, data,
R.layout.item_menu, new String [] { " itemImage " , " itemText " },
new int [] { R.id.item_image, R.id.item_text });
return simperAdapter;
}
}