我搜索了一个解决方案,并尝试了其他问题的许多解决方案,但我没有找到任何有用的解决方案。 我希望我的SearchView显示在我的ActionBar中。但它只显示在溢出菜单(三个点)中。 Mybe有人可以告诉我我的错误/解决方案。
我的menu_searchview.xml:
<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"
android:icon="@drawable/ic_magnify_white_24dp"
android:title="Search"
app:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>
</menu>
我的活动:
import android.app.Activity;
import android.support.v4.view.MenuItemCompat;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;
import de.trainer.R;
public class SearchCategoryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_category);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_searchview, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
这对我来说很好:
菜单文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_search"
app:actionLayout="@layout/search_view"
android:orderInCategory="0"
app:showAsAction="always|collapseActionView"
android:icon="@drawable/ic_action_search"
android:title="Buscar"/>
</menu>
layout / search_view:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="4dp"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:imeOptions="actionDone"
android:inputType="text"
android:textColor="@android:color/white"
android:textCursorDrawable="@null"/>
</RelativeLayout>
在您的活动中:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
try {
menu.clear();
inflater.inflate(R.menu.your_menu, menu);
RelativeLayout actionView = (RelativeLayout) menu.findItem(R.id.menu_search).getActionView();
final EditText editText = (EditText) actionView.findViewById(R.id.etSearch);
//final EditText editText = (EditText) menu.findItem(R.id.menu_search).getActionView();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (null != activityAdapter) {
activityAdapter.getFilter().filter(s);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
MenuItem menuItem = menu.findItem(R.id.menu_search);
MenuItemCompat.setOnActionExpandListener(menuItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
editText.requestFocus();
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
return true; // Return true to expand action view
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
// Borramos el texto que había escrito.
editText.setText("");
return true; // Return true to collapse action view
}
});
} catch (Exception ex) {
logManager.error("OCOM - Exception: " + ex.toString());
}
super.onCreateOptionsMenu(menu, inflater);
}
最后,在您的适配器中,您应该实现Filterable interfaz。
希望它可以帮到你!!
答案 1 :(得分:0)
替换android:actionViewClass =&#34; android.widget.SearchView&#34; 同 应用程式:actionViewClass =&#34; android.support.v7.widget.SearchView&#34;
答案 2 :(得分:0)
尝试
app:actionViewClass =“android.widget.SearchView”或
应用:actionViewClass = “android.support.v7.widget.SearchView”强>
而不是
机器人:actionViewClass = “android.widget.SearchView”
你也必须删除
android:icon =“@ drawable / ic_magnify_white_24dp”并更改以下方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_searchview, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setIconifiedByDefault(false);
return true;
}
我希望它能解决你的问题。
答案 3 :(得分:0)
从您的布局和活动中,我了解到您没有使用兼容库。在这种情况下,我建议将AXML切换到
<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"
android:icon="@drawable/ic_magnify_white_24dp"
android:title="Search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>
</menu>
注意,从 app:showAsAction 切换到 android:showAsAction
这可能导致问题。
干杯