简体   繁体   English

如何创建自定义 SearchView?

[英]How to create a Custom SearchView?

  <style name="MySearchViewStyle" parent="Widget.AppCompat.Light.SearchView">
        <item name="android:layout">@layout/custom_search_layout</item>
   </style>

I created this but don't know where to put that.我创建了这个,但不知道把它放在哪里。 I need to change the Underline of SearchView (is in toolbar) to an image.我需要将SearchView的下划线(在工具栏中)更改为图像。 I don't no how to do that.我不知道怎么做。 please help me if anyone know how to do that or reference link would be great help.[any help to draw xml file like this image如果有人知道怎么做,请帮助我,或者参考链接会很有帮助。[任何帮助绘制像这张图片这样的 xml 文件

在此处输入图片说明

This will work for sure.这肯定会起作用。

SearchView mSearchView = (SearchView)menu.findItem(R.id.search).getActionView(); try { Field searchField = SearchView.class.getDeclaredField("mSearchButton"); searchField.setAccessible(true); ImageView searchBtn = (ImageView)searchField.get(mSearchView); searchBtn.setImageResource(R.drawable.search_img); searchField = SearchView.class.getDeclaredField("mSearchPlate"); searchField.setAccessible(true); LinearLayout searchPlate = (LinearLayout)searchField.get(mSearchView); ((ImageView)searchPlate.getChildAt(0)).setImageResource(R.drawable.search_img); searchPlate.setBackgroundResource(R.drawable.edit_text_bkg); // set your custom background here } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); }

use this custom background drawable -使用这个自定义背景可绘制 -

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:bottom="1dp"
    android:left="0dp"
    android:right="0dp"
    android:top="-2dp">
    <shape android:shape="rectangle">
        <stroke
            android:width="1dp"
            android:color="#FE8059" />
    </shape>
</item>
</layer-list>

I've tested and it works fine.我已经测试过了,它工作正常。

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

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