android ActionBarSherlock使用说明

源代码地址: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.未完待续

原文地址:https://www.cnblogs.com/tony-yang-flutter/p/4670559.html