在 Android 操作栏中设置 SearchView 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11527636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Styling a SearchView in Android Action Bar
提问by TheLettuceMaster
I have a search widget in my Action Bar like this:
我的操作栏中有一个搜索小部件,如下所示:
(1) How do I change the color of the text that says "iPhone"?
(1) 如何更改显示“iPhone”的文本颜色?
(2) Also, if you notice the gray X -- the entire Search Widget is that color as well when it is in an icon position. I am on Holo.Theme.Light and utilizing my own mods to it.
(2) 此外,如果您注意到灰色的 X——当它处于图标位置时,整个搜索小部件也是该颜色。我在 Holo.Theme.Light 上并使用我自己的模组。
How do I change these two styles for the widget in my styles.xml file (assuming that is where you make the changes for a search widget)?
如何在我的styles.xml 文件中为小部件更改这两种样式(假设您在此处对搜索小部件进行了更改)?
采纳答案by lokoko
You would have to set
你必须设置
searchView.setBackgroundColor(Color.WHITE);
SearchViews are not so customizable.
SearchViews 不是那么可定制。
回答by Adidas
I have been spending many time for this but finally: :-)
我为此花了很多时间,但终于::-)
1) To change the text color :
1) 改变文字颜色:
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE);
2) To change the text hint :
2) 更改文本提示:
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE);
回答by Abdul Rahman
If you're using appcompat library, then the solution is a bit different from Jerome's answer. Here's my solution
如果您使用的是 appcompat 库,那么解决方案与 Jerome 的答案略有不同。这是我的解决方案
@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);
}
回答by Jerome VDL
You can change the style of the searchview. Here is some code I used to tweak the edittext, the voice icon...
您可以更改搜索视图的样式。这是我用来调整编辑文本、语音图标的一些代码......
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));
[edit] A more complete answer is available here : Changing the background drawable of the searchview widget
[编辑] 这里有一个更完整的答案:更改搜索视图小部件的背景可绘制
回答by QAMAR
As of Lollipop you can style search view like this from blog post http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
从 Lollipop 开始,您可以从博客文章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 (e.g. EditText) -->
<item name="queryBackground">...</item>
<!-- Background for the actions section (e.g. 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>
回答by Ali Ziaee
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
回答by TheLettuceMaster
The way I solved this was by using Holo.Light.DarkActionBar theme. I don't believe you can easily change the search widget otherwise.
我解决这个问题的方法是使用 Holo.Light.DarkActionBar 主题。我不相信您可以轻松更改搜索小部件。
回答by Mina Gabriel
in onCreateOptionsMenu :
在 onCreateOptionsMenu :
int id = searchView.getContext()
.getResources()
.getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);
回答by M.Raheel
@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);
}
Now this is the xml file searchcolor.xml for changing the plate color of search bar
现在这是用于更改搜索栏板颜色的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>
and menu/menu.xml for creating the search in action bar
和 menu/menu.xml 用于在操作栏中创建搜索
<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>
回答by Mehdiway
If you want white text, you should extend from an Action Bar theme that has a black background like Holo.Theme
or Theme.AppCompat.Light.DarkActionBar
if you're using the support library.
No other settings necessary.
如果您想要白色文本,您应该从具有黑色背景的操作栏主题扩展,Holo.Theme
或者Theme.AppCompat.Light.DarkActionBar
如果您正在使用支持库。
无需其他设置。