標籤:
原始碼地址:https://github.com/JakeWharton/ActionBarSherlock
1.添加項目依賴包
2.修改AndroidManifest.xml中的主題(或者繼承該主題的父樣式)
android:theme="@style/Theme.Sherlock.Light"
3.自訂的Activity必須繼承SherlockActivity類
4.重寫onCreateOptionsMenu和onOptionsItemSelected方法。
@Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.main_menu, menu); return super.onCreateOptionsMenu(menu); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case android.R.id.home://點擊應用表徵圖 Toast.makeText(this, "home as up", Toast.LENGTH_LONG).show(); break; } return super.onOptionsItemSelected(item); }
第一個方法設定menu,第二個方法設定menu的點擊事件
完成以上四步即可使用該架構
下面說一下架構中的一些小操作
1.更換左上方的表徵圖可以用一下兩種方法
getSupportActionBar().setIcon(R.drawable.e_1);
getSupportActionBar().setLogo(R.drawable.e_1);
2.設定標題
getSupportActionBar().setTitle("商品詳情");
3.設定actionbar的背景
Drawable drawable = getResources().getDrawable(R.drawable.e_1); getSupportActionBar().setBackgroundDrawable(drawable);
4.設定左上方的表徵圖可點擊的兩種方法
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
5.監聽左上方的表徵圖被點擊
@Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case android.R.id.home://點擊應用表徵圖 Toast.makeText(this, "home as up", Toast.LENGTH_LONG).show(); break; } return super.onOptionsItemSelected(item); }
6.設定標題是否可用,true可用且標題顯示,false不可用則標題隱藏
getSupportActionBar().setDisplayShowTitleEnabled(true);
7.未完待續
android ActionBarSherlock使用說明