是否可以更改 Android SearchView 上的文本颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11321129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:57:05  来源:igfitidea点击:

Is it possible to change the textcolor on an Android SearchView?

androidsearchview

提问by CACuzcatlan

The SearchView element doesn't have any properties for changing the text color. The default text color is black and doesn't work on our dark background. Is there a way to change the color of the text without resorting to hacks?

SearchView 元素没有任何用于更改文本颜色的属性。默认文本颜色为黑色,不适用于我们的深色背景。有没有办法在不求助于黑客的情况下改变文本的颜色?

I found this similar question related to changing the text size, but so far, it doesn't have any answers: How to set SearchView TextSize?

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

回答by akdotcom

Add

添加

<item name="android:editTextColor">@android:color/white</item>

to the parent theme and that should change the entered text. You can also use

到父主题,这应该会更改输入的文本。你也可以使用

<item name="android:textColorHint">@android:color/white</item>

to change the hint text for the SearchView. (Note that you can replace the @android:color/whitewith whatever appropriate value you're hoping to use)

更改 SearchView 的提示文本。(请注意,您可以将 替换为@android:color/white您希望使用的任何适当值)

回答by lokoko

Try something like this : You would get a handle to the textview from the sdk and then change it since they don't expose it publicly.

尝试这样的事情:您将从 sdk 获得 textview 的句柄,然后更改它,因为它们不会公开公开。

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

回答by user2331095

This works for me.

这对我有用。

SearchView searchView = (SearchView) findViewById(R.id.search);
EditText 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));

回答by Ferran Maylinch

I wanted to do something similar. I finally had to find the TextViewamong the SearchViewchildren:

我想做类似的事情。我终于不得不TextViewSearchView孩子们中找到:

for (TextView textView : findChildrenByClass(searchView, TextView.class)) {
    textView.setTextColor(Color.WHITE);
}

If you want the util method:

如果你想要 util 方法:

public static <V extends View> Collection<V> findChildrenByClass(ViewGroup viewGroup, Class<V> clazz) {

    return gatherChildrenByClass(viewGroup, clazz, new ArrayList<V>());
}

private static <V extends View> Collection<V> gatherChildrenByClass(ViewGroup viewGroup, Class<V> clazz, Collection<V> childrenFound) {

    for (int i = 0; i < viewGroup.getChildCount(); i++)
    {
        final View child = viewGroup.getChildAt(i);
        if (clazz.isAssignableFrom(child.getClass())) {
            childrenFound.add((V)child);
        }
        if (child instanceof ViewGroup) {
            gatherChildrenByClass((ViewGroup) child, clazz, childrenFound);
        }
    }

    return childrenFound;
}

回答by CoolMind

Thanks to @CzarMatt I added AndroidXsupport.

感谢 @CzarMatt 我添加了AndroidX支持。

For me the following works. I used a code from a link: Change text color of search hint in actionbar using support library.

对我来说以下作品。我使用了链接中的代码:使用支持库更改操作栏中搜索提示的文本颜色

    searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    EditText txtSearch = ((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text));
    txtSearch.setHint(getResources().getString(R.string.search_hint));
    txtSearch.setHintTextColor(Color.LTGRAY);
    txtSearch.setTextColor(Color.WHITE);

Changing action bar searchview hint text coloradvices another solution. It works but sets only hint text and color.

更改操作栏搜索视图提示文本颜色建议另一种解决方案。它有效但仅设置提示文本和颜色。

    searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getResources().getString(R.string.search_hint) + "</font>"));

回答by johnarleyburns

This is best achieved through custom styles. 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:

这最好通过自定义样式来实现。使用您自己的自定义样式重载操作栏小部件样式。对于带有深色操作栏的全息灯,请将其放在您自己的样式文件中,例如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

确保您的应用程序使用主题自定义主题,如在此处输入链接描述中所述

回答by oldergod

You can do so by setting the editTextColorattribute in the style.

您可以通过editTextColor在样式中设置属性来实现。

<style name="SearchViewStyle" parent="Some.Relevant.Parent">
    <item name="android:editTextColor">@color/some_color</item>
</style>

and you apply this style to Toolbaror the SearchViewin the layout.

并将此样式应用于布局中的ToolbarSearchView

<android.support.v7.widget.Toolbar
    android:theme="@style/SearchViewStyle">

    <android.support.v7.widget.SearchView />

</android.support.v7.widget.Toolbar>

回答by zankoav

if you use - android.support.v7.widget.SearchView

如果你使用 - android.support.v7.widget.SearchView

SearchView searchView = (SearchView) item.getActionView();
EditText editText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
editText.setTextColor(Color.WHITE);

in Kotlin

在科特林

val searchView = SearchView(this)
val editText = searchView.findViewById<EditText>(androidx.appcompat.R.id.search_src_text)
editText.setTextColor(Color.WHITE)

You should rather start using androidx support libraries now

您现在应该开始使用 androidx 支持库

回答by Arbitur

Create a Style for your Toolbar

为您打造风格 Toolbar

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="android:editTextColor">@color/some_color</item>
</style>

And set it as the theme for the Toolbar

并将其设置为主题 Toolbar

<android.support.v7.widget.Toolbar
    android:theme="@style/AppTheme.Toolbar"
    ...

回答by LukeWaggoner

If you're using the android.support.v7.widget.SearchView, it's possible without having to use reflection.

如果您使用的是 android.support.v7.widget.SearchView,则无需使用反射即可。

Here's how I'm doing it in my app:

这是我在我的应用程序中的做法:

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));
}

They don't expose the ids publicly for the non appcompat SearchView, but they do for AppCompat if you know where to look. :)

他们不会公开为非 appcompat SearchView 公开 ID,但如果您知道在哪里查看,他们会为 AppCompat 公开。:)