3

在我的活动中,我想在 ActionBar 图标左侧显示箭头,所以在活动中我写:

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

它工作正常,但我决定将ActionBar初始化移动到另一个类并将其用于我的应用程序中的所有活动,为此新类:

    public class Utils {
    public static void initActionBar(Activity activity, boolean homeIconNeeded) {
        ActionBar actionBar = activity.getActionBar();
        actionBar.setIcon(R.drawable.logo);
        actionBar.setHomeButtonEnabled(homeIconNeeded);
        actionBar.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.action_bar_background));
    }

    public static void initActionBar(ActionBar actionBar, boolean homeIconNeeded) {
        actionBar.setIcon(R.drawable.logo);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setBackgroundDrawable(SmartVmsApplication.getContext().getResources().getDrawable(R.drawable.action_bar_background));
    }
}

然后在我的活动中插入onCreate()回调initActionBar(this, true),但是箭头没有出现,无论我传递Activity还是ActionBar作为参数,这是一个问题。

4

1 回答 1

2

您忘记调用该setDisplayHomeAsUpEnabled()方法。

于 2012-10-29T16:48:41.280 回答