更改“在操作栏中搜索”的图标

时间:2014-01-06 14:23:01

标签: android search android-actionbar

我使用以下代码在操作栏中添加了搜索:

activity_main_actions.xml

   

<!-- Search  -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="ifRoom"
      android:actionViewClass="android.widget.SearchView"/>


<!-- Add -->
<item android:id="@+id/action_add_tip"
      android:icon="@drawable/header_add"
      android:title="@string/action_add_tip"
      android:showAsAction="ifRoom" />

<!-- Count -->
<item android:id="@+id/action_count_tip"
      android:icon="@drawable/header_count"
      android:title="@string/action_count_tip"
      android:showAsAction="ifRoom" />

MainActivity.java中的

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main_actions, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));


    return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Take appropriate action for each action item click
    switch (item.getItemId()) {
    case R.id.action_search:
        // search action
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

searchable.xml

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Take appropriate action for each action item click
    switch (item.getItemId()) {
    case R.id.action_search:
        // search action
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

的Manifest.xml

  <activity
        android:name="com.emy.healthytips.MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:launchMode="singleTop">
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".MainActivity" />

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>

搜索对我来说效果很好,但是在操作栏中搜索的图标仍然像“灰色图标”一样,如下面的ScreenShot,尽管我添加了自己的图标     <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search"

如何添加自己的图标?

enter image description here

1 个答案:

答案 0 :(得分:1)

这个问题可能与您的问题有关。请检查一下。

How to change the default icon on the SearchView, to be use in the action bar on Android?