采用Android工具栏的搜索查看更改TEXTCOLOR [英] Change TextColor in SearchView using Android Toolbar

查看:211
本文介绍了采用Android工具栏的搜索查看更改TEXTCOLOR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我设置我的动作条使用工具栏而不是使用该API 21与appcompat-V7:21。该 textColorPrimary 风格标签被配置使用的颜色 @android:彩色/白色,所以所有的新游戏Android的工具栏将有一个白色的文本颜色(到目前为止好)。

I have the following problem. I've setup my Actionbar to use a Toolbar instead using the API 21 with the appcompat-v7:21. The textColorPrimary style tag is configured to use the color @android:color/white, so all the titles in the new Android Toolbar will have a white text color (so far so good).

现在从来就增加了一个搜索查看和设置自定义背景,以这样的:

Now I´ve added a SearchView, and setup a custom background to it like this:

<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/action_bar_background</item>
    <item name="colorPrimaryDark">@color/status_background</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="searchViewStyle">@style/SearchViewStyle</item>
</style>

<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
    <item name="queryBackground">@drawable/search_background</item>
    <item name="searchIcon">@drawable/icon_search</item>
</style>

提拉 @绘制/ search_background 是一个纯白色的矩形。所以,你猜怎么着?由于在工具栏标题文字颜色是白色的,现在在搜索查看文本颜色为白色为好,而作为搜索查看背景是白色矩形,用户不能看到他打字。

The drawable @drawable/search_background is a plain white rectangle. So guess what? As the title text color in the Toolbar is white, now the text color in the SearchView is white as well, and as the SearchView background is a white rectangle, the user can't see what he's typing.

我的问题是:怎样才可以有不同的文字颜色为Android工具栏标题和搜索查看文本

My Question is: How can I have different text colors for the Android Toolbar Title and the SearchView text?

推荐答案

经过一番研究,我发现了一个安全的方式来做到这一点。

After some research I found a secure way to do it.

挖在和SearchView样式,我发现,用于显示和SearchView布局。这里面布置有(其中您在搜索查看键入的实际场)一个TextView

Digging up in the SearchView styles, I found the layout that is used to display the SearchView. Inside that layout there's a TextView (the actual field where you type in the SearchView)

<TextView
        android:id="@+id/search_badge"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:layout_marginBottom="2dip"
        android:drawablePadding="0dip"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="?android:attr/textColorPrimary"
        android:visibility="gone" />

请注意字段安卓文字颜色=机器人:ATTR / textColorPrimary。这会导致这个问题我原来有,在搜索查看文本颜色是一样的,在Android的工具栏标题文字颜色定义的。

Notice the field android:textColor="?android:attr/textColorPrimary". This causes the problem I originally had, the text color in the SearchView is the same as the one defined for the Title text color in the Android Toolbar.

现在有正有几种解决方案,可能工作<一个href="http://stackoverflow.com/questions/11321129/is-it-possible-to-change-the-textcolor-on-an-android-searchview">here,但我认为,大多数这些解决方案有同样的问题。它们都依赖于TextView中的ID,以访问视图,改变文字颜色,如这里

Now there're are several solutions that might work here, but I think that most of those solutions have the same problem. They all rely in the id of the TextView in order to access the view and change the text color, as described here

我个人认为这里面硬编码的code中的TextView中的ID是非常危险的,因为我们不知道明天谷歌决定使用另一值id为这一观点,在这种情况下,我们的code会遭到破坏。

Personally I think that hardcoding the id of the TextView inside the code is highly risky, because we don't know if tomorrow Google decides to use another id value for this view, in that case, our code will be broken.

由于这个原因我已经创建了一个获得在搜索查看TextView的对象递归方法,并改变颜色到任何我们想要的。

For that reason I've created a recursive method that obtains the TextView object in the SearchView, and changes the color to whatever we want.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
    inflater.inflate(R.menu.search, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

    searchView.setOnQueryTextListener(new SearchTextListener());
    changeSearchViewTextColor(searchView);
}


private void changeSearchViewTextColor(View view) {
    if (view != null) {
        if (view instanceof TextView) {
            ((TextView) view).setTextColor(Color.BLACK);
            return;
        } else if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                changeSearchViewTextColor(viewGroup.getChildAt(i));
            }
        }
    }
}

我测试过这个code。使用API​​ 21与工具栏,具有工具栏标题文字颜色设置​​为白色,并设置了搜索查看文本颜色为黑色和它完美的作品。另外,我们直接访问TextView的对象,我们可以改变的暗示,绘制,填充,和一切相关的TextView。

I've tested this code using API 21 with the Toolbar, having the Toolbar Title text color set to white, and setting up the SearchView text color to black and it works perfectly. Also, as we're accessing the TextView object directly, we can change the hint, drawable, paddings, and everything related to the TextView.

这篇关于采用Android工具栏的搜索查看更改TEXTCOLOR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