我正在使用一个带有搜索操作按钮的工具栏,它曾被用来工作,但突然间它停止向我显示。这是我的代码:
在我的菜单文件夹中
menu_feed.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="@android:drawable/ic_menu_search"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
这是我调用该项目并将其充气到工具栏中的类
FeedFragment.class
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_feed, container, false);
setupToolbar(v);
return v;
}
private void setupToolbar(View v) {
AppCompatActivity activity = (AppCompatActivity) getActivity();
toolbar = (Toolbar) v.findViewById(R.id.toolbar);
activity.setSupportActionBar(toolbar);
setHasOptionsMenu(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupRV();
mFeedPostsPresenter = new FeedPostsPresenterImpl(this);
}
private void setupRV() {
mFeedPostsRV = (RecyclerView) getView().findViewById(R.id.feed_posts_list);
mFeedPostsRV.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mFeedPostsRV.setLayoutManager(layoutManager);
RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
itemAnimator.setAddDuration(1000);
itemAnimator.setRemoveDuration(1000);
mFeedPostsRV.setItemAnimator(itemAnimator);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_feed, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_search){
Intent i = new Intent(getActivity(), SearchUsersActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
}
由于某种原因,我不知道为什么该搜索按钮从我的工具栏中消失了,而我再也看不到,这是我menu_feed.xml的输出:
在我的应用程序中的运行时中,没有action_search:
答案 0 :(得分:0)
“ collapseActionView”允许将按钮显示为小部件。该按钮可以是动态的,就像展开的搜索按钮一样。检查以下内容:Documentation。尝试在您的 menu_feel.xml
中将其删除