Change color of android.support.v7.widget.SearchVi

2019-06-20 17:32发布

I am using search view.

  <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>

Is this possible to change text color and icon color to view?

Currently it shows black text and icon. I want to change it to white color text and white search icon.

6条回答
▲ chillily
2楼-- · 2019-06-20 17:46
<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>

apply this theme to your toolbar hint that (colorControlHighlight) color of ripple effct of your menu items

查看更多
神经病院院长
3楼-- · 2019-06-20 17:47

For the android.support.v7.widget.SearchView you can change the text color, text hint color and the icon with this code :

@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);
}
查看更多
做个烂人
4楼-- · 2019-06-20 17:47

To Change the Text color :

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); 

To Change the close button :

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

// This excerpt is from Android source code (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); 

Reference : link

查看更多
Deceive 欺骗
5楼-- · 2019-06-20 17:51

If it resides in toolbar, set theme for toolbar. For example this makes the text(and icon) white

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
查看更多
混吃等死
6楼-- · 2019-06-20 17:53

Yes, you can do it, one of ways to do it:

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);
}
查看更多
仙女界的扛把子
7楼-- · 2019-06-20 18:05

This may work..

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));
查看更多
登录 后发表回答