我使用的是appcompat 7而不是sherlockactionbar。现在我想在操作栏中添加更新计数器按钮,但在onCreateOptionsMenu函数中返回null getActionView。我的代码是:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
View count = menu.findItem(R.id.update).getActionView();
TextView notifCount = (TextView) count.findViewById(R.id.textOne);
if(mNotifCount<=0) notifCount.setVisibility(View.GONE);
notifCount.setText(String.valueOf(mNotifCount));
return super.onCreateOptionsMenu(menu);
我的menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="@+id/action_example" android:title="@string/action_example"
app:showAsAction="withText|ifRoom" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item
android:title="update"
android:id="@+id/update"
android:actionLayout="@layout/feed_update_count"
app:showAsAction="always|collapseActionView"
android:orderInCategory="0" />
答案 0 :(得分:4)
将android:actionLayout
更改为app:actionLayout
并使用MenuItemCompat.getActionView(menuItem...);
答案 1 :(得分:0)
您可能需要使用MenuItemCompat来加载动作视图;
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));