基本上,当有一个图像按钮时,我希望搜索 View 展开和折叠,单击图标时图像按钮后面应该消失,搜索 View 折叠时应该出现 View 。
我使用的是不在操作栏中的搜索 View 。当我单击搜索图标时,它只扩展了一半屏幕,但它应该扩展完整的宽度,但当我关闭搜索 View 时它应该 wrap_content。
<SearchView
android:id="@+id/searchView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
我也试过:
android.view.ViewGroup.LayoutParams params = searchview.getLayoutParams();
params.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
但我在这里遇到错误 setLayoutParams 它说 "add to cast"
最佳答案
一开始你会得到一个异常,因为如果你查看位于
的 search_view.xml\Android\android-sdk\platforms\android-18\data\res\layout\
您会发现 SearchView 基本上是将多个小部件合并在一起的简单 LinearLayout。这就是为什么你得到类转换异常。而不是
android.view.ViewGroup.LayoutParams params = searchview.getLayoutParams();
使用
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) searchview.getLayoutParams();
或者只导入正确的类然后你就可以简单地使用它
import android.widget.LinearLayout.LayoutParams;
...
LayoutParams params = (LayoutParams) searchview.getLayoutParams();
你可以使用不带任何前缀的 LayoutParams。
但我不认为设置 LayoutParams
来包装内容会有帮助。我会将您的 SearchView
和 ImageButton
包装到 RelativeLayout
中,您可以在其中指定相对于其他 View 的 View 位置。然后只需将 SearchView
宽度设置为填充父级,这样它就可以填充剩余空间。
关于android - 搜索 View 未展开全宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19553961/