searchview不扩展全宽(searchview not expanding full width)

编程入门 行业动态 更新时间:2024-10-12 05:51:08
searchview不扩展全宽(searchview not expanding full width)

基本上我希望searachview展开和折叠,当后面有一个图像按钮时,当点击图标时应该消失,并且当searchview崩溃时应该显示视图。

我正在使用搜索视图不在操作栏中。当我单击搜索图标时,它只展开一半的屏幕,但它应该展开完整的宽度,但应该在关闭搜索视图时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它说“添加投”

Basically I want the searachview to expand and collapse when there is a imagebutton behind which should disappear when icon is clicked and view should appear when searchview collapses.

I am using search view not in action bar .When i click on the search icon it expands only half the screen but it should expand complete width but should wrap_content when I close the searchview.

<SearchView android:id="@+id/searchView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true">

I also tried :

android.view.ViewGroup.LayoutParams params = searchview.getLayoutParams(); params.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

But I am getting error here setLayoutParams it says "add to cast"

最满意答案

起初你会得到一个异常,因为如果你看看位于中的search_view.xml

\的Android \ Android的SDK \平台\机器人-18 \数据\ RES \布局\

你会发现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 ,在那里你可以指定相对于其他视图的视图位置。 然后,只需将SearchView宽度设置为填充父级,即可填充剩余空间。

At first you're getting an exception, because if you look into search_view.xml located in

\Android\android-sdk\platforms\android-18\data\res\layout\

You'll find out that SearchView is basically simple LinearLayout merging several widgets together. That's why you are getting class cast exception. Instead of

android.view.ViewGroup.LayoutParams params = searchview.getLayoutParams();

use

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) searchview.getLayoutParams();

or just import correct class and then you can simply use this

import android.widget.LinearLayout.LayoutParams; ... LayoutParams params = (LayoutParams) searchview.getLayoutParams();

than you can use LayoutParams without any prefix.

But I don't think that setting LayoutParams to wrap content will help. I would wrap your SearchView and ImageButton to RelativeLayout, where you could specify view position relative to the other views. Then simply set SearchView width to fill parent, so it could fill remaining space.

更多推荐

本文发布于:2023-07-26 06:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:expanding   searchview   width   full

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!