我在以下活动中有这个工具栏:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_select_currency"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
此菜单也在:
中声明<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
android:title="@string/search_currency"
android:icon="@drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
在我打电话的活动中:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.currency_selector_options_menu, menu);
return true;
}
private void initActionBar() {
// Initialize the Toolbar( this is the new ActionBar in Lollipop)
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_select_currency);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getString(R.string.select_currency));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
现在我的问题是我没有在正常的ActionBar中获得SearchView,我得到的是普通菜单。我怎样才能获得正常的SearchView?
答案 0 :(得分:6)
使用您的app命名空间:
xmlns:myapp="http://schemas.android.com/apk/res-auto"
myapp:showAsAction="always"
<强>说明:强>
由于您使用的是旧版Android的支持库并且它未包含在sdk中,android
命名空间不包含showAsAction
属性,而是包含在支持库中链接到您的项目,因此要访问该属性,您必须使用您的应用程序命名空间