如何更改操作栏搜索视图提示文本颜色?
这个问题解释了如何在使用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("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
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
。
这里特别是如何应用上面提到的风格。正确的答案是使用您自己的自定义样式重置操作栏窗口小部件样式。对于具有暗动作条的全息灯,请将其放在您自己的样式文件中,例如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>
确保您的应用程序使用enter link description here中描述的主题自定义主题
只需获取搜索小部件的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);
如果要在搜索视图中更改左箭头按钮,请添加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);