是否可以更改Android SearchView上的textcolor?

SearchView元素没有用于更改文本颜色的任何属性。 默认的文字颜色是黑色的,在我们的黑暗背景下不起作用。 有没有办法改变文本的颜色,而不诉诸黑客?

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

尝试这样的事情:你会得到一个来自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); 

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

到父主题,并应该改变input的文字。 你也可以使用

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

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

这对我有用。

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

我想做类似的事情。 我终于必须在SearchView子项中findTextView

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

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

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

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

  searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); EditText txtSearch = ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)); txtSearch.setHint(getResources().getString(R.string.search_hint)); txtSearch.setHintTextColor(Color.LTGRAY); txtSearch.setTextColor(Color.WHITE); 

更改操作栏searchview提示文字颜色build议另一个解决scheme。 它的作品,但只设置提示文字和颜色。

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

如果您使用的是android.support.v7.widget.SearchView,则可以不必使用reflection。

以下是我在应用程序中的操作:

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

他们不公开公开ID为非appcompat SearchView,但他们为AppCompat做,如果你知道在哪里看。 🙂

我有这个问题,这对我有用。

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

您可以通过在样式中设置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> 

如果您的应用程序主题基于holo主题,则在SearchView中将显示白色而不是黑色文本

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

我没有find任何其他方式来改变searchview的文本颜色,而不使用肮脏的黑客。

使用这个,是对的。 :d

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

我们可以,

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

快乐的编码!

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

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

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

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

SearchView对象从LinearLayout扩展,所以它包含其他视图。 诀窍是find持有提示文本的视图,并以编程方式更改颜色。 试图通过id查找视图的问题是,id与应用程序中使用的主题相关。 所以根据使用的主题, findViewById(int id)方法可能会返回null 。 适用于每个主题的更好方法是遍历视图层次结构,并find包含提示文本的小部件:

 // 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层次结构中其他窗口小部件的外观,如保存search查询的EditText 。 除非Google决定尽快更改SearchView的视图层次结构,否则应该可以使用此方法更改小部件的外观一段时间。

可以通过使用appcompat v7 library来自定义searchview。我使用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, ie 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" > 

欲了解更多信息,请参阅此URL:
这里http://www.jayway.com/2014/06/02/android-theming-the-actionbar/

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

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

定制上述主题:

android:textColorPrimary – >input文字的颜色

editTextColor – >input文本的颜色(将重写android:textColorPrimary的影响,如果设置)

android:textColorHint – >提示的颜色

*注:我尚未确定如何控制光标颜色(不使用reflection解决scheme)。

通过使用这个,我可以在search视图中更改input的颜色文本

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

这是为我工作。

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

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

改变它,完成!

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

为您的Toolbar创build一个样式

 <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" ... 

我试着find一个解决scheme。 我认为这会帮助你

 searchView.setBackgroundColor(Color.WHITE); 

在这里输入图像描述

改变input文字的颜色:

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

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

我从博客文章中find了一个解决scheme。 看到这里 。

基本上你风格searchViewAutoCompleteTextView并有android:actionBarWidgetThemeinheritance风格。

它与我合作

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

最干净的方法是:

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

现在让它的孩子像:

toolbarStyle父“ThemeOverlay.AppCompat.Dark.Actionbar”

添加这个样式的项目

项目名称“android:editTextColor”> yourcolor

完成。

另一个非常重要的是,在工具栏中放置layout_height =“?attr / actionbarSize”。 默认情况下,它是wrap_content.For文本是甚至不可见在searchview它解决了这个问题。

这对我有效: –

 <style name="SearchTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <item name="android:editTextColor">@android:color/white</item> <item name="android:textColorHint">@color/deepblue</item> <item name="android:textSize">16dp</item> <item name="android:textColor">@color/darkdeepdray</item> 

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

 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