简体   繁体   English

Android:动态更改ActionBar图标?

[英]Android: dynamically change ActionBar icon?

I would like to dynamically change the "home" icon in the ActionBar. 我想动态更改ActionBar中的“home”图标。 This is easily done in v14 with ActionBar.setIcon(...), but I can't find anyway to accomplish this in previous versions. 这可以在v14中使用ActionBar.setIcon(...)轻松完成,但我无法在以前的版本中找到完成此操作。

If your actionbar works like Sherlock and is based on menu items, this is my solution: 如果您的操作栏像Sherlock一样工作并且基于菜单项,这是我的解决方案:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem switchButton = menu.findItem(R.id.SwitchSearchOption);     
    if(searchScriptDisplayed){
        switchButton.setIcon(R.drawable.menu_precedent);
    }else{
        switchButton.setIcon(R.drawable.icon_search);
    }
    return super.onPrepareOptionsMenu(menu);

}

If you are using the ActionbarCompat code provided by google, you can access the home icon via the ActionBarHelperBase.java class for API v4 onwards. 如果您使用的是Google提供的ActionbarCompat代码,则可以通过ActionBarHelperBase.java类访问主页图标,以获取API v4及更高版本。

    //code snippet from ActionBarHelperBase.java
    ...
    private void setupActionBar() {
    final ViewGroup actionBarCompat = getActionBarCompat();
    if (actionBarCompat == null) {
        return;
    }

    LinearLayout.LayoutParams springLayoutParams = new LinearLayout.LayoutParams(
            0, ViewGroup.LayoutParams.MATCH_PARENT);
    springLayoutParams.weight = 1;

    // Add Home button
    SimpleMenu tempMenu = new SimpleMenu(mActivity);
    SimpleMenuItem homeItem = new SimpleMenuItem(tempMenu,
            android.R.id.home, 0, mActivity.getString(R.string.app_name));
    homeItem.setIcon(R.drawable.ic_home_ftn);
    addActionItemCompatFromMenuItem(homeItem);

    // Add title text
    TextView titleText = new TextView(mActivity, null,
            R.attr.actionbarCompatTitleStyle);
    titleText.setLayoutParams(springLayoutParams);
    titleText.setText(mActivity.getTitle());
    actionBarCompat.addView(titleText);
}
...

You should be able to modify the code to the home button accessible to the activities that extend ActionBarActivity and change it that way. 您应该能够将代码修改为扩展ActionBarActivity的活动可访问的主页按钮,并以这种方式进行更改。

Honeycomb seems a little harder and it doesn't seem to give such easy access. 蜂窝似乎有点难,它似乎没有这么容易访问。 At a guess, its id should also be android.R.id.home so you may be able to pull that from the view in ActionBarHelperHoneycomb.java 猜测,它的id也应该是android.R.id.home所以你可以从ActionBarHelperHoneycomb.java中的视图中提取它

I would say you do something like this : 我会说你做这样的事情:

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_drawer);

see the link How to change the icon actionBarCompat 请参阅链接如何更改图标actionBarCompat

The ActionBar will use the android:logo attribute of your manifest, if one is provided. 如果提供了清单,ActionBar将使用清单的android:logo属性。 That lets you use separate drawable resources for the icon (Launcher) and the logo (ActionBar, among other things). 这使您可以为图标(Launcher)和徽标(ActionBar等)使用单独的可绘制资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM