Change text color of search hint in actionbar usin

2019-01-25 22:58发布

I want to change the text color of search view hint, but I tried in many ways with no success. Like in this post: Changing action bar searchview hint text color

I'm using Appcompat, support library of actionbar.

I tried this with null pointer return:

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

3条回答
Lonely孤独者°
2楼-- · 2019-01-25 23:36

I found the answer here too:

getMenuInflater().inflate(R.menu.menu_home, menu);
MenuItem searchItem = menu.findItem(R.id.item_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setOnQueryTextListener(this);
mSearchView.setQueryHint(getString(R.string.text));
SearchAutoComplete searchAutoComplete = (SearchAutoComplete) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(mRes.getColor(android.R.color.white));
searchAutoComplete.setTextSize(14);
查看更多
时光不老,我们不散
3楼-- · 2019-01-25 23:44

Wrong id for the AutoCompleteTextView, I guess. Unless you use a custom Action Bar Search widget. Try smth. like the following:

((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
            .setHintTextColor(getResources().getColor(R.color.white));
查看更多
Evening l夕情丶
4楼-- · 2019-01-26 00:02

Using the following code:

getResources().getColor(R.color.white)

it is possible that an alert saying that getColor(int) is deprecated appears. This is because from api 23, a new getColor() method was added to the ContextCompat.

So just use the following:

((EditText) searchView.findViewById (android.support.v7.appcompat.R.id.search_src_text))
                     .setHintTextColor (ContextCompat.getColor (getBaseContext(), Color.WHITE));

It worked for me.

查看更多
登录 后发表回答