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条回答
Summer. ? 凉城
2楼-- · 2019-01-30 18:56

Also you can set the SearchView on your menuItem manually and thereby have full control over the view.

查看更多
来,给爷笑一个
3楼-- · 2019-01-30 18:57
SearchView searchView= (SearchView) findViewById(R.id.searchView1);
int id = searchView.getContext()
                   .getResources()
                   .getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);
查看更多
一夜七次
4楼-- · 2019-01-30 18:57

Just get the textView id of search widget and use setTextColor() method to change the search text color and setHintTextColor() to change the search hints text color.

// Get textview id of search widget
int id =  searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);

// Set search text color
textView.setTextColor(Color.WHITE);

// Set search hints color
textView.setHintTextColor(Color.GRAY);
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-30 18:57

Look to this class, it can help you to solve your problem

import android.R.color;
import android.content.Context;
import android.graphics.Typeface;
import android.widget.ImageView;

import com.actionbarsherlock.widget.SearchView;
import com.actionbarsherlock.widget.SearchView.SearchAutoComplete;

public class MySearchView {

    public static SearchView getSearchView(Context context, String strHint) {
        SearchView searchView = new SearchView(context);
        searchView.setQueryHint(strHint);
        searchView.setFocusable(true);
        searchView.setFocusableInTouchMode(true);
        searchView.requestFocus();
        searchView.requestFocusFromTouch();

        ImageView searchBtn = (ImageView) searchView.findViewById(R.id.abs__search_button);
        searchBtn.setImageResource(R.drawable.ic_menu_search);

        // ImageView searchBtnClose = (ImageView) searchView.findViewById(R.id.abs__search_close_btn);
        // searchBtnClose.setImageResource(R.drawable.ic_cancel_search_bar);


        SearchAutoComplete searchText = (SearchAutoComplete) searchView.findViewById(R.id.abs__search_src_text);
        setFont(context, searchText);
        searchText.setTextColor(context.getResources().getColor(color.white));
        searchText.setHintTextColor(context.getResources().getColor(color.white));

        return searchView;
    }

    private static void setFont(Context _context, SearchAutoComplete searchText) {
        Typeface tf = null;
        Typeface currentType = searchText.getTypeface();
        if (currentType == null) {
            tf = MyApplication.fontNormal;
        }
        else {
            int currentStyle = currentType.getStyle();
            if (currentStyle == Typeface.BOLD) {
                tf = MyApplication.fontBold;
            }
            else if (currentStyle == Typeface.BOLD_ITALIC) {
                tf = MyApplication.fontBoldItalic;
            }
            else if (currentStyle == Typeface.ITALIC) {
                tf = MyApplication.fontItalic;
            }
            else {
                tf = MyApplication.fontNormal;
            }
        }
        searchText.setTypeface(tf);
    }

    public static SearchView getSearchView(Context context, int strHintRes) {
        return getSearchView(context, context.getString(strHintRes));
    }
}
查看更多
趁早两清
6楼-- · 2019-01-30 18:57

This is really simple to achieve using the styles.xml Just understand that themeing the SearchView is directly related to the theme of the Toolbar from whose menu.xml it is fetched as a app:actionViewClass.

  1. Create a style for Toolbar.
  2. Apply that style as theme on the Toolbar and it changes al the related views and their properties.
查看更多
聊天终结者
7楼-- · 2019-01-30 19:01

I resolved it adding this property to the main theme:

<style name="AppTheme" parent="AppBaseTheme">
    .....
    .....
    <item name="android:actionBarWidgetTheme">@style/MyActionBarWidgetTheme</item> 
</style>

And then add the property i want to change in this case the hint color.

<style name="MyActionBarWidgetTheme" parent="@android:style/Theme.Holo">
    .....
    .....
    <item name="android:textColorHint">@color/blanco_60</item>
</style>

Hope it helps :D

查看更多
登录 后发表回答