1

I have read through everything question I can find, and tried so many things but nothing is working. A lot of answers seem to relate to the v7 support library. I am using AndroidX and Kotlin. I would like to change the color of searchIcon and closeIcon to white. I would also like to remove the searchHintIcon.

AndroidManifest.xml

    <activity
        android:name=".MainActivity"
        android:theme="@style/AppTheme"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Menu

    <item
    android:id="@+id/action_search"
    android:orderInCategory="1"
    app:showAsAction="ifRoom"
    android:title=""
    app:actionViewClass="androidx.appcompat.widget.SearchView" />

styles.xml (values-v21)

<resources>

<style
    name="AppTheme"
    parent="@style/Theme.AppCompat.Light.DarkActionBar">

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    
    <!--changes the search text to white -->
    <item name="android:editTextColor">@color/white</item>

    <item name="searchViewStyle">@style/AppTheme.SearchView</item>
</style>

<style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
    <item name="searchIcon">@drawable/ic_action_search_white</item>
    <item name="android:searchIcon">@drawable/ic_action_search_white</item>
    <item name="android:closeIcon">@drawable/ic_action_clear_white</item>
    <item name="closeIcon">@drawable/ic_action_clear_white</item>
    <item name="searchHintIcon">@null</item>
</style>

</resources>

I am not using a toolbar, just the action bar. I would like to change the icon color using xml if possible. Happy to add a toolbar if that helps.

4

1 回答 1

0

我终于想通了,所以我会回答我自己的问题。我已将菜单更改为:

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_search"
    android:title="Search"
    android:icon="@drawable/ic_action_search_white"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="androidx.appcompat.widget.SearchView"/>

collapseActionView修复了该问题并允许拾取图标。我拒绝使用它,因为我的行为很奇怪。

一旦我更新了 OnCreateOptionsMenu 一切都按预期工作并且图标是白色的

override fun onCreateOptionsMenu(menu: Menu): Boolean {

    menuInflater.inflate(R.menu.menu_main, menu)
    menuInflater.inflate(R.menu.menu_search, menu)

    sv = menu.findItem(R.id.action_search).actionView as SearchView
    sv.isSubmitButtonEnabled = false
    sv.setOnQueryTextListener(this)

    return true
}

为了完整起见,这里是其余的代码:

override fun onQueryTextSubmit(query: String?): Boolean {
    getData(query.toString())
    return true
}

override fun onQueryTextChange(newText: String?): Boolean {
    getData(newText.toString())
    return true
}

private fun getData(newText: String) {
    searchTerm = "%".plus(newText).plus("%")
    //todo ... get the data from the db
}
于 2020-07-27T00:23:58.570 回答