Text color of SearchView in ActionBar with ActionB

2020-03-24 13:41发布

I want to change the color of a text in an ActionBarSherlock SearchView, so I found this on StackOverflow:

AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchText.setHintTextColor(getResources().getColor(R.color.white));
searchText.setTextColor(getResources().getColor(R.color.white));    

But the problem is that searchText is null always. Also I tried with search_src_text (no abs__) - again, searchText is null.

I use Sherlock for my ActionBar (having custom view for the actionbar) but I also want the text color of the search view to be changed.

Anything I don't get?

2条回答
我欲成王,谁敢阻挡
2楼-- · 2020-03-24 14:01

other way, work for me

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        searchView = new SearchView(getSupportActionBar().getThemedContext());

        AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
        searchText.setTextColor(Color.BLACK); 
.....................
}
查看更多
萌系小妹纸
3楼-- · 2020-03-24 14:15

Change and apply your styles as follows:

  1. Overwrite SearchViewStyle that defines relevant style.

    <style name="SearchViewStyle" 
           parent="Widget.Sherlock.Light.SearchAutoCompleteTextView">
        <item name="android:textColor">myTextColor</item>
    </style>
    
  2. Add this style definition to your app theme.

    <style name="CustomTheme" parent="Theme.Sherlock.Light">
        ...
        <item name="searchAutoCompleteTextView">@style/SearchViewStyle</item>
        ...
    </style>
    
  3. Apply this theme in your Manifest (android:theme="CustomTheme").

Hope this helps ... Cheers!

p.s. search for SearchViewStyle in ABS style definitions (XML files). This way you can follow up available styles using Ctrl+click on definitions.

查看更多
登录 后发表回答