【问题标题】:Android SearchView customisationAndroid SearchView 自定义
【发布时间】:2017-07-19 20:18:28
【问题描述】:

我是 android 新手,我被困在一些我认为很简单但我很困惑的东西上。 我需要在我的相对布局中而不是在操作栏/工具栏中创建自定义 searchView。问题是我不知道如何自定义背景、文本输入颜色、XML 中的搜索图标颜色,或者只是它们的属性。目前我在我的手机上,无法显示我的代码。有谁告诉我如何自定义它?也许向我展示我可以遵循的任何教程?提前谢谢!!!!

【问题讨论】:

    标签: android android-layout android-search


    【解决方案1】:

    将此代码添加到您的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 的每个选项。

    【讨论】:

      【解决方案2】:

      只要做你自己的搜索视图,很简单。

      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
      
          }
      };
      

      【讨论】:

      • 谢谢,我也试试这个???
      【解决方案3】:

      我建议你看看我在这里给出的答案来设置你自己的 SearchBar 样式。

      我不知道原因,但我无法设置 Google 的默认搜索栏的样式,所以我不得不创建自己的搜索栏。

      我是这样做的:

      Styling Search View on Android (min21)

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

      How to filter a RecyclerView with a SearchView

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

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

      祝你有美好的一天:)

      【讨论】:

        【解决方案4】:

        frame_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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-08
          • 1970-01-01
          • 2020-04-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多