【问题标题】:Android: SearchView component change icon (no ActionBar)Android:SearchView组件更改图标(无ActionBar)
【发布时间】:2015-11-23 02:49:52
【问题描述】:

如何更改 SearchView 的默认搜索图标?

<SearchView
   android:id="@+id/searchView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

我没有使用 actionBar。可能吗?怎么样?

【问题讨论】:

    标签: android android-actionbar android-appcompat searchview


    【解决方案1】:

    试试这个

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="searchViewStyle">@style/MySearchView</item>
    </style>
    
    <style name="MySearchView" parent="Widget.AppCompat.SearchView">
        <item name="searchIcon">@drawable/ic_search</item>
        <item name="closeIcon">@drawable/ic_close</item>
    </style>
    

    【讨论】:

    • 我正在使用这个主题@android:style/Theme.Holo.NoActionBar。
    【解决方案2】:

    可以使用 AppCompat。

    但我使用了小技巧:

    mSearchView = (SearchView) searchViewMenuItem.getActionView();
    int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
    ImageView v = (ImageView) mSearchView.findViewById(searchImgId);
    v.setImageResource(R.drawable.your_new_icon);  
    

    来自这个How to change the default icon on the SearchView, to be use in the action bar on Android?,看起来还不错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-24
      • 2012-08-05
      • 2021-06-09
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多