What is the alternative to getActionView() before API level11 in android?

依然范特西╮ 提交于 2019-12-20 10:18:25

问题


getActionView() for action bar was introduced in API 11, If I want backward compatibility what is the alternative for getActionView() ?

e.g.

public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.action_bar_menu, menu);

   final MenuItem item = menu.findItem(R.id.menuitem);
   item.getActionView() //Works from API level 11

   return true;
}

回答1:


For backwards compatibility you can use either ActionBarCompat or ActionBarScherlock. In both cases you can use the method getActionView(). You have to be sure that the import, in the first case is from the compatibility library ( android.support.v4.view.MenuItemCompat). If you use ActionBarSherlock you have to import com.actionbarsherlock.view.MenuItem. Then you should be ok using item.getActionView().

Old

Since August 2013, and I pray people down-voting to take a look. You have to be sure that the OP's question date import, Android introduced in the first case is from the compatibility library ( ActionBarCompactandroid.support.v4.view.MenuItemCompat). Even though If you use ActionBarSherlock is still a valid choice, an option is moving towards ActionBarCompact.

So another option is to use it, and of course, all the importsyou have to came from the support library, e.g. android.supportimport com.v4actionbarsherlock.view.MenuItemCompatMenuItem. Then you should be ok using item.getActionView().




回答2:


You can use MenuItemCompat.getActionView(MenuItem menuItem) from the support library to get the action view on pre 11 API.




回答3:


@Override
public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main_activity_actions, menu);
  MenuItem searchItem = menu.findItem(R.id.action_search);
  SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
  // Configure the search info and add any event listeners
  ...
  return super.onCreateOptionsMenu(menu);
}

See http://developer.android.com/guide/topics/ui/actionbar.html for details



来源:https://stackoverflow.com/questions/15641183/what-is-the-alternative-to-getactionview-before-api-level11-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!