當前位置:首頁 » 安卓系統 » androidmenu

androidmenu

發布時間: 2023-07-22 16:22:54

① 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怎樣設置item的文字

Android Menu的文字有兩種設置方式:

  1. 在 menu.xml中設置。

  2. java代碼onCreateOptionsMenu中設置。

以下為示例代碼:

  1. 在menu.xml中指定title即可

    <item android:id="@+id/menu"
    android:title="文字"/>

  2. 重載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、文本,菜單項的顯示文本。

③ android menu 怎麼得到item

android 中的menu一般是指上下文菜單或者是選項菜單

其中上選項菜單是可以在布局中res下的menu中在xml布局中寫好布局來的然後通過java代碼中的onCreateOptionsMenu來載入選項菜單,android4.4高級版本後是自動把菜單載入到標題欄上的,而不是低版本的按下menu鍵才顯是出來的,上下文菜單是是通過onCreateContextMenu這個方法來注冊上下文菜單的

下面講講如何獲取menu中的item

獲取上下文菜單的item其實就是當單機選項菜單時會觸發這個方法

(MenuItemmi){
//判斷單擊的是哪個菜單項,並針對性的作出響應。
switch(mi.getItemId()){
caseFONT_RED:
title.setTextColor(Color.RED);
break;
caseFONT_GREEN:
title.setTextColor(Color.GREEN);
break;
caseFONT_BLUE:
title.setTextColor(Color.BLUE);
break;
caseMENU1:
createdialog();
break;
}
returntrue;
}

獲取選項菜單的item其實就是當單擊選項菜單時會觸發這個方法

(intfeatureId,MenuItemitem){
//利用switch根據ItemId區分點擊的是哪個菜單以便正確響應用戶操作
MenuItemtemp=item;
switch(item.getItemId()){
caseR.id.rename:
createdialog();
break;
caseR.id.red:
title.setTextColor(Color.RED);
break;
caseR.id.green:
title.setTextColor(Color.GREEN);
break;
caseR.id.blue:
title.setTextColor(Color.BLUE);
break;
// caseR.id.choose_color:
// createpopupmenu(temp);
// break;
}
returnsuper.onMenuItemSelected(featureId,item);
}

④ 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可能需要重寫的方法是哪些

可能需要重寫的如下:
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);
}

⑥ Android中menu菜單中的圖片是怎麼加進去的

給這個圖片按鈕添加一個事件, button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(當前Activity.this, 下一個Activity.class);
startActivity(intent);
當前Activity.finish();//關閉當前Activity
}
});
與普通按鈕沒有什麼區別,一個就是有圖片的,一個沒有嘛,對它做事件都 一樣,只不過顯示效果不一樣。

熱點內容
刺客信條4解壓後 發布:2025-02-05 06:55:23 瀏覽:901
icophp 發布:2025-02-05 06:54:26 瀏覽:763
雲伺服器如何安裝nginx 發布:2025-02-05 06:47:16 瀏覽:95
福州職場解壓方式 發布:2025-02-05 06:36:31 瀏覽:557
c語言源程序的語句分隔符是 發布:2025-02-05 06:06:05 瀏覽:304
第一彈怎麼上傳視頻 發布:2025-02-05 06:06:04 瀏覽:997
策略樹演算法 發布:2025-02-05 06:00:31 瀏覽:610
存儲光碟數據恢復 發布:2025-02-05 05:43:50 瀏覽:384
android位置信息嗎 發布:2025-02-05 05:43:45 瀏覽:440
畫師怎麼配置電腦 發布:2025-02-05 05:38:56 瀏覽:969