导航:首页 > 操作系统 > androidmenu的使用

androidmenu的使用

发布时间:2022-12-24 04:27:09

android点击menu键如何选择使用不同的菜单

什么叫【选择使用不同的菜单】?menu中最多显示8个,超过8个会自动添加一个【更多】,点击后显示其他的,你是问怎么设置菜单里面的内容还是什么?

❷ Android如何自定义Menu

新建自定义Menu————>TabMenu.java如下:

package com.ncw;

import android.content.Context;

import android.graphics.Color;

import android.graphics.drawable.ColorDrawable;

import android.view.Gravity;

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.AdapterView.OnItemClickListener;

import android.widget.LinearLayout.LayoutParams;

public class TabMenu extends PopupWindow {

private GridView gridView;

private LinearLayout mLayout;

public TabMenu(Context context, OnItemClickListener bodyClick, int colorBgTabMenu) {

super(context);

mLayout = new LinearLayout(context);

mLayout.setOrientation(LinearLayout.VERTICAL);

gridView = new GridView(context);

gridView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT));

gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));// 选中的时候为透明色

gridView.setNumColumns(4);

gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

gridView.setVerticalSpacing(0);

gridView.setHorizontalSpacing(0);

gridView.setPadding(0, 0, 0, 0);

gridView.setGravity(Gravity.CENTER);

gridView.setOnItemClickListener(bodyClick);

mLayout.addView(gridView);

// 设置默认项

this.setContentView(mLayout);

this.setWidth(LayoutParams.FILL_PARENT);

this.setHeight(LayoutParams.WRAP_CONTENT);

this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 设置TabMenu菜单背景

this.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应

}

public void SetBodySelect(int index, int colorSelBody) {

int count = gridView.getChildCount();

for (int i = 0; i < count; i++) {

if (i != index)

((LinearLayout) gridView.getChildAt(i))

.setBackgroundColor(Color.TRANSPARENT);

}

((LinearLayout) gridView.getChildAt(index))

.setBackgroundColor(colorSelBody);

}

public void SetBodyAdapter(MenuBodyAdapter bodyAdapter) {

gridView.setAdapter(bodyAdapter);

}

/**

* 自定义Adapter,TabMenu的每个分页的主体

*

*/

static public class MenuBodyAdapter extends BaseAdapter {

private Context mContext;

private int[] resID;

/**

* 设置TabMenu的分页主体

*

* @param context

* 调用方的上下文

* @param resID

*/

public MenuBodyAdapter(Context context, int[] resID) {

this.mContext = context;

this.resID = resID;

}

@Override

public int getCount() {

return resID.length;

}

public Object getItem(int position) {

return makeMenyBody(position);

}

public long getItemId(int position) {

return position;

}

private LinearLayout makeMenyBody(int position) {

LinearLayout result = new LinearLayout(this.mContext);

result.setOrientation(LinearLayout.VERTICAL);

result.setGravity(Gravity.CENTER_HORIZONTAL

| Gravity.CENTER_VERTICAL);

result.setPadding(0, 0, 0, 0);

ImageView img = new ImageView(this.mContext);

img.setBackgroundResource(resID[position]);

result.addView(img, new LinearLayout.LayoutParams(new LayoutParams(

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)));

return result;

}

public View getView(int position, View convertView, ViewGroup parent) {

return makeMenyBody(position);

}

}

}

?

1

使用自定义Menu————>testTabMenu.java

package com.ncw;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.util.Log;

import android.view.Gravity;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

public class testTabMenu extends Activity {

TabMenu.MenuBodyAdapter bodyAdapter = new TabMenu.MenuBodyAdapter(this,

new int[] { R.drawable.menu_01, R.drawable.menu_02,

R.drawable.menu_03, R.drawable.menu_04 });

TabMenu tabMenu;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tabMenu = new TabMenu(this, new BodyClickEvent(), R.drawable.menu_bg);// 出现与消失的动画

tabMenu.update();

tabMenu.SetBodyAdapter(bodyAdapter);

}

class BodyClickEvent implements OnItemClickListener {

@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

tabMenu.SetBodySelect(arg2, Color.GRAY);

Log.i("Log", " BodyClickEvent implements OnItemClickListener "

+ arg2);

}

}

@Override

/**

* 创建MENU

*/

public boolean onCreateOptionsMenu(Menu menu) {

menu.add("menu");// 必须创建一项

return super.onCreateOptionsMenu(menu);

}

@Override

/**

* 拦截MENU

*/

