【问题标题】:Is it possible to change the textcolor on an Android SearchView?是否可以更改 Android SearchView 上的文本颜色?
【发布时间】:2012-07-04 11:42:46
【问题描述】:

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

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

【问题讨论】:

标签: android android-toolbar searchview


【解决方案1】:

我尝试并为此找到了一种解决方案。我想它会帮助你..

searchView.setBackgroundColor(Color.WHITE);

【讨论】:

  • 问题是文字颜色不是背景颜色
【解决方案2】:

如果您的应用主题基于 holo 主题,您将在 SearchView 中获得白色而不是黑色文本

<style name="Theme.MyTheme" parent="android:Theme.Holo">

我没有找到任何其他方法来更改搜索视图的文本颜色而不使用肮脏的黑客。

【讨论】:

  • 这不起作用。我希望它会这样,因为它比手动搜索“魔术”视图 ID 要干净得多。我尝试了几个主题,其中一个 Activity 的布局具有深色背景,但它们都没有在 SearchView 中产生除黑色文本之外的任何内容。
【解决方案3】:

是的,可以使用以下方法。

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

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

【讨论】:

  • 这对我来说有点复杂。
  • 字符串文本 = ""+HintMessage+""; argEditText.setHint(Html.fromHtml(text));您也可以只使用这两行代码来设置颜色提示
  • SearchAutoComplete autoCompleteView = (SearchAutoComplete) mSearchView .findViewById(com.android.internal.R.id.search_src_text);autoCompleteView.setHintTextColor(R.color.hint_text_color); 这是我的解决方案,但我认为你的解决方案比我的好,我会使用你的解决方案,谢谢。
  • @gaols 你有解决方案吗..?
  • @gaols,你的“com.android.internal.R.id.search_src_text”在哪里?
【解决方案4】:

试试这样的: 您可以从 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);

【讨论】:

  • 这总是返回 null
  • 请注意,如果您使用的是 ActionBarSherlock,则视图 ID 会有所不同。例如,该lib当前将编辑文本ID设置为“package:id/abs__search_src_text”,可以通过R.id.abs__search_src_text直接访问。
  • 另外,我真正想要更改的是 SearchView 提示文本颜色,这可以通过 textView.setHintTextColor() 轻松完成。
  • 这是一个非常肮脏的 hack。像 akdotcom 建议的那样使用 android:editTextColor 和 android:textColorHint
  • 对于 AndroidX,您必须使用 searchView.findViewById&lt;TextView&gt;(androidx.appcompat.R.id.search_src_text) 来查找 TextView
【解决方案5】:

用这个就对了。 :D

AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchText.setHintTextColor(getResources().getColor(color.black));
searchText.setTextColor(getResources().getColor(color.black));

