androidmenu方法
1. 如何在 Android Studio 下创建 Menu
当需要创建一个 Menu 布局文件时,如果直接在 res 文件夹右键选择 XML 文件添加
直接创建.xml文件
会发现虽然可以创建,但是新建的布局文件似乎有点奇怪,不能添加 menu 以及 item
无法正常使用
解决办法:
出现这种情况,可以尝试使用下面这种方法来进行 menu 布局文件的创建:
Android resource directory
menu
在新创建出来的 menu 文件上再次右键
Menu resource file
输入 File name
到这里就已经完成了,可以正常的使用了。
2. android中menu怎么写
菜单资源文件必须放在res/menu目录中。菜单资源文件必须使用<menu>标签作为根节点。除了<menu>标签外,还有另外两个标签用于设置菜单项和分组,这两个标签是<item>和<group>。
<menu>标签没有任何属性,但可以嵌套在<item>标签中,表示子菜单的形式。不过<item>标签中不能再嵌入<item>标签。
1.<item>标签的属性含义如下:
Id:表示菜单项的资源ID
menuCategory:同种菜单项的种类。该属性可取4个值:container、system、secondary和alternative。通过menuCategroy属性可以控制菜单项的位置。例如将属性设为system,表示该菜单项是系统菜单,应放在其他种类菜单项的后面。
orderInCategor:同种类菜单的排列顺序。该属性需要设置一个整数值。例如menuCategory属性值都为system的3个菜单项(item1、item2和item3)。将这3个菜单项的orderInCategory属性值设为3、2、1,那么item3会显示在最前面,而item1会显示在最后面。
title:菜单项标题(菜单项显示的文本)
titleCondensed:菜单项的短标题。当菜单项标题太长时会显示该属性值
icon:菜单项图标资源ID
alphabeticShortcut:菜单项的字母快捷键
numericShortcut:菜单项的数字快捷键
checkable:表示菜单项是否带复选框。该属性可设计为true或false
checked:如果菜单项带复选框(checkable属性为true),该属性表示复选框默认状态是否被选中。可设置的值为true或false
visible:菜单项默认状态是否可视
enable:菜单项默认状态是否被激活
3. 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>
运行效果图:
4. android中menu可能需要重写的方法是哪些
可能需要重写的如下:
public boolean onCreateOptionsMenu(Menu menu)方法只被系统调用一次,如需要动态更改菜单内容还需重写onPrepareOptionsMenu(Menu menu)方法实现
[java] view plain
Menu m=null;
int count=0;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(count>0){
if(count%2==0){
menu.removeGroup(1);
}else{
menu.add(1, Menu.FIRST, 0, "5st");
menu.add(1, Menu.FIRST+1, 0, "6st");
}
}
count++;
return super.onPrepareOptionsMenu(menu);
}
2,context menu(长按屏幕产生)
[java] view plain
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "you select"+item.getItemId(), 500).show();
break;
case 2:
Toast.makeText(this, "you select"+item.getItemId(), 500).show();
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, Menu.FIRST, 0, "1st");
menu.add(0, Menu.FIRST+1, 0, "2st");
super.onCreateContextMenu(menu, v, menuInfo);
}
5. 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特性和使用汇总(一)》
6. android menu属性
这是监听手机上的主菜单文件的,点击手机菜单键,会弹出一个菜单,1代表第一行命令,点击就是推出程序,点击第二个就是弹出软件的介绍。
7. android menu动态怎么增加功能
从menu按键开始,涉及以下3个方法:
1、onCreateOptionsMenu(Menu menu)
2、onPrepareOptionsMenu(Menu menu)
3、onOptionsItemSelected(MenuItem item)
方法名
触发时间
onCreateOptionsMenu
进入Launcher后第一次点MENU按钮时触发
onPrepareOptionsMenu
有两种情况:1、进入Launcher后第一次点MENU按钮触发onCreateOptionsMenu后调用;2、经过第一步,以后每次点MENU按钮时都会触发
onOptionsItemSelected
选择MENU里面的任意内容后触发
8. android 控件menu怎么用
res的menu文件中可以设置menu样式,在代码中使用menu
在代码中使用
public boolenonCreateOptionsMenu(Menu menu){
MenuInflatemenuInflate = getMenuInflate();
menuInflate.inflate(R.menu.option_menu,menu);
returntrue;
}
9. android menu怎样设置item的文字
Android Menu的文字有两种设置方式:
在 menu.xml中设置。
在java代码onCreateOptionsMenu中设置。
以下为示例代码:
在menu.xml中指定title即可
<item android:id="@+id/menu"
android:title="文字"/>重载onCreateOptionsMenu(Menu menu)方法,并调用API。
menu.add((int groupId, int itemId, int order, charsequence title) .setIcon(drawable ID)
add()方法的四个参数,依次是:
1、组别,如果不分组的话就写Menu.NONE。
2、Id,这个很重要,Android根据这个Id来确定不同的菜单 。
3、顺序,哪个菜单项在前面由这个参数的大小决定 。
4、文本,菜单项的显示文本。