如何删除工具栏中 SearchView 小部件中的白色下划线 Android

How to remove white underline in a SearchView widget in Toolbar Android

我在我的项目中使用工具栏搜索小部件。一切正常,但期望我完全坚持删除工具栏中搜索字段下方的下划线。我尝试了很多解决方案,但没有任何效果。以下是我尝试过的一些解决方案。

要求去除图片中的白色下划线

解法:

//Removing underline

    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
    View searchPlate = searchView.findViewById(searchPlateId);
    if (searchPlate!=null) {
        searchPlate.setBackgroundColor (Color.TRANSPARENT);
        int searchTextId = searchPlate.getContext ().getResources ().getIdentifier ("android:id/search_src_text", null, null);

    }

第二解:

 EditText searchEdit = ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text));
 searchEdit.setBackgroundColor(Color.TRANSPARENT);

上面的代码可以改变 EditText 的背景,但下划线仍然显示在 SearchBar 的关闭图标下方。

我用于 SearchBar 小部件的完整代码如下:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater infl) {
        super.onCreateOptionsMenu(menu, infl);
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.action_search, menu);
        final SearchView searchView = (SearchView) MenuItemCompat
                .getActionView(menu.findItem(R.id.search));

        SearchManager searchManager = (SearchManager) getActivity ().getSystemService (getActivity ().SEARCH_SERVICE);
        searchView.setSearchableInfo (searchManager.getSearchableInfo (getActivity ().getComponentName ()));

        //changing edittext color
        EditText searchEdit = ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text));
        searchEdit.setTextColor(Color.WHITE);
}

action_search.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:compat="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search"
        android:title="Search"
        android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
        compat:showAsAction="ifRoom|collapseActionView"
        compat:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

可能与 this

重复

一次尝试如下

View v = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
v.setBackgroundColor(Color.parseColor("here give actionbar color code"));

希望对您有所帮助。

或者你可以通过样式来做到这一点:

<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
    <item name="submitBackground">@color/primaryColor</item>
    <item name="queryBackground">@color/primaryColor</item>
</style>

queryBackground 是查询背景的属性。如果您支持语音搜索,您可能还想删除该麦克风按钮下方的线路。这就是 submitBackground 属性的用途。

然后当然要将样式应用到您的主题中。

<style name="Theme.App" parent="Theme.AppCompat.Light.NoActionBar.FullScreen">
    <item name="searchViewStyle">@style/SearchViewMy</item>
</style>

如果您使用的是 SearchView,则使用 for API below 21

android:queryBackground="@android:color/transparent"

如果你使用 API 21 或更多

,你可以使用下面的代码
app:queryBackground="@android:color/transparent"

更改SearchView编辑文字背景颜色的代码如下

public static void setSearchViewEditTextBackgroundColor(Context context, SearchView searchView, int backgroundColor){
    int searchPlateId = context.getResources().getIdentifier("android:id/search_plate", null, null);
    ViewGroup viewGroup = (ViewGroup) searchView.findViewById(searchPlateId);
    viewGroup.setBackgroundColor(ComponentUtils.getColor(context, backgroundColor));
}

如果你的action bar背景颜色是白色那么调用上面的方法如下。

setSearchViewEditTextBackgroundColor(this, searchView, R.color.white);

现在 SearchView 编辑文本的下划线将消失。

您可以尝试使用下面的代码,对我来说很有用。

View v = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    v.setBackgroundColor(ContextCompat.getColor(context,android.R.color.transparent));

如果您使用的是 v7 或 androidx 小部件,只需添加此行

app:queryBackground="@android:color/transparent"

对于 AndroidX,我根据此处的其他答案使用了它,

searchView.findViewById(androidx.appcompat.R.id.search_plate)
        .setBackgroundColor(Color.TRANSPARENT)

首先你应该像这样简单地了解底线:

searchPlate = (View) findViewById(R.id.search_plate);

然后你应该像这样将背景颜色设置为透明:

searchPlate.setBackgroundColor(Color.TRANSPARENT);

这非常适合 androidx.appcompat.widget.SearchView!

下面的代码对我有用。

val searchView = menu?.findItem(R.id.action_search)?.actionView as SearchView
searchView.findViewById<View>(androidx.appcompat.R.id.search_plate).background = null

对于androidxkotlin

        menuInflater.inflate(R.menu.conversation_list_screen_menu, menu)
        val searchItem = menu.findItem(R.id.action_search)
        searchView = searchItem.actionView as SearchView
        searchView?.findViewById<View>(androidx.appcompat.R.id.search_plate)?.setBackgroundColor(Color.TRANSPARENT)

如果您想从 SearchView 中删除下划线,只需将以下代码复制粘贴到您的 SearchView 中即可。有效

 android:queryBackground="@null"

像这样,

      <SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:queryBackground="@null"
                />