更改SearchView按钮的背景颜色并查询

时间:2016-12-20 19:25:53

标签: android searchview

出于某种原因,我的searchview有一个白色背景而不是透明,我似乎无法摆脱它。我正在使用AppCompat主题。我手动使用ToolBar作为ActionBar

menu.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="com.exposure.controls.ExposureSearchView" />
</menu>

enter image description here

<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorBackground">@color/app_background</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="searchViewStyle">@style/MySearchViewStyle</item>
    </style>

    <style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
        <item name="queryBackground">@android:color/transparent</item>
        <item name="submitBackground">@android:color/transparent</item>
    </style>

4 个答案:

答案 0 :(得分:3)

我将这些背景更新为透明且有效。

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="android:background" tools:ignore="NewApi">@android:color/transparent</item>
        <item name="background">@android:color/transparent</item>

答案 1 :(得分:0)

我不确定它是否有效,在您的发布主题中将<item name="android:background">@android:color/transparent</item>更改为<item name="android:background">@color/app_background</item>

答案 2 :(得分:0)

试试这种方式

<style name="searchButtonStyle" parent="Widget.AppCompat.Light.ActionButton.Overflow">
        <item name="android:background">@color/your_color</item>
        <item name="background">@color/your_color</item>
</style>

将此添加到您的样式中,如

<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">  
        <item name="android:actionOverflowButtonStyle">@style/searchButtonStyle</item>
        <item name="actionOverflowButtonStyle">@style/searchButtonStyle</item>
</style>

答案 3 :(得分:0)

    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
            SearchManager searchManager = (SearchManager) getSystemService(Activity.SEARCH_SERVICE);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    // Setting up custom search button icon
            int searchImgId = android.support.v7.appcompat.R.id.search_button;    // I used the explicit layout ID of searchview's ImageView
// Use Whatever icon you want over here in place of ic_search_black
            ImageView v = (ImageView) searchView.findViewById(searchImgId);
            v.setImageResource(R.drawable.ic_search_black);
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    // Do operation
                    return true;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }
            });