【发布时间】:2015-05-07 20:53:48
【问题描述】:
我正在使用来自support.v7 库的SearchView。我查看了this post,这表明您可以通过设置LayoutTransition 来简单地为搜索视图设置动画
LinearLayout searchBar = (LinearLayout) searchView.findViewById(searchBarId);
//Give the Linearlayout a transition animation.
searchBar.setLayoutTransition(new LayoutTransition());
但是以上方法对我不起作用。搜索视图似乎也只填充了横向 中的部分屏幕宽度。我想要它fill_parent
我试图通过创建一个简单的动画来解决这两个问题,但在searchView.getLayoutParams(); 失败了,因为它似乎总是返回 null。
最终目标是在操作栏中有一个 SearchView 可以平滑地展开/折叠并占据横向屏幕宽度的宽度。
有没有办法做到这一点?
这是我目前拥有的搜索视图设置:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.menu_main, menu);
//Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
//Returns null
//searchView.getLayoutParams();
return true;
}
还有menu_main.xml
<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/search"
android:title="Search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
【问题讨论】:
-
更新为包含菜单 xml。
标签: android android-support-library searchview android-actionbar-compat