更改android.support.v7.widget.SearchView的颜色

时间:2016-07-29 11:49:26

标签: android android-layout searchview

我正在使用搜索视图。

  <android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/imageViewMenu"
            >
  </android.support.v7.widget.SearchView>

是否可以更改文字颜色和图标颜色进行查看?

目前它显示黑色文字和图标。我想将其更改为白色文本和白色搜索图标。

6 个答案:

答案 0 :(得分:12)

这可能有用......

SearchView searchView = (SearchView) findViewById(R.id.search);
EditText searchEditText = (EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.white));
searchEditText.setHintTextColor(getResources().getColor(R.color.white));

答案 1 :(得分:7)

对于android.support.v7.widget.SearchView,您可以使用以下代码更改文本颜色,文本提示颜色和图标:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem searchMenuItem = menu.findItem(R.id.menu_action_search);
    SearchView searchViewAction = (SearchView) MenuItemCompat.getActionView(searchMenuItem);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());

    searchViewAction.setSearchableInfo(searchableInfo);
    searchViewAction.setIconifiedByDefault(true);

    EditText searchEditText = (EditText)searchViewAction.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchEditText.setTextColor(getResources().getColor(R.color.white));
    searchEditText.setHintTextColor(getResources().getColor(R.color.white));

    ImageView searchMagIcon = (ImageView)searchViewAction.findViewById(android.support.v7.appcompat.R.id.search_button);
    searchMagIcon.setImageResource(R.drawable.ic_search_white);


    return super.onPrepareOptionsMenu(menu);
}

答案 2 :(得分:5)

更改文字颜色:

int searchSrcTextId = getResources().getIdentifier("android:id/search_src_text", null, null);  
EditText searchEditText = (EditText) searchView.findViewById(searchSrcTextId);  
searchEditText.setTextColor(Color.WHITE);  
searchEditText.setHintTextColor(Color.LTGRAY); 

更改关闭按钮:

int closeButtonId = getResources().getIdentifier("android:id/search_close_btn", null, null);  
ImageView closeButtonImage = (ImageView) searchView.findViewById(closeButtonId);  
closeButtonImage.setImageResource(R.drawable.ic_action_cancel);  

//此摘录来自Android源代码(SearchView.java)

mSearchButton = findViewById(R.id.search_button);  
mSearchEditFrame = findViewById(R.id.search_edit_frame);  
mSearchPlate = findViewById(R.id.search_plate);  
mSubmitArea = findViewById(R.id.submit_area);  
mSubmitButton = findViewById(R.id.search_go_btn);  
mCloseButton = (ImageView) findViewById(R.id.search_close_btn);  
mVoiceButton = findViewById(R.id.search_voice_btn);  
mSearchHintIcon = (ImageView) findViewById(R.id.search_mag_icon); 

参考:link

答案 3 :(得分:5)

如果它位于工具栏中,请为工具栏设置主题。例如,这会使文本(和图标)变白

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

答案 4 :(得分:3)

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
    <item name="colorControlHighlight">@color/LightGray</item>
    <item name="android:textColorHint">@color/LightGray</item>
    <item name="android:editTextColor">@color/TransparentGray</item>
</style>

将此主题应用于您的工具栏提示(colorControlHighlight)菜单项的波纹效果颜色

答案 5 :(得分:1)

是的,你可以这样做,其中一种方法:

public static void customizeSearchView(SearchView searchView) {

        int searchTextViewId = searchView.getContext().getResources()
                .getIdentifier("android:id/search_src_text", null, null);
        AutoCompleteTextView searchTextView
                = (AutoCompleteTextView) searchView.findViewById(searchTextViewId);
        searchTextView.setTextSize(14);
        searchTextView.setTextColor(Color.WHITE);
}