A. 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
}
});
與普通按鈕沒有什麼區別,一個就是有圖片的,一個沒有嘛,對它做事件都 一樣,只不過顯示效果不一樣。
B. 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特性和使用匯總(一)》
C. 我剛學Android不久,用系統自帶的Menu,加了圖片後怎麼沒顯示出來
4.2上的optionsMenu是放到actionbar上面了,必須要在xml文件中設置showAsAction="always"活著showAsAction="ifRoom"才能在actionbar上顯示。只有在actionBar上顯示的菜單,才會有圖標。要在代碼中設置的話,menu.findItem(id).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)。圖上面的菜單顯示應該是老掉牙的系統了,沒有遵循android design guidelines。
D. Android的界面是如何適配多樣化屏幕
在研究Android的icon設計之前,有必要先了解Android的界面是如何適配多樣化屏幕的。
適配性
上一篇博文中提到,由於同一個UI元素(如100 x100像素的圖片)在高精度的屏幕上要比低精度的屏幕上看起來要小,為了讓這兩個屏幕上的圖片看起來效果差不多,可以採用以下兩種方法:
程序將圖片進行縮放,但是效果較差。
為這兩個精度屏幕的手機各提供一個圖片。
但是屏幕的參數多樣化,為每一個精度的屏幕都設計一套icon,工作量大並且不能滿足程序的兼容性要求,勢必要對屏幕的分級,如在160dpi和180dpi的手機屏幕上採用同一套icon,當這套icon在240dpi效果滿足不了設計要求,就需要另做一套稍大些的icon。
在Android 1.5以及更早的版本中,只支持3.2″ 屏幕上的HVGA (320×480)解析度,開發人員也不需要考慮界面的適配性問題。從Android 1.6之後,平台支持多種尺寸和解析度的設備,這也就意味著開發人員在設計時要考慮到屏幕的多樣性。
為了簡化設計並且兼容更多的手機屏幕,平台依照尺寸和解析度對屏幕進行了區分:
三種尺寸:大,中,小。
三種精度:高(hdpi),中(mdpi)和低(ldpi)。
程序可以為這三種尺寸的屏幕提供默認資源,如有需要,還可以為各種精度的屏幕提供資源。在運行時,系統會根據屏幕布局載入正確尺寸或者精度的圖片。
iPhone的icon設計就這么簡單,iPhone的屏幕只有320×480像素,所以程序沒有適配性問題。
參考文章:
《Icon Design Guidelines, Android 2.0》
《iPhone human interface guidelines》
本文由站酷網-zystoo翻譯,轉載請保留此信息,多謝合作。
E. android如何調用系統的隱藏方法
比如如果要在安卓4.0以上顯示菜單的圖標
java">publicvoidenableMenuIcon(android.widget.Menumenu){
if(android.os.Build.VERSION.SDK_INT<14)return;//系統在4.0以下時禁用
try{
Classclz=Class.forName("com.android.internal.view.menu.MenuBuilder");//找到要調用的方法所在類
java.lang.reflect.Methodm=clz.getDeclaredMethod("setOptionalIconsVisible",boolean.class);//找到指定參數類型和參數數量的方法名
m.setAccessible(true);//設置可訪問該域
m.invoke(menu,true);//模擬這個動態的對象進行調用該方法,並附加方法參數
}catch(Throwablee){}
}