下面的布局不显示搜索图标只显示另一个图标,搜索来自其他图标的下拉菜单。我想将其作为操作栏中的单独图标
<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="com.example.diveintojava1.MainActivity" >
<item android:id="@+id/search"
android:title="search"
android:icon="@drawable/search1"
android:actionViewClass="android.support.v7.widget.SearchView"
android:showAsAction="collapseActionView|ifRoom"
/>
<item android:id="@+id/file"
android:title="drop"
android:icon="@drawable/moremenu" >
<menu>
<item android:id="@+id/create_new"
android:title="java" />
<item android:id="@+id/open"
android:title="rate us" />
</menu>
</item>
</menu>
以下是实现菜单的java代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
答案 0 :(得分:0)
机器人:showAsAction =&#34; collapseActionView | ifRoom&#34;将ifRoom更改为始终
答案 1 :(得分:0)
试试这个,点击此处了解详情:http://developer.android.com/guide/topics/ui/actionbar.html和http://sourceoncloud.wordpress.com/2013/07/26/actionbar-compact/
<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"
xmlns:appcompact="http://schemas.android.com/apk/res-auto"
tools:context="com.example.diveintojava1.MainActivity" >
<item android:id="@+id/search"
android:title="search"
android:icon="@drawable/search1"
android:actionViewClass="android.support.v7.widget.SearchView"
appcompact:showAsAction="collapseActionView|ifRoom"
/>
<item android:id="@+id/file"
android:title="drop"
android:icon="@drawable/moremenu" >
<menu>
<item android:id="@+id/create_new"
android:title="java" />
<item android:id="@+id/open"
android:title="rate us" />
</menu>
</item>
</menu>
答案 2 :(得分:0)
改变这个:
android:showAsAction="collapseActionView|ifRoom"
进入这个:
android:showAsAction="always"