Android SearchView自定义

5 android android-layout android-search

我刚接触到android并且我被困在一些我认为很简单但我感到困惑的东西..我需要在动作栏/工具栏中但不是在我的Relativelayout中制作自定义searchView.问题是我不知道如何定制背景,textinput颜色,XML中的搜索图标颜色,或只是他们的属性.目前我在手机上,无法显示我的代码.有谁告诉我如何定制它?也许向我展示我可以遵循的任何教程?提前致谢 !!!!

AGM*_*zim 6

在您的RelativeLayout- 上添加此代码

<android.support.v7.widget.SearchView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAsh"
        android:backgroundTint="@color/colorAsh"
        android:searchIcon="@drawable/ic_search"
        android:commitIcon="@drawable/ic_commit"
        android:searchHintIcon="@drawable/ic_search" 
        android:closeIcon="@drawable/ic_close" 
        android:goIcon="@drawable/ic_go">

    </android.support.v7.widget.SearchView>
Run Code Online (Sandbox Code Playgroud)

别忘了改变这些图标.

并按照此SearchView 了解每个选项SearchView .


Mik*_*Kim 5

只需要做自己的搜索视图即可,非常简单。

自定义搜索视图.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:gravity="center_vertical">

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_search_white"/>

    <EditText
        android:id="@+id/edt_search_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text"
        android:imeOptions="actionSearch"
        android:hint="@string/by_name"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:lines="1"
        android:textColorHint="@color/colorTextGrey"
        android:textColor="@color/white"
        android:textSize="14sp"
        android:background="@android:color/transparent"/>

    <ImageView
        android:id="@+id/iv_clear_text"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:adjustViewBounds="true"
        android:visibility="gone"
        android:src="@drawable/ic_clear"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后您可以将此布局包含在您的活动布局文件中。这是一个简单的布局,其中包括一个“搜索图标”,后跟 EditText,然后是“清除图标”。用户输入一些文本后会显示清晰的图标。

然后在您的活动中,您需要在用户单击键盘上的搜索时监听,并在用户输入文本以显示“清除图标”时监听

    /*search btn clicked*/
    edtSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                performSearch();
                return true;
            }
            return false;
        }
    });

    /*hide/show clear button in search view*/
    edtSearchText.addTextChangedListener(searchViewTextWatcher);


TextWatcher searchViewTextWatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if(s.toString().trim().length()==0){
            ivClearText.setVisibility(View.GONE);
        } else {
            ivClearText.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
};
Run Code Online (Sandbox Code Playgroud)


小智 3

我建议您查看我在这里给出的答案,以设计您自己的搜索栏。

我不明白原因,但我无法设置 Google 的默认搜索栏的样式,因此我必须创建自己的搜索栏。

我是这样做的:

Android 上的搜索视图样式 (min21)

如果您想实现 InstantSearch 算法,请关注这位伟大的作者和他的项目(查看已接受的答案):

如何使用 SearchView 过滤 RecyclerView

如果您需要进一步的帮助,请发表评论。如果答案有帮助也请投票我的答案:P

更新:这是 Google 的搜索视图实现,只是为了给您提供更多信息:https://developer.android.com/guide/topics/search/search-dialog.html

祝你今天过得愉快 :)