問題:
在使用Actionbar時,默認在左上角是會有一個跟軟件發布時的LOGO一樣的圖標,在大多數情況下按照默認圖標進行顯示已經很好,既使得軟件整體統一,也方便省事。但有些情況下,還是希望不同的界面左上角的圖標是不同的,或不想使用默認的LOGO,比如LOGO是有底色,放在Actionbar上不好看……
解決辦法:
在配置文件Manifest中增加android:logo="@drawable/logo_top"屬性,如果是想給整個程序添加統一的自定義圖標,則在application標簽下增加該屬性;如果是想針對不同的activity添加不同的左上角圖標,則在各自的activity標簽下增加該屬性,然后指向不同的圖片資源即可。
上面的一位網友給出的方法非常只好.
下面給出一個demo來測試一下:
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.iconshow" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.iconshow.MainActivity" android:logo="@drawable/ic_launcher_settings" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
java代碼如下:
package com.example.iconshow; import android.os.Bundle; import android.app.ActionBar; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //set icon in java,here set icon in AndroidManifest.xml, so ignore following code /* requestWindowFeature(Window.FEATURE_LEFT_ICON); getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher_settings); */ ActionBar actionBar = this.getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); setContentView(R.layout.activity_main); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub //add top-left icon click event deal switch(item.getItemId()){ case android.R.id.home: finish(); } return super.onOptionsItemSelected(item); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
運行即可以看到屏幕左上角有一個"<"的返回箭頭和在xml中指定的icon圖標(96*96pix),點擊事件是如下處理的:
@Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub //add top-left icon click event deal switch(item.getItemId()){ case android.R.id.home: finish(); } return super.onOptionsItemSelected(item); }
....