Android SearchView定制化

7

我是一个新手安卓开发者,但我遇到了一些问题,这些问题看起来很简单,但令我感到困惑。 我需要在我的相对布局中创建一个自定义的SearchView,而不是在ActionBar / Toolbar中创建。 问题是我不知道如何在XML中自定义背景,文本输入颜色,搜索图标颜色或者其他属性。目前我只能用手机打字,无法展示我的代码。 有人可以告诉我如何自定义吗?或者有任何教程可以提供给我吗? 非常感谢!!!

4个回答

9
请在您的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>

别忘了更改这些图标。

并且请遵循这个SearchView,以了解SearchView的每个选项。


5

只需创建自己的搜索视图,这很简单。

custom_searchview.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>

您可以将此布局包含在活动布局文件中。这是一个简单的布局,包括“搜索图标”,然后是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

    }
};

谢谢,我会尝试这个。 - user5807086

3
我建议您查看我在这里提供的答案,以样式化自己的SearchBar。
我不知道原因,但我无法样式化Google默认的SearchBar,所以我不得不创建自己的SearchBar。
这是我做的方式: Android上的搜索视图样式(min21) 如果您想实现InstantSearch算法,请关注这位伟大的作者及其项目(请参阅已接受的答案): 如何使用SearchView过滤RecyclerView 如果您需要进一步的帮助,请留言。如果答案有所帮助,请点赞我的回答:P
更新:这是Google的Search View实现,只是为了给您更多信息: https://developer.android.com/guide/topics/search/search-dialog.html 祝您愉快 :)

0

frament_active.xml

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 tools:context=".Fragment.ActiveFragment">

 <LinearLayout
    android:layout_margin="30dp"
    android:background="@drawable/searchview_background"
    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="25dp"
        android:layout_height="25dp"
        android:adjustViewBounds="true"
        android:visibility="gone"
        android:src="@drawable/ic_search"/>


    <EditText
        android:padding="16dp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text"
        android:imeOptions="actionSearch"
        android:hint="Search Items"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:lines="1"
        android:textColorHint="#653201"
        android:textColor="#653201"
        android:textSize="14sp"
        android:background="@android:color/transparent"/>

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

 </LinearLayout>

searchview_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="#EFE0BB"/>
<stroke android:color="#EFE0BB" android:width="1dp"/>
</shape>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接