更改操作栏searchview提示文本颜色
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更改操作栏searchview提示文本颜色相关的知识,希望对你有一定的参考价值。
如何更改操作栏搜索视图提示文本颜色?
这个问题解释了如何在使用ABS时获取EditText:Android ActionBar Customize Search View
有没有android.R.id我可以用来获取EditText的引用,所以我可以改变提示颜色?或者还有其他方法可以改变颜色吗?
你主要主题的android:actionBarWidgetTheme应该指向一个带有android:textColorHint的样式。那个将允许更改搜索视图的提示文本颜色。
看看这门课,它可以帮助您解决问题
import android.R.color;
import android.content.Context;
import android.graphics.Typeface;
import android.widget.ImageView;
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);
setFont(context, searchText);
searchText.setTextColor(context.getResources().getColor(color.white));
searchText.setHintTextColor(context.getResources().getColor(color.white));
return searchView;
}
private static void setFont(Context _context, SearchAutoComplete searchText) {
Typeface tf = null;
Typeface currentType = searchText.getTypeface();
if (currentType == null) {
tf = MyApplication.fontNormal;
}
else {
int currentStyle = currentType.getStyle();
if (currentStyle == Typeface.BOLD) {
tf = MyApplication.fontBold;
}
else if (currentStyle == Typeface.BOLD_ITALIC) {
tf = MyApplication.fontBoldItalic;
}
else if (currentStyle == Typeface.ITALIC) {
tf = MyApplication.fontItalic;
}
else {
tf = MyApplication.fontNormal;
}
}
searchText.setTypeface(tf);
}
public static SearchView getSearchView(Context context, int strHintRes) {
return getSearchView(context, context.getString(strHintRes));
}
}
使用最新的appcompat库(或者如果您的目标是5.0+),可以使用ThemeOverlay
完全使用XML:
<style name="ThemeOverlay.MyApp.ActionBar" parent="ThemeOverlay.AppCompat.ActionBar">
<item name="autoCompleteTextViewStyle">@style/Widget.MyApp.AutoCompleteTextView.SearchView</item>
</style>
<style name="Widget.MyApp.AutoCompleteTextView.SearchView" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:textColor">#000</item>
<item name="android:textColorHint">#5000</item>
</style>
然后在你的布局文件中:
<android.support.v7.widget.Toolbar
app:theme="@style/ThemeOverlay.MyApp.ActionBar"
... />
如果您还希望它适用于使用ActionBar
而不是Toolbar
的活动,您可以将其添加到Activity
的主题:
<style name="Theme.MyApp" parent="Theme.AppCompat">
<item name="actionBarTheme">@style/ThemeOverlay.MyApp.ActionBar</item>
...
</style>
通过使用它,您可以更改可搜索的xml
- 搜索文字提示颜色
- 搜索文字
- 搜索关闭图标
- 搜索提示图标
- 搜索板块
int searchHintBtnId = searchView.getContext().getResources() .getIdentifier("android:id/search_mag_icon", null, null); int hintTextId = searchView.getContext() .getResources() .getIdentifier("android:id/search_src_text", null, null); int closeBtnId = searchView.getContext() .getResources() .getIdentifier("android:id/search_close_btn", null, null); // Set the search plate color to white int searchPlateId = getResources().getIdentifier("android:id/search_plate", null, null); View view = searchView.findViewById(searchPlateId); view.setBackgroundResource(R.drawable.search_plate); ImageView closeIcon = (ImageView) searchView.findViewById(closeBtnId); closeIcon.setImageResource(R.drawable.close); ImageView searchHintIcon = (ImageView) searchView.findViewById(searchHintBtnId); searchHintIcon.setImageResource(R.drawable.search); TextView textView = (TextView) searchView.findViewById(hintTextId); textView.setTextColor(getResources().getColor(R.color.search_text_color)); textView.setHintTextColor(getResources().getColor(R.color.search_hint_text_color));
这是drawable / search_plate.xml
<!-- main color --> <item android:bottom="1dp" android:left="1dp" android:right="1dp"> <shape > <solid android:color="@color/app_theme_color" /> </shape> </item> <!-- draw another block to cut-off the left and right bars --> <item android:bottom="10.0dp"> <shape > <solid android:color="@color/app_theme_color" /> </shape> </item> </layer-list>
试试这个代码。这很容易。
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setHint("Search");
searchEditText.setHintTextColor(getResources().getColor(R.color.white));
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchEditText.setHintTextColor(getResources()的getColor(R.color.white));
您也可以手动在menuItem上设置SearchView,从而完全控制视图。
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);
更改“搜索视图提示文本”颜色的解决方案是:
SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(Color.GRAY);
使用styles.xml
实现这一点非常简单只需要了解主题化SearchView与工具栏的主题直接相关,其中menu.xml
将其作为app:actionViewClass
获取。
- 为工具栏创建样式。
- 将该样式作为主题应用于工具栏,它会更改所有相关视图及其属性。
把它放在你的XML文件中:
android:textColorHint="@android:color/white"
searchView.setQueryHint(html.fromHtml("<font color = #ffffff>" + getResources().getString(R.string.hintSearchMess) + "</font>"));
SearchView searchView= (SearchView) findViewById(R.id.searchView1);
int id = searchView.getContext()
.getResources()
.getIdentifier("an以上是关于更改操作栏searchview提示文本颜色的主要内容,如果未能解决你的问题,请参考以下文章
在 Android 操作栏中设置 SearchView 的样式
是否可以更改 Android SearchView 上的文本颜色?
如何更改 SearchView 上的默认图标,以便在 Android 的操作栏中使用?