在我的活动中,我想要在操作栏图标左侧显示箭头,因此在活动中我写了以下代码:
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
作为参数传递,这都是一个问题。