更改操作栏搜索视图提示文本颜色

58

如何更改操作栏搜索视图提示文本的颜色?

这个问题解释了如何在使用ABS时获取EditText:

Android ActionBar Customize Search View

是否有一个android.R.id,我可以使用它来获取对EditText的引用,以便更改提示文字颜色?还是有其他方法可以更改颜色?

操作栏搜索视图提示文本

18个回答

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

2
你能加上一点解释吗? - Steve Westbrook
13
如何不理解呢?通过HTML标签更改文本和提示文本的颜色... - Vadim Teselkin
2
我们可以使用html.fromhtml更改字体大小吗? - Chirag Khatsuriya
如果您只需要不同的大小,您可以使用简单的<small>或<big>。 - Vadim Teselkin

66

android:actionBarWidgetTheme 属性应指向一个具有 android:textColorHint 的样式,该属性允许更改搜索视图的提示文本颜色。


android:actionBarWidgetTheme 仅在 API 等级 14 及以上版本可用。 - Roger Alien
2
解决方案不起作用。https://dev59.com/gmgu5IYBdhLWcg3wbWo9#14364222 可以,但这只是一个权宜之计。 - Maksim Dmitriev
4
我会翻译以下内容:我在这里添加一条评论,android:actionBarWidgetTheme 样式的父级也是一个主题。你可以使用 @android:style/Theme.Holo 来获取浅色搜索图标,使用 @android:style/Theme.Holo.Light 来获取深色搜索图标。参考这里 - Glenn.nz
我正在使用material3搜索栏。我通过设置android:textAppearance="@style/CustomSearchBarStyle"使其正常工作。希望这能帮助到需要解决方案的人。 - undefined

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

2
很高兴知道如何查找search_src_text。谢谢。 - giltsl
1
这个解决方案适用于实际搜索文本,但不影响提示文本颜色。 - Graydyn Young
它在它的时间里与我一起工作,现在还不知道 :) 对此很抱歉@GraydynYoung - Amr Angry

18

这对我有用,希望能帮到你

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

也可以看看这个


1
我发现 android.support.v7.appcompat 不是必需的,只需要使用 searchView.findViewById(R.id.search_src_text) 就可以了。 - user2331095

12

我通过将此属性添加到主题中解决了它:

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

然后在这种情况下添加我想要更改的属性,即提示颜色。

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

希望这能帮到你 :D


"android:actionBarWidgetTheme需要API级别14" - aleb
是的!有主题化! - Radu
1
这个不起作用 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> - Red M

9

以下是具体如何应用上面提到的样式。正确的方法是使用自己的自定义样式来重载操作栏小部件样式。对于带有深色操作栏的Holo Light,请将以下内容放入您自己的样式文件中,例如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>

请确保您的应用程序使用如下所述的自定义主题:应用主题


7
如果您想更改搜索视图中的左箭头按钮,请添加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"
        />

如果您想要改变文本颜色和提示文本颜色,请添加以下内容。
EditText searchEditText = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchEditText.setTextColor(Color.WHITE);
    searchEditText.setHintTextColor(Color.WHITE);

如果您想更改关闭图标,请添加以下内容。
ImageView imvClose = searchView.findViewById(android.support.v7.appcompat.R.id
            .search_close_btn);
    imvClose.setImageResource(R.drawable.ic_search_view_close);

7
一个SearchView对象继承自LinearLayout,因此它持有其他视图。关键是找到包含提示文本的视图并通过编程方式更改颜色。通过遍历视图层次结构,可以找到包含提示文本的小部件:
// 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));

这种方法适用于任何主题。此外,您可以更改SearchView层次结构中的其他小部件的外观,例如保持搜索查询的 EditText


6
只需获取搜索小部件的textView ID,并使用setTextColor()方法更改搜索文本颜色,setHintTextColor()则是更改搜索提示文本颜色的方法。
// 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);

最棒的解决方案。 - Mazharul Islam

6
这对我有用:-)
 EditText seartext = searchView.findViewById(R.id.search_src_text);
    seartext.setTextColor(Color.WHITE);
    seartext.setHintTextColor(Color.GRAY);

尽量不要在不必要的情况下使用大写字母。这样做会将您的评论发送到审核队列,让像我这样的人(以及未来的您)审查一个没有任何问题的帖子(只有大写字母...)。 - Having a life on a beach

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