我正在尝试将搜索按钮添加为活动中带工具栏的片段中的菜单项。这是menu.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_search_white_24px"
android:title="Search"
android:orderInCategory="1"
app:showAsAction="always|collapseActionView" />
在我的片段类中,我在onCreateView方法中添加了setHasOptionsMenu(true);
。这是片段类中的onCreateOptionsMenu方法。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
mSearchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
随处搜索并尝试了所有内容,但搜索按钮从未出现过。我能错过什么?
答案 0 :(得分:1)
<?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"
android:title="Search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
活动
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_dash_board, menu);
// Retrieve the SearchView and plug it into SearchManager
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setQueryHint("Search");
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final SearchView searchView = (SearchView) MenuItemCompat
.getActionView(menu.findItem(R.id.action_search));
}
答案 1 :(得分:0)
而不是onPrepareOptionsMenu
。
@Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem mSearchMenuItem = menu.findItem(R.id.mi_search);
SearchView searchView = (SearchView) mSearchMenuItem.getActionView();
}
答案 2 :(得分:0)
使用以下代码。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
mSearchView.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
答案 3 :(得分:0)
您的菜单
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_search"
android:title="@string/search"
android:orderInCategory="2"
android:actionLayout="@layout/seacrhlayout"
android:icon="@drawable/cart"
app:showAsAction="always"
/>
</menu>
您的行动布局
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:padding="5dp">
<RelativeLayout
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:id="@+id/main">
<ImageView
android:id="@+id/seacrh"
android:src="@drawable/search"
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</FrameLayout>
你的oncreatemenuoption
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
MenuItemCompat.setActionView(item, R.layout.seacrhlayout);
View view = (View) MenuItemCompat.getActionView(item);
ImageView search=view.findViewById(R.id.seacrh);
//do whatever you want to don on search button
return true;
}
// please check your tool bar color and the icon color of the search. may be due to color its not visible