導航:首頁 > 操作系統 > 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的使用相關的資料

熱點內容
豌豆莢app上有什麼游戲 瀏覽:283
公路商店app標簽選什麼 瀏覽:337
linuxoracle命令行登錄 瀏覽:224
android深度休眠 瀏覽:169
php微信開發例子 瀏覽:843
醫得app登錄密碼是什麼 瀏覽:140
spring開發伺服器地址 瀏覽:411
伺服器上如何查看伺服器的埠 瀏覽:678
單片機伺服器編譯 瀏覽:770
單口usb列印機伺服器是什麼 瀏覽:859
戰地五開伺服器要什麼條件 瀏覽:956
在word中壓縮圖片大小 瀏覽:255
javatomcat圖片 瀏覽:419
程序員生產智能創意 瀏覽:67
匯和銀行app怎麼登錄 瀏覽:383
騰訊伺服器如何上傳源碼 瀏覽:749
單片機的原理概述 瀏覽:512
火控pdf 瀏覽:270
如何復制雲伺服器centos環境 瀏覽:988
債權pdf 瀏覽:307