如何更改搜索建议文本颜色

时间:2015-01-31 02:40:40

标签: android search

我希望它是黑色的。我试图更改textPrimaryColor但搜索输入字段也受到影响,我不希望这种情况发生。

Screenshot

这是我对Action Bar的风格。

<style name="MyActionBar" parent="Theme.AppCompat.Light">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorPrimaryInverse">@android:color/white</item>
    <item name="android:textColorHint">@android:color/white</item>
    <item name="android:actionMenuTextColor">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
</style>

2 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,并在此处找到了解决方案:https://code.google.com/p/android/issues/detail?id=5237#c8

修改

尝试覆盖建议文字的样式。

在“MyActionBar”样式中添加以下项目:

<item name="android:autoCompleteTextViewStyle">@style/AutoCompleteTextViewLight</item>

创建固定颜色的样式AutoCompleteTextViewLight:

<style name="AutoCompleteTextViewLight" parent="@android:style/Widget.AutoCompleteTextView">
    <item name="android:textColor">@android:color/primary_text_light</item>
</style>

答案 1 :(得分:0)

我之前遇到过这个问题并为此创造了一个解决方案。

/**
 * @param viewGroup
 *            View to search for child views inside
 * @param _class
 *            child view class inside the View
 * @return child view of corresponding View class
 */
public static <V extends View> Collection<V> findChildrenByClass(ViewGroup viewGroup, Class<V> _class) {

    return gatherChildrenByClass(viewGroup, _class, new ArrayList<V>());
}

/**
 * @param viewGroup
 *            View to search for child views inside
 * @param _class
 *            child view class inside the View
 * @param childrenFound
 *            child view if found
 * @return child view of corresponding View class
 */
@SuppressWarnings("unchecked")
private static <V extends View> Collection<V> gatherChildrenByClass(ViewGroup viewGroup, Class<V> _class, Collection<V> childrenFound) {

    for (int i = 0; i < viewGroup.getChildCount(); i++)
    {
        final View child = viewGroup.getChildAt(i);
        if (_class.isAssignableFrom(child.getClass())) {
            childrenFound.add((V)child); // unchecked on this line
        }
        if (child instanceof ViewGroup) {
            gatherChildrenByClass((ViewGroup) child, _class, childrenFound);
        }
    }

    return childrenFound;
}

以下两种方法可以查找SearchView的所有子视图,目前,SearchView有三个子视图:

  • TextView可以在下拉建议列表中看到
  • 您在AutoCompleteTextView 中输入的
  • SearchView
  • ImageView这是SearchView
  • 右侧的近景图片

现在,让我们找到TextView然后更改其颜色。在ActivityFragment

中执行此操作
// Setting up the TextView of searchView suggestions drop down list
for (TextView textView : findChildrenByClass(mSearchView, TextView.class)) {
     textView.setHint("Type a word");
     textView.setTextColor(Color.rgb(255, 255, 255));
     textView.setHintTextColor(Color.argb(128, 255, 255, 255));
}

全部完成:)