Changing action bar searchview hint text color

2019-01-30 18:32发布

How can I change the action bar search view hint text colour?

This question explains how to get the EditText when using ABS: Android ActionBar Customize Search View

Is there a android.R.id I could use to get a reference to the EditText so I could change the hint colour? Or is there some other way to change the colour?

Action bar search view hint text

18条回答
Explosion°爆炸
2楼-- · 2019-01-30 19:01

this worked for me, hope it helps

((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.white));

also see this

查看更多
甜甜的少女心
3楼-- · 2019-01-30 19:01

Try this code. It is very easy.

    // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
           searchEditText.setHint("Search");
        searchEditText.setHintTextColor(getResources().getColor(R.color.white));
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

searchEditText.setHintTextColor(getResources().getColor(R.color.white));

查看更多
淡お忘
4楼-- · 2019-01-30 19:01
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);
textView.setHintTextColor(Color.WHITE);
查看更多
来,给爷笑一个
5楼-- · 2019-01-30 19:02

Here's specifically how you apply the style as hinted at above. The right answer is to overload the action bar widget style with your own custom style. For holo light with dark action bar, put this in your own styles file such as res/values/styles_mytheme.xml:

<style name="Theme.MyTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarWidgetTheme">@style/Theme.MyTheme.Widget</item>
    <!-- your other custom styles -->
</style>

<style name="Theme.MyTheme.Widget" parent="@android:style/Theme.Holo">
    <item name="android:textColorHint">@android:color/white</item>
    <!-- your other custom widget styles -->
</style>

Make sure your application is using theme custom theme as described in enter link description here

查看更多
放我归山
6楼-- · 2019-01-30 19:03

A SearchView object extends from LinearLayout, so it holds other views. The trick is to find the view holding the hint text and change the colour programmatically. By traversing the view hierarchy, you can find the widget containing the hint text:

// get your SearchView with its id
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
// traverse the view to the widget containing the hint text
LinearLayout ll = (LinearLayout)searchView.getChildAt(0);
LinearLayout ll2 = (LinearLayout)ll.getChildAt(2);
LinearLayout ll3 = (LinearLayout)ll2.getChildAt(1);
SearchView.SearchAutoComplete autoComplete = (SearchView.SearchAutoComplete)ll3.getChildAt(0);
// set the hint text color
autoComplete.setHintTextColor(getResources().getColor(Color.WHITE));

This method works with any theme. Also, you can change the look of other widgets in the SearchView hierarchy, such as the EditText holding the search query.

查看更多
Juvenile、少年°
7楼-- · 2019-01-30 19:05

With the most recent appcompat library (or if you're targeting 5.0+), this is possible to do purely in XML using a ThemeOverlay:

<style name="ThemeOverlay.MyApp.ActionBar" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="autoCompleteTextViewStyle">@style/Widget.MyApp.AutoCompleteTextView.SearchView</item>
</style>

<style name="Widget.MyApp.AutoCompleteTextView.SearchView" parent="Widget.AppCompat.AutoCompleteTextView">
    <item name="android:textColor">#000</item>
    <item name="android:textColorHint">#5000</item>
</style>

And then in your layout file:

<android.support.v7.widget.Toolbar
    app:theme="@style/ThemeOverlay.MyApp.ActionBar"
    ... />

If you also want it to apply to activities that are using an ActionBar instead of a Toolbar, you can add this to the Activity's theme:

<style name="Theme.MyApp" parent="Theme.AppCompat">
    <item name="actionBarTheme">@style/ThemeOverlay.MyApp.ActionBar</item>
    ...
</style>
查看更多
登录 后发表回答