Android中的SearchView展开/折叠animation

我拥有的

我有一个SearchView,它完美的工作。 但是当我触摸它时,它就会从无到有。 没有过渡animation,所以它看起来不太好。

我想要的是

我想要一个简单的幻灯片左侧和滑动右侧的animation在分别展开和折叠时在SearchView上播放。

我所试过的

SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); //Get the ID for the search bar LinearLayout int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null); //Get the search bar Linearlayout LinearLayout searchBar = (LinearLayout) searchView.findViewById(searchBarId); //Give the Linearlayout a transition animation. searchBar.setLayoutTransition(new LayoutTransition()); 

但searchBar始终为空,所以我不能在其上设置布局转换。

我可以得到一个解决scheme吗? 我的方法是否正确?

Solutions Collecting From Web of "Android中的SearchView展开/折叠animation"

实际上,您可以通过执行以下操作来获取LinearLayout:

 LinearLayout searchBar = (LinearLayout) searchView.findViewById(R.id.search_bar); 

所以,

 SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); final int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null); LinearLayout searchBar = (LinearLayout) searchView.findViewById(searchBarId); searchBar.setLayoutTransition(new LayoutTransition()); 

在阅读@Steven的答案后,我得到了下面给出的解决scheme

onCreateOptionsMenu(Menu menu)方法里面写代码

  getMenuInflater().inflate(R.menu.meet_people_menu, menu); final MenuItem searchItem = menu.findItem(R.id.app_bar_search); SearchView searchView = (SearchView) searchItem.getActionView(); LinearLayout searchBar = (LinearLayout) searchView.findViewById(R.id.search_bar); searchBar.setLayoutTransition(new LayoutTransition()); 

这是我的meet_people_menu.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="com.jz.cso.activities.MeetPeopleActivity"> <item android:title="" android:id="@+id/app_bar_search" android:icon="@drawable/ic_search_black_24dp" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="always" ></item> </menu> 
 View searchBar = searchView.findViewById(R.id.search_bar); if (searchBar != null && searchBar instanceof LinearLayout) { ((LinearLayout) searchBar).setLayoutTransition(new LayoutTransition()); }