Android ActionBar compatibility: MenuItem.setActionView(View)

风流意气都作罢 提交于 2019-11-30 04:48:17

问题


I'm using the appcompat7 lib for ActionBar backwards compatibility. Now I have a MenuItem that I retrieve, and then want to set an ImageView myView as its icon.

The way how to do it from API level 11 is:

MenuItem menuItemRefresh = menu.findItem(R.id.refresh);
menuItemRefresh.setActionView(myView);

For API levels lower than 11 this doesn't work, the second line will show an error. Is there an option to do this in compatibility mode?


回答1:


Look at MenuItemCompat: http://developer.android.com/reference/android/support/v4/view/MenuItemCompat.html

There is a static function setActionView(MenuItem item, View view)

So your code should look like:

MenuItem menuItemRefresh = menu.findItem(R.id.refresh);
menuItemRefresh = MenuItemCompat.setActionView(menuItemRefresh, myView);


来源:https://stackoverflow.com/questions/20402912/android-actionbar-compatibility-menuitem-setactionviewview

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