SearchView元素没有更改文本颜色的任何属性。默认文本颜色是黑色,不适用于我们的深色背景。有没有一种方法可以更改文本颜色,而不必诉诸hack?
我发现了一个类似的问题,与更改文本大小有关,但到目前为止,它还没有任何答案:如何设置SearchView TextSize?
SearchView元素没有更改文本颜色的任何属性。默认文本颜色是黑色,不适用于我们的深色背景。有没有一种方法可以更改文本颜色,而不必诉诸hack?
我发现了一个类似的问题,与更改文本大小有关,但到目前为止,它还没有任何答案:如何设置SearchView TextSize?
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,但如果您知道在哪里查找,它们会公开 id。 :)
@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;
}
<style name="Theme.MyTheme" parent="android:Theme.Holo">
我没有找到其他改变SearchView文本颜色的方法,除非使用一些不太规范的方法。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);
使用这种方法,我成功地在提示中添加了红色的*标记。根据您的需要更改此方法。希望对您有所帮助....:)
TextView textView = (TextView) searchView.findViewById(androidx.appcompat.R.id.search_src_text);
textView.setTextColor(Color.RED);
textView.setHintTextColorstrong text(Color.RED);
Yes we can,
SearchView searchView = (SearchView) findViewById(R.id.sv_symbol);
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.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);
}
AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchText.setHintTextColor(getResources().getColor(color.black));
searchText.setTextColor(getResources().getColor(color.black));
更改输入文本的颜色:
((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);