[英]Changing action bar searchview hint text color
如何更改操作栏搜索视图提示文本颜色?
这个问题解释了如何在使用 ABS 时获取 EditText: Android ActionBar Customize Search View
是否有一个 android.R.id 我可以用来获取对 EditText 的引用,以便我可以更改提示颜色? 或者有其他方法可以改变颜色吗?
android:actionBarWidgetTheme你的主主题应该指向一个包含android:textColorHint的样式。 那将允许更改搜索视图的提示文本颜色。
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("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);
这对我有用,希望它有帮助
((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.white));
我解决了将这个属性添加到主题的问题:
<style name="AppTheme" parent="AppBaseTheme">
.....
.....
<item name="android:actionBarWidgetTheme">@style/MyActionBarWidgetTheme</item>
</style>
然后添加我想在这种情况下更改提示颜色的属性。
<style name="MyActionBarWidgetTheme" parent="@android:style/Theme.Holo">
.....
.....
<item name="android:textColorHint">@color/blanco_60</item>
</style>
希望它有帮助:D
以下是您如何应用上面提示的样式的具体方法。 正确的答案是使用您自己的自定义样式重载操作栏小部件样式。 对于带有深色操作栏的全息灯,请将其放在您自己的样式文件中,例如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>
确保您的应用程序使用主题自定义主题,如在此处输入链接描述中所述
SearchView
对象从LinearLayout
扩展而来,因此它包含其他视图。 诀窍是找到包含提示文本的视图并以编程方式更改颜色。 通过遍历视图层次结构,您可以找到包含提示文本的小部件:
// 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));
此方法适用于任何主题。 此外,您可以更改SearchView
层次结构中其他小部件的外观,例如保存搜索查询的EditText
。
如果要更改搜索视图中的左箭头按钮,请添加 app:collapseIcon。
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:collapseIcon="@drawable/ic_search_view_home"
/>
如果要更改文本颜色和文本提示颜色,请添加以下内容。
EditText searchEditText = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setTextColor(Color.WHITE);
searchEditText.setHintTextColor(Color.WHITE);
如果要更改关闭图标,请添加以下内容。
ImageView imvClose = searchView.findViewById(android.support.v7.appcompat.R.id
.search_close_btn);
imvClose.setImageResource(R.drawable.ic_search_view_close);
刚刚获得textView
搜索小插件和使用的ID setTextColor()
方法来改变搜索文本颜色和setHintTextColor()
来改变搜索的提示文本颜色。
// Get textview id of search widget
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
// Set search text color
textView.setTextColor(Color.WHITE);
// Set search hints color
textView.setHintTextColor(Color.GRAY);
使用最新的 appcompat 库(或者如果您的目标是 5.0+),这可以使用ThemeOverlay
完全在 XML 中ThemeOverlay
:
<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
,您可以将其添加到Activity
的主题:
<style name="Theme.MyApp" parent="Theme.AppCompat">
<item name="actionBarTheme">@style/ThemeOverlay.MyApp.ActionBar</item>
...
</style>
这对我有用:-)
EditText seartext = searchView.findViewById(R.id.search_src_text);
seartext.setTextColor(Color.WHITE);
seartext.setHintTextColor(Color.GRAY);
试试这个代码。 这很容易。
// 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));
看看这个类,它可以帮助你解决你的问题
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));
}
}
通过使用它,您可以更改 Searchable 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>
更改搜索视图提示文本颜色的解决方案是:
SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchAutoComplete.setHintTextColor(Color.GRAY);
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);
使用styles.xml
实现这一点真的很简单只要理解主题化SearchView 与工具栏的主题直接相关,工具栏的menu.xml
是作为app:actionViewClass
。
您也可以手动在 menuItem 上设置 SearchView,从而完全控制视图。
把它放在你的XML文件中:
android:textColorHint="@android:color/white"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.