android ActionBarSherlock使用說明

來源:互聯網
上載者:User

標籤:

原始碼地址: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使用說明

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.