菜单开发也是程序员经常需要关注的,如何使菜单变得更加方便用户使用是每一个开发者都要面临的实际问题,今天给Android开发者介绍一下如何开发出像UC浏览器一样的菜单。使用AlertDialog生成菜单,利用setView()方法设置菜单视图。
成都创新互联专注于企业成都全网营销、网站重做改版、永宁网站定制设计、自适应品牌网站建设、H5高端网站建设、成都商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为永宁等各大城市提供网站开发制作服务。
布局如下:
1、菜单布局
利用GridView
<
?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的布局
<
?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
>
主程序代码如下:
代码比较简单,注释非常详细
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;
}
}
标题名称:Android如何实现uc浏览器一样的菜单
当前URL:http://www.mswzjz.cn/qtweb/news44/306894.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能