在Android操作栏中设置SearchView的样式

我的操作栏中有一个search小部件,如下所示:

在Action Bar中搜索Widget

(1)如何更改“iPhone”文本的颜色?

(2)此外,如果您注意到灰色X – 整个search窗口小部件在图标位置时也是该颜色。 我在Holo.Theme.Light和利用我自己的MODS。

如何在我的styles.xml文件中更改这两个窗口小部件的样式(假设这是您对search小部件进行更改的位置)?

你将不得不设置

searchView.setBackgroundColor(Color.WHITE); 

SearchViews不是如此可定制的。

我已经花了很多时间,但最后::-)

1)改变文字颜色:

 ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE); 

2)改变文字提示:

 ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE); 

您可以更改searchview的样式。 这里是我用来调整edittext,语音图标的一些代码…

 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null); searchView.findViewById(searchPlateId).setBackgroundResource(R.drawable.textfield_search_selected); int voiceSearchPlateId = searchView.getContext().getResources().getIdentifier("android:id/submit_area", null, null); searchView.findViewById(voiceSearchPlateId).setBackgroundResource(R.drawable.textfield_search_right_selected); // change hint color int searchTextViewId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null); TextView searchTextView = (TextView) searchView.findViewById(searchTextViewId); searchTextView.setHintTextColor(getResources().getColor(R.color.light_grey)); 

更完整的答案在这里可用: 更改searchview小部件的背景可绘制

如果你使用的是appcompat库,那么解决scheme与Jerome的答案有点不同。 这是我的解决scheme

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu, menu); restoreActionBar(); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); MenuItem searchMenuItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); searchAutoComplete.setHintTextColor(Color.WHITE); searchAutoComplete.setTextColor(Color.WHITE); View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate); searchplate.setBackgroundResource(R.drawable.texfield_searchview_holo_light); ImageView searchCloseIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn); searchCloseIcon.setImageResource(R.drawable.clear_search); ImageView voiceIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn); voiceIcon.setImageResource(R.drawable.abc_ic_voice_search); ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon); searchIcon.setImageResource(R.drawable.abc_ic_search); return super.onCreateOptionsMenu(menu); } 

作为棒棒糖,你可以像这样从博客文章风格search视图http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

 values/themes.xml: <style name=”Theme.MyTheme” parent=”Theme.AppCompat”> <item name=”searchViewStyle”>@style/MySearchViewStyle</item> </style> <style name=”MySearchViewStyle” parent=”Widget.AppCompat.SearchView”> <!-- Background for the search query section (eg EditText) --> <item name="queryBackground">...</item> <!-- Background for the actions section (eg voice, submit) --> <item name="submitBackground">...</item> <!-- Close button icon --> <item name="closeIcon">...</item> <!-- Search button icon --> <item name="searchIcon">...</item> <!-- Go/commit button icon --> <item name="goIcon">...</item> <!-- Voice search button icon --> <item name="voiceIcon">...</item> <!-- Commit icon shown in the query suggestion row --> <item name="commitIcon">...</item> <!-- Layout for query suggestion rows --> <item name="suggestionRowLayout">...</item> </style> 

我解决这个问题的方式是使用Holo.Light.DarkActionBar主题。 我不相信你可以很容易地改变search部件,否则。

onCreateOptionsMenu :

 int id = searchView.getContext() .getResources() .getIdentifier("android:id/search_src_text", null, null); TextView textView = (TextView) searchView.findViewById(id); textView.setTextColor(Color.WHITE); 
 SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search)); searchView.setSubmitButtonEnabled(true); // to enable submit button ImageView b = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_go_btn); b.setImageResource(android.R.drawable.ic_menu_search); //to change submit button icon TextView a=(TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); a.setTextColor(...); //to change color of search text 

如果你想要白色文本,你应该从一个黑色的背景像Holo.Theme或者Theme.AppCompat.Light.DarkActionBar的Action Bar主题扩展,如果你使用的是支持库。
没有其他必要的设置。

 @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); // Associate search configuration with the SearchView SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); searchView.setQueryHint("Client/DOB/PPSN"); searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName())); // Setting the textview default behaviour properties int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null); TextView textView = (TextView) searchView.findViewById(id); textView.setTextColor(Color.WHITE); textView.setHintTextColor(Color.WHITE); // Set the search plate color to white int linlayId = getResources().getIdentifier("android:id/search_plate", null, null); View view = searchView.findViewById(linlayId); Drawable drawColor = getResources().getDrawable(R.drawable.searchcolor); view.setBackground( drawColor ); return super.onCreateOptionsMenu(menu); } 

现在,这是用于更改search栏板颜色的xml文件searchcolor.xml

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape > <solid android:color="#ffffff" /> </shape> </item> <!-- main color --> <item android:bottom="1.5dp" android:left="1.5dp" android:right="1.5dp"> <shape > <solid android:color="#2c4d8e" /> </shape> </item> <!-- draw another block to cut-off the left and right bars --> <item android:bottom="10.0dp"> <shape > <solid android:color="#2c4d8e" /> </shape> </item> </layer-list> 

和menu / menu.xml来在操作栏中创buildsearch

 <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/search" android:icon="@drawable/search" android:showAsAction="ifRoom" android:actionViewClass="android.widget.SearchView" /> </menu> 

在您的xxxActivity.java文件中获取SearchView,然后:

 SearchView searchView = (SearchView)menu.findItem(R.id.my_search_view).getActionView(); int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null); // Getting the 'search_plate' LinearLayout. View searchPlate = searchView.findViewById(searchPlateId); searchPlate.setBackgroundResource(R.drawable.textfield_searchview); 

创build文件res/drawable/texfield_searchview.xml

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:drawable="@drawable/textfield_search_selected_holo_light" /> <item android:drawable="@drawable/textfield_search_default_holo_light" /> </selector>