androidmenu样式
㈠ 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特性和使用汇总(一)》
㈡ 如何让android menu横向显示
不可以,menu是需要触发了menu这个按键才会显示出来。
菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu),以下说的是创建OptionsMenu
一、概述
public boolean onCreateOptionsMenu(Menu menu):使用此方法调用OptionsMenu。
public boolean onOptionsItemSelected(MenuItem item):选中菜单项后发生的动作。
public void onOptionsMenuClosed(Menu menu):菜单关闭后发生的动作。
public boolean onPrepareOptionsMenu(Menu menu):选项菜单显示之前onPrepareOptionsMenu方法会被调用,你可以用此方法来根据打当时的情况调整菜单。
public boolean onMenuOpened(int featureId, Menu menu):单打开后发生的动作。
二、默认样式
默认样式是在屏幕底部弹出一个菜单,这个菜单我们就叫他选项菜单OptionsMenu,一般情况下,选项菜单最多显示2排每排3个菜单项,这些菜单项有文字有图标,也被称作Icon Menus,如果多于6项,从第六项开始会被隐藏,在第六项会出现一个More里,点击More才出现第六项以及以后的菜单项,这些菜单项也被称作Expanded Menus。下面介绍。
1.main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"android:text="请点击Menu键显示选项菜单"
android:id="@+id/TextView02"/>
</LinearLayout>
2。重载onCreateOptionsMenu(Menu menu)方法
重载onCreateOptionsMenu(Menu menu)方法,并在此方法中添加菜单项,最后返回true,如果false,菜单则不会显示。
(Menumenu)
@Override
(Menumenu){
/*
*
*add()方法的四个参数,依次是:
*
*1、组别,如果不分组的话就写Menu.NONE,
*
*2、Id,这个很重要,Android根据这个Id来确定不同的菜单
*
*3、顺序,那个菜单现在在前面由这个参数的大小决定
*
*4、文本,菜单的显示文本
*/
menu.add(Menu.NONE,Menu.FIRST+1,5,"删除").setIcon(
android.R.drawable.ic_menu_delete);
//setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以
//android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的
menu.add(Menu.NONE,Menu.FIRST+2,2,"保存").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE,Menu.FIRST+3,6,"帮助").setIcon(
android.R.drawable.ic_menu_help);
menu.add(Menu.NONE,Menu.FIRST+4,1,"添加").setIcon(
android.R.drawable.ic_menu_add);
menu.add(Menu.NONE,Menu.FIRST+5,4,"详细").setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE,Menu.FIRST+6,3,"发送").setIcon(
android.R.drawable.ic_menu_send);
returntrue;
}
3。为菜单项注册事件
使用onOptionsItemSelected(MenuItem item)方法为菜单项注册事件
(MenuItemitem)
@Override
(MenuItemitem){
switch(item.getItemId()){
caseMenu.FIRST+1:
Toast.makeText(this,"删除菜单被点击了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+2:
Toast.makeText(this,"保存菜单被点击了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+3:
Toast.makeText(this,"帮助菜单被点击了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+4:
Toast.makeText(this,"添加菜单被点击了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+5:
Toast.makeText(this,"详细菜单被点击了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+6:
Toast.makeText(this,"发送菜单被点击了",Toast.LENGTH_LONG).show();
break;
}
returnfalse;
}
4.完整代码
packagecom.android.menu;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.widget.Toast;
{
/**.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
(Menumenu){
/*
*
*add()方法的四个参数,依次是:
*
*1、组别,如果不分组的话就写Menu.NONE,
*
*2、Id,这个很重要,Android根据这个Id来确定不同的菜单
*
*3、顺序,那个菜单现在在前面由这个参数的大小决定
*
*4、文本,菜单的显示文本
*/
menu.add(Menu.NONE,Menu.FIRST+1,5,"删除").setIcon(
android.R.drawable.ic_menu_delete);
//setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以
//android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的
menu.add(Menu.NONE,Menu.FIRST+2,2,"保存").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE,Menu.FIRST+3,6,"帮助").setIcon(
android.R.drawable.ic_menu_help);
menu.add(Menu.NONE,Menu.FIRST+4,1,"添加").setIcon(
android.R.drawable.ic_menu_add);
menu.add(Menu.NONE,Menu.FIRST+5,4,"详细").setIcon(
android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE,Menu.FIRST+6,3,"发送").setIcon(
android.R.drawable.ic_menu_send);
returntrue;
}
@Override
(MenuItemitem){
switch(item.getItemId()){
caseMenu.FIRST+1:
Toast.makeText(this,"删除菜单被点击了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+2:
Toast.makeText(this,"保存菜单被点击了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+3:
Toast.makeText(this,"帮助菜单被点击了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+4:
Toast.makeText(this,"添加菜单被点击了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+5:
Toast.makeText(this,"详细菜单被点击了",Toast.LENGTH_LONG).show();
break;
caseMenu.FIRST+6:
Toast.makeText(this,"发送菜单被点击了",Toast.LENGTH_LONG).show();
break;
}
returnfalse;
}
@Override
publicvoidonOptionsMenuClosed(Menumenu){
Toast.makeText(this,"选项菜单关闭了",Toast.LENGTH_LONG).show();
}
@Override
(Menumenu){
Toast.makeText(this,
"选项菜单显示之前onPrepareOptionsMenu方法会被调用,你可以用此方法来根据打当时的情况调整菜单",
Toast.LENGTH_LONG).show();
//如果返回false,此方法就把用户点击menu的动作给消费了,onCreateOptionsMenu方法将不会被调用
returntrue;
}
}
5.运行效果
㈢ Android如何自定义menu的背景
01.public class MenuEx extends Activity { 02. 03. private static final String TAG = "android123"; 04. @Override 05. public void onCreate(Bundle savedInstanceState) { 06. super.onCreate(savedInstanceState); 07. setContentView(R.layout.main); 08. } 09. @Override 10. public boolean onCreateOptionsMenu(Menu menu) { 11. super.onCreateOptionsMenu(menu); 12. MenuInflater inflater = new MenuInflater(getApplicationContext()); 13. inflater.inflate(R.menu.options_menu, menu); 14. setMenuBackground(); 15. return true; 16. } 01.public class MenuEx extends Activity { 02. 03. private static final String TAG = "android123"; 04. @Override 05. public void onCreate(Bundle savedInstanceState) { 06. super.onCreate(savedInstanceState); 07. setContentView(R.layout.main); 08. } 09. @Override 10. public boolean onCreateOptionsMenu(Menu menu) { 11. super.onCreateOptionsMenu(menu); 12. MenuInflater inflater = new MenuInflater(getApplicationContext()); 13. inflater.inflate(R.menu.options_menu, menu); 14. setMenuBackground(); 15. return true; 16. } 上面的例子可以轻松的替换当前Activity的Menu背景颜色,这里Android开发网再次提醒大家上面加粗的包名不能随意改动,如果非原生的Android系统,这句可能根据各个厂商编译的固件来灵活处理。
㈣ 安卓开发 menu排列问题,怎么设置成横排,填满底部
你这是自定义样式的,,你跟改为默认样式就可以。
例子:
㈤ android应用开发中,如何让自制的设置菜单列表和系统设置列表的样式保持一致,当主题更改时也能跟着变化
自己定义一个布局(和系统的菜单一样) 然后拦截menu事件 弹出就可以了 自定义menu
步骤:
1) 覆写onCreateOptionsMenu(),返回值改为false
public boolean onCreateOptionsMenu(
Menu menu) {
return false;
}
2) 创建 */
View contentView = LayoutInflater.from(this).inflate(R.layout.menu,
null);
view = contentView.findViewById(R.id.view);
view.setOnClickListener(this);
/**
* 创建弹出的会话框 contentView 是会话框显示的View 宽 ,高
*/
optionMenu = new PopupWindow(contentView, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
3) 显示菜单
mOptionsMenu.showAtLocation(
findViewById(R.id.main),
Gravity.BOTTOM, 0, 0);
㈥ 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 item的字体样式和背景颜色怎么自定义
android旋钮背景颜色 android按钮背景颜色 默认情况下,Button使用android系统提供的默认背景。因此在不同平台上或者设备上,button显示的风格也不相同。android支持修改button默认的显示风格,可通过Drawable状态列表替换默认的背景
㈧ 怎么让android系统中隐藏的menu按钮显示出来
菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu),以下说的是创建OptionsMenu 一、概述 public boolean onCreateOptionsMenu(Menu menu):使用此方法调用OptionsMenu。 public boolean onOptionsItemSelected(MenuItem item):选中菜单项后发生的动作。 public void onOptionsMenuClosed(Menu menu):菜单关闭后发生的动作。 public boolean onPrepareOptionsMenu(Menu menu):选项菜单显示之前onPrepareOptionsMenu方法会被调用,你可以用此方法来根据打当时的情况调整菜单。 public boolean onMenuOpened(int featureId, Menu menu):单打开后发生的动作。 二、默认样式 默认样式是在屏幕底部弹出一个菜单,这个菜单我们就叫他选项菜单OptionsMenu,一般情况下,选项菜单最多显示2排每排3个菜单项,这些菜单项有文字有图标,也被称作Icon Menus,如果多于6项,从第六项开始会被隐藏,在第六项会出现一个More里,点击More才出现第六项以及以后的菜单项,这些菜单项也被称作Expanded Menus。下面介绍。 1/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请点击 Menu键显示选项菜单" android:id="@+id/TextView02" /> </LinearLayout> 2。重载onCreateOptionsMenu(Menu menu)方法 重载onCreateOptionsMenu(Menu menu)方法,并在此方法中添加菜单项,最后返回true,如果false,菜单则不会显示。 public boolean onCreateOptionsMenu(Menu menu) @Override public boolean onCreateOptionsMenu(Menu menu) { /* * * add()方法的四个参数,依次是: * * 1、组别,如果不分组的话就写Menu.NONE, * * 2、Id,这个很重要,Android根据这个Id来确定不同的菜单 * * 3、顺序,那个菜单现在在前面由这个参数的大小决定 * * 4、文本,菜单的显示文本 */ menu.add(Menu.NONE, Menu.FIRST + 1, 5, "删除").setIcon( android.R.drawable.ic_menu_delete); // setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以 // android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的 menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon( android.R.drawable.ic_menu_edit); menu.add(Menu.NONE, Menu.FIRST + 3, 6, "帮助").setIcon( android.R.drawable.ic_menu_help); menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon( android.R.drawable.ic_menu_add); menu.add(Menu.NONE, Menu.FIRST + 5, 4, "详细").setIcon( android.R.drawable.ic_menu_info_details); menu.add(Menu.NONE, Menu.FIRST + 6, 3, "发送").setIcon( android.R.drawable.ic_menu_send); return true; } 3。为菜单项注册事件 使用onOptionsItemSelected(MenuItem item)方法为菜单项注册事件 public boolean onOptionsItemSelected(MenuItem item) @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Menu.FIRST + 1: Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST + 2: Toast.makeText(this, "保存菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST + 3: Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST + 4: Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST + 5: Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST + 6: Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show(); break; } return false; } 4.完整代码 package com.android.menu; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class DefaultMenu extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { /* * * add()方法的四个参数,依次是: * * 1、组别,如果不分组的话就写Menu.NONE, * * 2、Id,这个很重要,Android根据这个Id来确定不同的菜单 * * 3、顺序,那个菜单现在在前面由这个参数的大小决定 * * 4、文本,菜单的显示文本 */ menu.add(Menu.NONE, Menu.FIRST + 1, 5, "删除").setIcon( android.R.drawable.ic_menu_delete); // setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以 // android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的 menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon( android.R.drawable.ic_menu_edit); menu.add(Menu.NONE, Menu.FIRST + 3, 6, "帮助").setIcon( android.R.drawable.ic_menu_help); menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon( android.R.drawable.ic_menu_add); menu.add(Menu.NONE, Menu.FIRST + 5, 4, "详细").setIcon( android.R.drawable.ic_menu_info_details); menu.add(Menu.NONE, Menu.FIRST + 6, 3, "发送").setIcon( android.R.drawable.ic_menu_send); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Menu.FIRST + 1: Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST + 2: Toast.makeText(this, "保存菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST + 3: Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST + 4: Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST + 5: Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show(); break; case Menu.FIRST + 6: Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show(); break; } return false; } @Override public void onOptionsMenuClosed(Menu menu) { Toast.makeText(this, "选项菜单关闭了", Toast.LENGTH_LONG).show(); } @Override public boolean onPrepareOptionsMenu(Menu menu) { Toast.makeText(this, "选项菜单显示之前onPrepareOptionsMenu方法会被调用,你可以用此方法来根据打当时的情况调整菜单", Toast.LENGTH_LONG).show(); // 如果返回false,此方法就把用户点击menu的动作给消费了,onCreateOptionsMenu方法将不会被调用 return true; } } 5.运行效果
㈨ 安卓编程怎样自定义menu中的字体大小
一、先到AndroidManifest.xml看看当前的theme是什么:
比如我这里的是AppTheme
[html] view plain
<application
......
android:theme="@style/MyAppTheme" >
二、然后在资源文件的res/values/styles.xml中找到 你的主题:
[html] view plain
<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
三、然后在此添加上一个item,name=android:actionMenuTextAppearance,然后引用你自己定义的文字样式,不管是大小还是颜色都可以自己定义
[html] view plain
<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionMenuTextAppearance">@style/MyMenuTextStyle</item>
</style>
<style name="MyMenuTextStyle">
<item name="android:textColor">@android:color/red</item>
<item name="android:textSize">16sp</item>
</style>
㈩ 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:菜单项默认状态是否被激活