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条回答
Lonely孤独者°
2楼-- · 2019-01-30 18:44

By using this you can change the Searchable xml

  1. Search text hint color
  2. Search Text
  3. Search close icon
  4. Search hint icon
  5. Search plate
int searchHintBtnId = searchView.getContext().getResources()
                    .getIdentifier("android:id/search_mag_icon", null, null);
     int hintTextId = searchView.getContext()
                    .getResources()
                    .getIdentifier("android:id/search_src_text", null, null);
            int closeBtnId = searchView.getContext()
                    .getResources()
                    .getIdentifier("android:id/search_close_btn", null, null);
            // Set the search plate color to white
            int searchPlateId = getResources().getIdentifier("android:id/search_plate", null, null);

            View view = searchView.findViewById(searchPlateId);
            view.setBackgroundResource(R.drawable.search_plate);

            ImageView closeIcon = (ImageView) searchView.findViewById(closeBtnId);
            closeIcon.setImageResource(R.drawable.close);

            ImageView searchHintIcon = (ImageView) searchView.findViewById(searchHintBtnId);
            searchHintIcon.setImageResource(R.drawable.search);

            TextView textView = (TextView) searchView.findViewById(hintTextId);
            textView.setTextColor(getResources().getColor(R.color.search_text_color));
            textView.setHintTextColor(getResources().getColor(R.color.search_hint_text_color));

Here is drawable/search_plate.xml

<!-- main color -->
<item android:bottom="1dp"
    android:left="1dp"
    android:right="1dp">
    <shape >
        <solid android:color="@color/app_theme_color" />
    </shape>
</item>

<!-- draw another block to cut-off the left and right bars -->
<item android:bottom="10.0dp">
    <shape >
        <solid android:color="@color/app_theme_color" />
    </shape>
</item> </layer-list>
查看更多
时光不老,我们不散
3楼-- · 2019-01-30 18:46
searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getResources().getString(R.string.hintSearchMess) + "</font>"));
查看更多
不美不萌又怎样
4楼-- · 2019-01-30 18:49

If you want to change left arrow button in search view, add app:collapseIcon.

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:collapseIcon="@drawable/ic_search_view_home"
        />

If you want to change text color and text hint color, add the following.

EditText searchEditText = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchEditText.setTextColor(Color.WHITE);
    searchEditText.setHintTextColor(Color.WHITE);

If you want to change close icon, add the following.

ImageView imvClose = searchView.findViewById(android.support.v7.appcompat.R.id
            .search_close_btn);
    imvClose.setImageResource(R.drawable.ic_search_view_close);
查看更多
Root(大扎)
5楼-- · 2019-01-30 18:50

The solution to change color of Search View Hint Text is :

 SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
 searchAutoComplete.setHintTextColor(Color.GRAY);
查看更多
一纸荒年 Trace。
6楼-- · 2019-01-30 18:55

android:actionBarWidgetTheme of your main theme should point to a style that has a android:textColorHint in it. That one will allow changing the hint text color of the search view.

查看更多
Deceive 欺骗
7楼-- · 2019-01-30 18:55

Put this in your XML file:

android:textColorHint="@android:color/white"
查看更多
登录 后发表回答