基本上我希望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.
更多推荐
发布评论