更改操作栏searchview提示文本颜色

Juh*_*ani 48 search android android-actionbar

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

这个问题解释了如何在使用ABS时获取EditText: Android ActionBar自定义搜索视图

有没有android.R.id我可以用来获取EditText的引用,所以我可以改变提示颜色?或者还有其他方法可以改变颜色吗?

操作栏搜索视图提示文本

Vad*_*kin 55

searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getResources().getString(R.string.hintSearchMess) + "</font>"));
Run Code Online (Sandbox Code Playgroud)

  • 有什么不明白的?通过html标签更改文本和提示文本的颜色... (10认同)
  • 你能加一点解释吗? (2认同)
  • 我们可以使用html.fromhtml更改字体大小吗? (2认同)

小智 54

你主要主题的android:actionBarWidgetTheme应该指向一个带有android:textColorHint的样式.那个将允许更改搜索视图的提示文本颜色.

  • 我只是在这里添加评论,`android:actionBarWidgetTheme`风格的父母也是一个主题.您可以使用`@android:style/Theme.Holo`作为较轻的搜索图标,使用`@android:style/Theme.Holo.Light`作为较暗的搜索图标.来自[here](http://daniel-codes.blogspot.co.nz/2012/11/the-unknown-style-actionbarwidgettheme.html) (4认同)
  • 该解决方案不起作用。http://stackoverflow.com/a/14364222 确实如此,但这确实是一种解决方法。 (2认同)

Amr*_*gry 22

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);
Run Code Online (Sandbox Code Playgroud)

  • 很高兴知道如何找到search_src_text.谢谢. (2认同)

Sye*_*hdi 18

这对我有用,希望它有所帮助

((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.white));
Run Code Online (Sandbox Code Playgroud)

也看到这个


Kok*_*sho 12

我决定将此属性添加到主题:

<style name="AppTheme" parent="AppBaseTheme">
    .....
    .....
    <item name="android:actionBarWidgetTheme">@style/MyActionBarWidgetTheme</item> 
</style>
Run Code Online (Sandbox Code Playgroud)

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

<style name="MyActionBarWidgetTheme" parent="@android:style/Theme.Holo">
    .....
    .....
    <item name="android:textColorHint">@color/blanco_60</item>
</style>
Run Code Online (Sandbox Code Playgroud)

希望它有所帮助:D


jfc*_*ier 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));
Run Code Online (Sandbox Code Playgroud)

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


小智 7

这里特别是如何应用上面提到的风格.正确的答案是使用您自己的自定义样式重置操作栏窗口小部件样式.对于带有暗动作条的全息灯,请将其放在您自己的样式文件中,例如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>
Run Code Online (Sandbox Code Playgroud)

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


Maz*_*Ali 6

这对我有用:-)

 EditText seartext = searchView.findViewById(R.id.search_src_text);
    seartext.setTextColor(Color.WHITE);
    seartext.setHintTextColor(Color.GRAY);
Run Code Online (Sandbox Code Playgroud)


Fer*_*med 5

只需获取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);
Run Code Online (Sandbox Code Playgroud)


Jar*_*ard 5

使用最新的 appcompat 库(或者如果您的目标是 5.0+),可以使用以下命令完全在 XML 中完成ThemeOverlay

<style name="ThemeOverlay.MyApp.ActionBar" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="autoCompleteTextViewStyle">@style/Widget.MyApp.AutoCompleteTextView.SearchView</item>
</style>

<style name="Widget.MyApp.AutoCompleteTextView.SearchView" parent="Widget.AppCompat.AutoCompleteTextView">
    <item name="android:textColor">#000</item>
    <item name="android:textColorHint">#5000</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后在您的布局文件中:

<android.support.v7.widget.Toolbar
    app:theme="@style/ThemeOverlay.MyApp.ActionBar"
    ... />
Run Code Online (Sandbox Code Playgroud)

如果您还希望它应用于使用 aActionBar而不是 a 的活动Toolbar,您可以将其添加到Activity的主题中:

<style name="Theme.MyApp" parent="Theme.AppCompat">
    <item name="actionBarTheme">@style/ThemeOverlay.MyApp.ActionBar</item>
    ...
</style>
Run Code Online (Sandbox Code Playgroud)


小智 5

如果要在搜索视图中更改左箭头按钮,请添加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"
        />
Run Code Online (Sandbox Code Playgroud)

如果要更改文本颜色和文本提示颜色,请添加以下内容。

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

如果要更改关闭图标,请添加以下内容。

ImageView imvClose = searchView.findViewById(android.support.v7.appcompat.R.id
            .search_close_btn);
    imvClose.setImageResource(R.drawable.ic_search_view_close);
Run Code Online (Sandbox Code Playgroud)