public boolean onMenuOpened(int featureId, Menu menu) {

if (tabMenu != null) {

if (tabMenu.isShowing())

tabMenu.dismiss();

else {

tabMenu.showAtLocation(findViewById(R.id.LinearLayout01),

Gravity.BOTTOM, 0, 0);

}

}

return false;// 返回为true 则显示系统menu

}

}


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/LinearLayout01"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<TextView

android:id="@+id/TextView01"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="自定义Menu————张亚龙" >

</TextView>

</LinearLayout>

运行效果图:

❸ android中怎么让menu菜单显示在屏幕左上角

用惯了Android的人在刚拿到iPhone的时候,总是会习惯性的用手指从状态栏往下拖一下,这都是给Notification闹的。
不过Notification也确实是1个不错的提示工具,不干扰正常的操作,事后还可以再翻看详细的内容,点击后还可以进入相关的画面查看更具体的内容。
今天我就以代码为主的形式来介绍Notification的使用,包括基本用法,自定义的View,以及更多的控制方法。
另一种Android中常用到的提示方法Toast的用法请参见《教程:在Android中使用Toast进行提示》
我们先看下Notification的几个主要组成部分:
Icon:不解释
Ticker Text:Notification刚出来的时候,在状态栏上滚动的字幕,如果很长,会自动分割滚动

Content Title:Notification展开后的标题
Content Text:Notification展开后的内容

Notification的一般用法
取得NotificationManager
private NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
创建Notification并且显示
//Notification的滚动提示
String tickerText = "My notification, It's a long text! Hello World desiyo?";
//Notification的图标,一般不要用彩色的
int icon = R.drawable.icon_02241_3;

//contentTitle和contentText都是标准的Notification View的内容
//Notification的内容标题,拖下来后看到的标题
String contentTitle="My notification";
//Notification的内容
String contentText="Hello World!";

//Notification的Intent,即点击后转向的Activity
Intent notificationIntent = new Intent(this, this.getClass());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);

//创建Notifcation
Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
//设定Notification出现时的声音,一般不建议自定义
notification.defaults |= Notification.DEFAULT_SOUND;
//设定如何振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
//指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身
//这符合一般的Notification的运作规范
notification.flags|=Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
//显示这个notification
mNotificationManager.notify(HELLO_ID, notification);
这是最基本的应用,可以说除了找个合适的图标以外,其它都很简单。

使用自定义View的Notification
同Toast一样,我们也可以自已指定1个View来作为Notification展开后的显示内容,比如说在Android Market中下载的时候,Notification中会显示当前下载的进度,那么我们也来模拟1个这样的效果吧。
首先给出View的定义文件:notification_view_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
>
<ImageView android:id="@+id/notificationImage"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@android:drawable/stat_sys_download"
/>
<TextView android:id="@+id/notificationTitle"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="@id/notificationImage"
android:layout_alignParentRight="true"
android:paddingLeft="6dp"
android:textColor="#FF000000"
/>
<TextView android:id="@+id/notificationPercent"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/notificationImage"
android:paddingTop="2dp"
android:textColor="#FF000000"
/>
<ProgressBar android:id="@+id/notificationProgress"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/notificationTitle"
android:layout_alignLeft="@id/notificationTitle"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/notificationPercent"
android:paddingLeft="6dp"
android:paddingRight="3dp"
android:paddingTop="2dp"
style="?android:attr/progressBarStyleHorizontal"
/>
</RelativeLayout>
RelativeLayout的使用,可以参考:《教程:Android各种Layout特性和使用汇总(一)》

阅读全文

与androidmenu的使用相关的资料

热点内容
voc文件夹 浏览:862
租广东联通服务器注意什么云空间 浏览:932
javascript高级程序设计pdf 浏览:289
pwm单片机原理 浏览:346
ai算法在线修复图片 浏览:979
scratch编程中如何做射击游戏 浏览:476
at89c51编程器 浏览:341
项目经理叫醒程序员 浏览:342
autocad旋转命令 浏览:660
手机版wpsoffice怎么打包文件夹 浏览:579
在成都学车用什么app 浏览:818
grep命令管道 浏览:426
java修改重启 浏览:567
单片机供电方案 浏览:770
airpodspro一代怎么连接安卓 浏览:218
豌豆荚app上有什么游戏 浏览:284
公路商店app标签选什么 浏览:339
linuxoracle命令行登录 浏览:227
android深度休眠 浏览:173
php微信开发例子 浏览:846