在Android的SearchView中,是否可以更改文本颜色?

144

SearchView元素没有更改文本颜色的任何属性。默认文本颜色是黑色,不适用于我们的深色背景。有没有一种方法可以更改文本颜色,而不必诉诸hack?

我发现了一个类似的问题,与更改文本大小有关,但到目前为止,它还没有任何答案:如何设置SearchView TextSize?


请提及您尝试过的内容。 - Sahil Mahajan Mj
是的,这是可能的。看看这个链接:https://dev59.com/hGMl5IYBdhLWcg3woYEu#66246372 - KennyAli
38个回答

6
如果你正在使用android.support.v7.widget.SearchView,那么可以在不使用反射的情况下实现。以下是我在我的应用程序中进行操作的方法:
EditText text = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
ImageView searchCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);

if (searchPlate != null) {
    searchPlate.setBackgroundResource(R.drawable.search_background);
}

if (text != null){
    text.setTextColor(resources.getColor(R.color.white));
    text.setHintTextColor(getResources().getColor(R.color.white));

    SpannableStringBuilder magHint = new SpannableStringBuilder("  ");
    magHint.append(resources.getString(R.string.search));

    Drawable searchIcon = getResources().getDrawable(R.drawable.ic_action_view_search);
    int textSize = (int) (text.getTextSize() * 1.5);
    searchIcon.setBounds(0, 0, textSize, textSize);
    magHint.setSpan(new ImageSpan(searchIcon), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Set the new hint text
    text.setHint(magHint);

}

if (searchCloseIcon != null){
    searchCloseIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_close));
}

对于非 AppCompat 的 SearchView,它们不会公开其 id,但如果您知道在哪里查找,它们会公开 id。 :)


这在各种设备上实际上工作得非常不错。棒极了。 - Shark

5
我遇到过这个问题,以下方法对我有效。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.customer_menu, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView       = (SearchView) menu.findItem(R.id.menu_customer_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        searchView.setOnQueryTextListener(this);

        //Applies white color on searchview text
        int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) searchView.findViewById(id);
        textView.setTextColor(Color.WHITE);

        return true;
}

4
我总是在以下代码中得到null值:(TextView) searchView.findViewById(id); - danielrvt-sgb

4
如果你的应用主题基于Holo主题,那么在SearchView中会得到白色而不是黑色文本。 <style name="Theme.MyTheme" parent="android:Theme.Holo"> 我没有找到其他改变SearchView文本颜色的方法,除非使用一些不太规范的方法。

1
这个没有起作用。我本以为会的,因为它比手动搜索一个“神奇”的视图ID要简洁很多。我尝试了几个主题,其中一个活动的布局有一个黑色背景,但它们都只产生了SearchView中的黑色文本,没有其他任何效果。 - E-Riz

4
最清晰的方式是:
工具栏使用主题ThemeOverlay.AppCompat.Dark.Actionbar。
现在像这样制作它的子项:
toolbarStyle父项"ThemeOverlay.AppCompat.Dark.Actionbar"
在该样式中添加此项
项目名称"android:editTextColor">yourcolor 完成。
还有一件非常重要的事情,在工具栏中放置layout_height ="?attr/actionbarSize"。默认情况下,它是wrap_content。对我来说,在搜索视图中文本甚至看不见,它解决了这个问题。

4
是的,可以使用以下方法实现。
public static EditText setHintEditText(EditText argEditText, String argHintMessage, boolean argIsRequire) {
    try {
        if (argIsRequire) {
            argHintMessage = "   " + argHintMessage;
            //String text = "<font color=#8c8c8c>"+argHintMessage+"</font> <font color=#cc0029>*</font>";
            String text = "<font color=#8c8c8c>" + argHintMessage + "</font>";
            argEditText.setHint(Html.fromHtml(text));
        } else {
            argEditText.setHint(argHintMessage);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return argEditText;
}

这个方法的调用看起来像这样...
metLoginUserName=(EditText)this.findViewById(R.id.etLoginUserName);
    metLoginPassword=(EditText)this.findViewById(R.id.etLoginPassword);

    /**Set the hint in username and password edittext*/
    metLoginUserName=HotSpotStaticMethod.setHintEditText(metLoginUserName, getString(R.string.hint_username),true);
    metLoginPassword=HotSpotStaticMethod.setHintEditText(metLoginPassword, getString(R.string.hint_password),true);

使用这种方法,我成功地在提示中添加了红色的*标记。根据您的需要更改此方法。希望对您有所帮助....:)


这对我有点复杂。 - gaols
1
字符串 text = "<font color=#8c8c8c>"+HintMessage+"</font>"; argEditText.setHint(Html.fromHtml(text)); 您也可以只使用这两行代码来设置颜色提示。 - DynamicMind
1
SearchAutoComplete autoCompleteView = (SearchAutoComplete) mSearchView.findViewById(com.android.internal.R.id.search_src_text); autoCompleteView.setHintTextColor(R.color.hint_text_color); 这是我的解决方案,但我认为你的比我的更好,我会使用你的解决方案,谢谢。 - gaols
@ gaols 你有解决方案吗? - DynamicMind
@gaols,请问你的“com.android.internal.R.id.search_src_text”在哪里? - wangqi060934
@gaols的答案适用于appcompat-v7。请查看abc_search_view.xml,其中包含一个android.support.v7.widget.SearchView$SearchAutoComplete类的视图。 - straya

4
对我来说这是可行的,如果您使用 androidx.appcompat.widget.SearchView , 请使用androidx.appcompat.R.id.search_src_text 来确定搜索视图,这样textView中就不会返回null。
    TextView textView = (TextView) searchView.findViewById(androidx.appcompat.R.id.search_src_text);
    textView.setTextColor(Color.RED);
    textView.setHintTextColorstrong text(Color.RED);

3

Yes we can,

SearchView searchView = (SearchView) findViewById(R.id.sv_symbol);

为了将搜索视图文本应用为白色,请执行以下操作:
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);

愉快的编码!!!


3

这对我来说是有效的。

final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);

searchView.setOnQueryTextListener(this);   
searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.white));
searchEditText.setHintTextColor(getResources().getColor(R.color.white));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) 
{
    searchEditText.setBackgroundColor(getResources().getColor(R.color.c_trasnparent));
    searchEditText.setGravity(Gravity.CENTER);
    searchEditText.setCompoundDrawables(null,null,R.drawable.ic_cross,null);
}

3
使用这个,它是正确的。 :D
AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchText.setHintTextColor(getResources().getColor(color.black));
searchText.setTextColor(getResources().getColor(color.black));

3

更改输入文本的颜色:

((EditText)((SearchView)findViewById(R.id.searchView)).findViewById(((SearchView)findViewById(R.id.searchView)).getContext().getResources().getIdentifier("android:id/search_src_text", null, null))).setTextColor(Color.WHITE);

更改提示文本的颜色:

((EditText)((SearchView)findViewById(R.id.searchView)).findViewById(((SearchView)findViewById(R.id.searchView)).getContext().getResources().getIdentifier("android:id/search_src_text", null, null))).setHintTextColor(Color.LTGRAY);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接