【讨论】:

    【解决方案6】:

    我遇到了这个问题,这对我有用。

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

    【讨论】:

    • 我总是在:(TextView) searchView.findViewById(id);
    【解决方案7】:

    改变输入文本的颜色:

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

    【讨论】:

      【解决方案8】:

      你可以像这个类一样实现改变字体颜色和图像::

      import com.actionbarsherlock.widget.SearchView;
      import com.actionbarsherlock.widget.SearchView.SearchAutoComplete;
      
      
      public class MySearchView {
          public static SearchView getSearchView(Context context, String strHint) {
              SearchView searchView = new SearchView(context);
              searchView.setQueryHint(strHint);
              searchView.setFocusable(true);
              searchView.setFocusableInTouchMode(true);
              searchView.requestFocus();
              searchView.requestFocusFromTouch();
      
               ImageView searchBtn = (ImageView) searchView.findViewById(R.id.abs__search_button);
               searchBtn.setImageResource(R.drawable.ic_menu_search);
      
      
               ImageView searchBtnClose = (ImageView) searchView.findViewById(R.id.abs__search_close_btn);
               searchBtnClose.setImageResource(R.drawable.ic_cancel_search_bar);
      
      
               SearchAutoComplete searchText = (SearchAutoComplete) searchView.findViewById(R.id.abs__search_src_text);
      
              searchText.setTextColor(context.getResources().getColor(color.white));
      
              return searchView;
          }
      
      
          public static SearchView getSearchView(Context context, int strHintRes) {
              return getSearchView(context, context.getString(strHintRes));
          }
      }
      

      希望对大家有所帮助。 :D

      【讨论】:

        【解决方案9】:
        searchView = (SearchView) view.findViewById(R.id.searchView);
        
        SearchView.SearchAutoComplete searchText = (SearchView.SearchAutoComplete) searchView
              .findViewById(org.holoeverywhere.R.id.search_src_text);
        searchText.setTextColor(Color.BLACK);
        

        我正在使用 Holoeverywhere 库。注意 org.holoeverywhere.R.id.search_src_text

        【讨论】:

          【解决方案10】:
          TextView textView = (TextView) searchView.findViewById(R.id.search_src_text);
          textView.setTextColor(Color.BLACK);
          

          【讨论】:

            【解决方案11】:

            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 的视图层次结构,否则您应该可以在一段时间内使用此方法更改小部件的外观。

            【讨论】:

              【解决方案12】:

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

              确保您的应用程序使用主题自定义主题,如 enter link description here 中所述

              【讨论】:

                【解决方案13】:

                我想做类似的事情。我终于不得不在SearchView 孩子中找到TextView

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

                如果你想要 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;
                }
                

                【讨论】:

                • 我以前的解决方案也遇到了空异常,这对我有用!
                • 这应该被标记为接受的答案。上面的一个给出null。谢谢你的好工作伙伴:)
                • 这个解决方案太恶心了,但它是唯一真正有效的解决方案,这要怪安卓团队!
                • 我们如何使用这个访问搜索图标?
                • 太棒了!有史以来最好的解决方案......所有这些 xml 设计都是......
                【解决方案14】:

                可以使用 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>
                

                在值文件夹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/

                【讨论】:

                  【解决方案15】:

                  我从一篇博文中找到了解决方案。见here

                  基本上你设置了 searchViewAutoCompleteTextView 的样式并让 android:actionBarWidgetTheme 继承了样式。

                  【讨论】:

                    【解决方案16】:

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

                    对我来说,以下作品。 我使用了链接中的代码: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 color 建议另一种解决方案。它可以工作,但只设置提示文本和颜色。

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

                    【讨论】:

                    • 如果您已将项目迁移到 AndroidX,请使用 androidx.appcompat.R.id.search_src_text
                    【解决方案17】:

                    添加

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

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

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

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

                    【讨论】:

                    • 这为我修复了它,并且不涉及任何使用反射的复杂恶作剧。也适用于 appcompat。
                    • 您还可以将searchview上的android:theme设置为具有android:editTextColor元素的样式。这样你只会影响那个搜索视图。
                    • 有效!我在工具栏内使用搜索视图,扩展了 ThemeOverlay.AppCompat.Light 并添加了上述项目并在工具栏小部件上设置样式;)就像一个魅力......
                    • 这个解决方案对我不起作用。文本仍然有错误的颜色。下面以编程方式设置提示颜色的答案对我有用。
                    • 这个答案的问题是它也会改变你EditTexts的文本颜色。唯一对我有用的单独更改 SearchView 文本颜色的解决方案是:stackoverflow.com/a/40550067/1617737
                    【解决方案18】:

                    如果您使用的是 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,但如果您知道在哪里查看,它们会为 AppCompat 公开。 :)

                    【讨论】:

                    • 这实际上在各种设备上运行良好。不错。
                    【解决方案19】:

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

                    将工具栏主题设置为@style/ThemeOverlay.AppCompat.Light 将为提示文本和输入文本生成深色(黑色),但不会影响光标*颜色。因此,将 Toolbar 主题设置为 @style/ThemeOverlay.AppCompat.Dark 将为提示文本和输入的文本生成浅色(白色),光标* 无论如何都是白色的。

                    自定义上述主题:

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

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

                    android:textColorHint --> 提示的颜色

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

                    【讨论】:

                    • 光标颜色来自colorControlActivated
                    【解决方案20】:

                    是的,我们可以,

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

                    要为 SerachView 文本应用白色,

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

                    编码愉快!!!!

                    【讨论】:

                      【解决方案21】:

                      这对我有用。

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

                      【讨论】:

                      • 如果您将视图作为 TextView 或什至作为 AutoCompleteTextView 获取,这也适用。
                      • 是的,这是在遵循@JuanJoséMeleroGómez 的建议后为我工作的
                      • 我想,这是从我的回答中复制并缩短的:stackoverflow.com/a/26251197/2914140
                      【解决方案22】:

                      SearchView 转换为 TextView 允许您使用 TextView 的方法来更改其颜色:

                      ((TextView) searchView).setTextColor(Color.WHITE);
                      ((TextView) searchView).setHintTextColor(Color.WHITE);
                      

                      【讨论】:

                        【解决方案23】:

                        它对我有用

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

                        【讨论】:

                          【解决方案24】:

                          通过使用它,我能够在搜索视图中更改键入的颜色文本

                          AutoCompleteTextView typed_text = (AutoCompleteTextView) inputSearch.findViewById(inputSearch.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
                          typed_text.setTextColor(Color.WHITE);
                          

                          【讨论】:

                            【解决方案25】:

                            这对我有用。

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

                            【讨论】:

                              【解决方案26】:

                              您可以通过在样式中设置editTextColor 属性来做到这一点。

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

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

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

                              【讨论】:

                              • +1 这胜过所有涉及 findViewById 恕我直言的解决方案。应尽可能使用视图样式。
                              • 这应该是公认的答案!非常干净和容易。
                              【解决方案27】:

                              如果你使用 - 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);
                              

                              在 Kotlin 中

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

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

                              【讨论】:

                                【解决方案28】:

                                哇。很多答案。它从您的原色中获取颜色值。

                                改变它,它就完成了!

                                @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>
                                

                                【讨论】:

                                  【解决方案29】:

                                  为您的Toolbar 创建样式

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

                                  并将其设置为Toolbar的主题

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

                                  【讨论】:

                                    【解决方案30】:

                                    最干净的方法是:

                                    工具栏使用主题 ThemeOverlay.AppCompat.Dark.Actionbar。

                                    现在把它变成这样的孩子:

                                    toolbarStyle 父级 "ThemeOverlay.AppCompat.Dark.Actionbar"

                                    以该样式添加此项目

                                    项目名称 "android:editTextColor">你的颜色

                                    完成。

                                    还有一个非常重要的事情是,在工具栏中放置 layout_height="?attr/actionbarSize"。默认情况下它是 wrap_content。对我来说,搜索视图中甚至看不到文本,它解决了这个问题。

                                    【讨论】:

                                      猜你喜欢
                                      • 1970-01-01
                                      • 2016-09-06
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 2021-08-20
                                      相关资源
                                      最近更新 更多