问题描述
我刚刚实施的 V7 AppCompat
支持库,但 MenuItemCompat.getActionView
总是在每一个Android版本返回null我测试(4.2.2,2.3.4 ....)
在搜索查看
显示在操作栏中,但它不响应触摸操作,不扩张,以显示其的EditText
键,就像是一个简单的图标。
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.menu,菜单);
菜单项searchItem = menu.findItem(R.id.action_search);
搜索查看搜索查看=(搜索查看)MenuItemCompat.getActionView(searchItem);
如果(搜索查看!= NULL){
SearchViewCompat.setOnQueryTextListener(搜索查看,mOnQueryTextListener);
searchView.setIconifiedByDefault(假);
Log.d(TAG,搜索查看非空);
} 其他
Log.d(TAG,搜索查看为空);
}
返回super.onCreateOptionsMenu(菜单);
}
menu.xml文件
< XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto>
<项目机器人:ID =@ + ID / ACTION_SEARCH
应用程序:showAsAction =永远| collapseActionView
机器人:图标=@可绘制/ abc_ic_search
机器人:标题=@字符串/ action_bar_search
机器人:actionViewClass =android.support.v7.widget.SearchView/>
<项目机器人:ID =@ + ID / action_refresh
机器人:图标=@可绘制/刷新
机器人:标题=@字符串/ action_bar_refresh
应用程序:showAsAction =ifRoom/>
< /菜单>
最后,我找到了解决办法。
-
改变
actionViewClass
的命名空间,安卓actionViewClass
到应用程序:actionViewClass
-
实施
android.support.v7.widget.SearchView.OnQueryTextListener
接口,用于当前的活动。 -
直接用
setOnQueryTextListener
而不是SearchViewCompat.setOnQueryTextListener
@覆盖 公共布尔onCreateOptionsMenu(功能菜单){ MenuInflater充气= getMenuInflater(); inflater.inflate(R.menu.menu,菜单); 菜单项searchItem = menu.findItem(R.id.action_search); 搜索查看搜索查看=(搜索查看)MenuItemCompat.getActionView(searchItem); 如果(搜索查看!= NULL){ searchView.setOnQueryTextListener(本); } 返回super.onCreateOptionsMenu(菜单); }
I just implemented the v7 AppCompat
support library but the MenuItemCompat.getActionView
always return null in every Android version I tested (4.2.2, 2.3.4 ....)
The SearchView
is displayed in action bar but it doesn't respond to touch actions and doesn't expand to show its EditText
and is just like a simple icon.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
if (searchView != null) {
SearchViewCompat.setOnQueryTextListener(searchView, mOnQueryTextListener);
searchView.setIconifiedByDefault(false);
Log.d(TAG,"SearchView not null");
} else
Log.d(TAG, "SearchView is null");
}
return super.onCreateOptionsMenu(menu);
}
Menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
app:showAsAction="always|collapseActionView"
android:icon="@drawable/abc_ic_search"
android:title="@string/action_bar_search"
android:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="@+id/action_refresh"
android:icon="@drawable/refresh"
android:title="@string/action_bar_refresh"
app:showAsAction="ifRoom"/>
</menu>
Finally I found the solution.
Changing namespace of
actionViewClass
fromandroid:actionViewClass
toapp:actionViewClass
Implementing
android.support.v7.widget.SearchView.OnQueryTextListener
interface for current activity.Directly use
setOnQueryTextListener
instead ofSearchViewCompat.setOnQueryTextListener
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); if (searchView != null) { searchView.setOnQueryTextListener(this); } return super.onCreateOptionsMenu(menu); }
这篇关于MenuItemCompat.getActionView总是返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!