简体   繁体   English

searchview不扩展全宽

[英]searchview not expanding full width

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. 基本上我希望searachview扩展和折叠,当后面有一个图像按钮时,当点击图标时它应该消失,当searchview崩溃时应该出现视图。

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. 我正在使用搜索视图而不是在操作栏中。当我点击搜索图标时,它只展开了一半的屏幕,但它应该扩展完整的宽度,但是当我关闭搜索视图时应该是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">

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" 但我在这里得到错误setLayoutParams它说“添加到演员”

At first you're getting an exception, because if you look into search_view.xml located in 首先你得到一个异常,因为如果你看看位于的search_view.xml

\\Android\\android-sdk\\platforms\\android-18\\data\\res\\layout\\ \\的Android \\ Android的SDK \\平台\\机器人-18 \\数据\\ RES \\布局\\

You'll find out that SearchView is basically simple LinearLayout merging several widgets together. 您会发现SearchView基本上是简单的LinearLayout将几个小部件合并在一起。 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. 你可以使用没有任何前缀的LayoutParams。

But I don't think that setting LayoutParams to wrap content will help. 但我不认为设置LayoutParams来包装内容会有所帮助。 I would wrap your SearchView and ImageButton to RelativeLayout , where you could specify view position relative to the other views. 我将您的SearchViewImageButton包装到RelativeLayout ,您可以在其中指定相对于其他视图的视图位置。 Then simply set SearchView width to fill parent, so it could fill remaining space. 然后只需将SearchView宽度设置为填充父级,即可填充剩余空间。

Have you tried the XML-attribute android:maxWidth like described here: http://developer.android.com/reference/android/widget/SearchView.html#attr_android:maxWidth 您是否尝试过如下所述的XML属性android:maxWidth: http//developer.android.com/reference/android/widget/SearchView.html#attr_android :maxWidth

I am using the SearchView from actionbarsherlock and used the corresponding java-method to stretch the SearchView over the available width of the actionbar. 我正在使用来自actionbarsherlock的SearchView,并使用相应的java方法在操作栏的可用宽度上拉伸SearchView。

第一个截图

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/search_b"
        android:orderInCategory="3"
        android:title="Search"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView" />

    <item
        android:id="@+id/action_scan"
        android:icon="@drawable/scan_b"
        android:title="Scan"
        android:orderInCategory="2"
        app:showAsAction="ifRoom|collapseActionView" />

    <item
        android:id="@+id/action_add_cusomer"
        android:icon="@drawable/add_cust_b"
        android:title="Add Customer"
        android:orderInCategory="1"
        app:showAsAction="ifRoom|collapseActionView" />

</menu>

After Clicking on Search icon:

单击搜索后的屏幕截图

Try this according to your needs but dont think to work on your own button, you can move default searchButton to right side with giving wrap_content width parameter and then when you click to search set width to match_parent then again when you close it set width to wrap_content 根据你的需要尝试这个,但不要认为你自己的按钮工作,你可以通过给出wrap_content宽度参数将默认的searchButton移动到右侧,然后当你点击搜索设置宽度到match_parent时再关闭它设置宽度到wrap_content

Use your searchView child of RelativeLayout and be sure about right LayoutParams(RelativeLayout.LayoutParams) 使用RelativeLayout的searchView子项并确保正确的LayoutParams(RelativeLayout.LayoutParams)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/counter_text_color" >

    <Button
        android:id="@+id/buttonLeftMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:background="@drawable/ic_drawer" />

    <ImageView
        android:id="@+id/iv_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/actionbar_logo" />

    <TextView
        android:id="@+id/tw_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="YOUR "
        android:textSize="24sp" />

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/buttonRightMenu" />

    <Button
        android:id="@+id/buttonRightMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:background="@drawable/ic_action_settings" />

</RelativeLayout>

and at your class 在你的班上

searchView.setOnSearchClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.RIGHT_OF, R.id.buttonLeftMenu);
        params.addRule(RelativeLayout.LEFT_OF, R.id.buttonRightMenu);
        searchView.setLayoutParams(params);

    }
});

searchView.setOnCloseListener(new OnCloseListener() {

    @Override
    public boolean onClose() {
        // TODO Auto-generated method stub
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.LEFT_OF, R.id.buttonRightMenu);
        searchView.setLayoutParams(params);

        return false;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM