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

144

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

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


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

2
TextView textView = (TextView) searchView.findViewById(R.id.search_src_text);
textView.setTextColor(Color.BLACK);

1

对于带有searchView的appcompat-v7工具栏(通过MenuItemCompat提供):

将工具栏主题设置为@style/ThemeOverlay.AppCompat.Light会产生黑色的提示文本和输入文本,但不会影响光标*颜色。相应地,将工具栏主题设置为@style/ThemeOverlay.AppCompat.Dark会产生白色的提示文本和输入文本,光标*仍然是白色。

定制上述主题:

android:textColorPrimary --> 输入文本的颜色

editTextColor --> 输入文本的颜色(如果设置了,将覆盖android:textColorPrimary的影响)

android:textColorHint --> 提示文本的颜色

*注意:我还没有确定如何控制光标颜色(不使用反射解决方案)。


光标颜色来自colorControlActivated。 - Henry

1

使用扩展改变文本/提示颜色的Kotlin方法

fun SearchView.changeColors(color: Int) {
    val editText = this.findViewById<EditText>(androidx.appcompat.R.id.search_src_text)
    editText.setTextColor(ContextCompat.getColor(context, color))
    editText.setHintTextColor(ContextCompat.getColor(context, color))
}

1
使用appcompat v7库可以自定义搜索视图。我使用了appcompat v7库,并为其定义了自定义样式。 在drawable文件夹中放置bottom_border.xml文件,其内容如下:
 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 <item>
  <shape >
      <solid android:color="@color/blue_color" />
  </shape>
 </item>
 <item android:bottom="0.8dp"
   android:left="0.8dp"
   android:right="0.8dp">
  <shape >
      <solid android:color="@color/background_color" />
  </shape>
 </item>

 <!-- draw another block to cut-off the left and right bars -->
 <item android:bottom="2.0dp">
  <shape >
      <solid android:color="@color/main_accent" />
  </shape>
  </item>
 </layer-list>

在values文件夹中的styles_myactionbartheme.xml文件中:
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
  <style name="AppnewTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowBackground">@color/background</item>
    <item name="android:actionBarStyle">@style/ActionBar</item>
    <item name="android:actionBarWidgetTheme">@style/ActionBarWidget</item>
  </style> 
  <!-- Actionbar Theme -->
  <style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/main_accent</item>
    <!-- <item name="android:icon">@drawable/abc_ic_ab_back_holo_light</item> -->
  </style> 
  <style name="ActionBarWidget" parent="Theme.AppCompat.Light">
    <!-- SearchView customization-->
     <!-- Changing the small search icon when the view is expanded -->
    <!-- <item name="searchViewSearchIcon">@drawable/ic_action_search</item> -->
     <!-- Changing the cross icon to erase typed text -->
   <!--   <item name="searchViewCloseIcon">@drawable/ic_action_remove</item> -->
     <!-- Styling the background of the text field, i.e. blue bracket -->
    <item name="searchViewTextField">@drawable/bottom_border</item>
     <!-- Styling the text view that displays the typed text query -->
    <item name="searchViewAutoCompleteTextView">@style/AutoCompleteTextView</item>        
  </style>

    <style name="AutoCompleteTextView" parent="Widget.AppCompat.Light.AutoCompleteTextView">
     <item name="android:textColor">@color/text_color</item>
   <!--   <item name="android:textCursorDrawable">@null</item> -->
    <!-- <item name="android:textColorHighlight">@color/search_view_selected_text</item> -->
  </style>
 </resources>

我定义了 custommenu.xml 文件来显示菜单:
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:com.example.actionbartheme="http://schemas.android.com/apk/res-auto" >  

  <item android:id="@+id/search"
      android:title="@string/search_title"
      android:icon="@drawable/search_buttonn"
      com.example.actionbartheme:showAsAction="ifRoom|collapseActionView"
        com.example.actionbartheme:actionViewClass="android.support.v7.widget.SearchView"/>        
  </menu>

你的活动应该扩展ActionBarActivity而不是Activity。这里是onCreateOptionsMenu方法。
  @Override
  public boolean onCreateOptionsMenu(Menu menu) 
  {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.custommenu, menu);
  }

在清单文件中:
  <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppnewTheme" >

更多信息请访问以下网址:
这里 http://www.jayway.com/2014/06/02/android-theming-the-actionbar/


1
一个 SearchView 对象继承自 LinearLayout,因此它包含其他视图。窍门在于找到持有提示文本的视图并以编程方式更改颜色。尝试通过 id 查找视图的问题在于 id 取决于应用程序中使用的主题。因此,根据使用的主题,findViewById(int id) 方法可能返回 null。一个更好的方法是遍历视图层次结构并找到包含提示文本的小部件:
// 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));
// set the text color
autoComplete.setTextColor(Color.BLUE);

使用这种方法,您还可以更改SearchView层次结构中的其他小部件的外观,例如保存搜索查询的EditText。除非Google决定不久更改SearchView的视图层次结构,否则您应该能够使用此方法一段时间来更改小部件的外观。

1
通过使用这个,我能够改变搜索视图中键入文本的颜色。
AutoCompleteTextView typed_text = (AutoCompleteTextView) inputSearch.findViewById(inputSearch.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
typed_text.setTextColor(Color.WHITE);

1

使用androidx在工具栏中更改搜索视图样式。

首先,在您的工具栏样式中设置搜索视图样式(使用您自己的应用程序基础主题更改父级主题):

     <!-- toolbar style -->
      <style name="ToolbarStyle" parent="Theme.AppCompat.Light.NoActionBar">     
          <!-- search view about -->
          <item name="android:editTextColor">@color/colorWhitePrimaryText</item>
          <item name="android:textColorHint">@color/colorWhiteSecondaryText</item>
          <item name="android:textCursorDrawable">@drawable/drawable_cursor_white</item>
          <item name="closeIcon">@mipmap/ic_close</item>
          <item name="searchHintIcon">@mipmap/ic_search_white</item>
          <item name="searchIcon">@mipmap/ic_search_white</item>
          <item name="android:longClickable">false</item>
          <item name="android:queryHint">@string/toolbar_search</item>
      </style> 

然后,在您的工具栏布局中使用它:

android:theme="@style/ToolbarStyle"

使用此功能,您可以更改大多数搜索视图的样式,除了查询提示。
最后,在工具栏菜单选项中设置查询提示。
toolbar.setOnMenuItemClickListener(item -> {
        switch (item.getItemId()){
            case R.id.toolbar_search:
                SearchView searchView = (SearchView) item.getActionView();
                searchView.setQueryHint("default query");
                searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        return false;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        return false;
                    }
                });
                return true;
                default:
                    return false;
        }

你是不是想在“searchView style”下发布一些内容? - DavidW
我已经完成并简化了我的解决方案,也许可以帮到你。 - user2333

1

哇,很多答案。它从您的主要颜色获取颜色值。

更改它,就完成了!

@Override
public void onResume() {
    super.onResume();
    getActivity().setTheme(R.style.YourTheme_Searching);
}

样式;

<style name="YourTheme.Searching" parent="YourTheme">
    <item name="android:textColorPrimary">@android:color/white</item>
</style>

1

使用 Kotlin 和 AndroidX,只需尝试以下操作:

val id: Int = binding.searchView.context
        .resources
        .getIdentifier("android:id/search_src_text", null, null)
val editText: EditText = binding.searchView.findViewById(id)
editText.setTextColor(Color.BLUE)
editText.setHintTextColor(Color.BLACK)

0

它在我这里可以工作

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem searchItem = menu.findItem(R.id.action_search);
    EditText searchEditText = (EditText) searchView.getActionView().findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchEditText.setTextColor(getResources().getColor(R.color.white));
    searchEditText.setHintTextColor(getResources().getColor(R.color.white));
    return super.onPrepareOptionsMenu(menu);
}

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