更改操作栏searchview提示文本颜色

时间:2013-07-22 15:22:01

标签: android search android-actionbar

如何更改操作栏搜索视图提示文字颜色?

此问题解释了如何在使用ABS时获取EditText: Android ActionBar Customize Search View

是否有android.R.id我可以用来获取EditText的引用,以便我可以更改提示颜色?或者还有其他方法可以改变颜色吗?

Action bar search view hint text

19 个答案:

答案 0 :(得分:49)

searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getResources().getString(R.string.hintSearchMess) + "</font>"));

答案 1 :(得分:48)

主题的

android:actionBarWidgetTheme 应指向其中包含 android:textColorHint 的样式。那个将允许更改搜索视图的提示文本颜色。

答案 2 :(得分:21)

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);

答案 3 :(得分:13)

这对我有用,希望有所帮助

((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.white));

also see this

答案 4 :(得分:10)

我决定将此属性添加到主题:

<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

答案 5 :(得分:7)

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

答案 6 :(得分:6)

具体如何应用上面提到的风格。正确的答案是使用您自己的自定义样式重置操作栏窗口小部件样式。对于具有暗动作条的全息灯,请将其放在您自己的样式文件中,例如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

中所述的主题自定义主题

答案 7 :(得分:5)

只需获取搜索小部件的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);

答案 8 :(得分:4)

如果要在搜索视图中更改左箭头按钮,请添加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);

答案 9 :(得分:3)

在本课程中,它可以帮助您解决问题

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));
    }
}

答案 10 :(得分:3)

使用最新的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>

答案 11 :(得分:2)

通过使用它,您可以更改可搜索的xml

  1. 搜索文字提示颜色
  2. 搜索文字
  3. 搜索关闭图标
  4. 搜索提示图标
  5. 搜索板
  6. 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>
    

答案 12 :(得分:2)

试试这段代码。这很容易。

    // 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));

答案 13 :(得分:1)

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);

答案 14 :(得分:1)

这对我有用:-)

 EditText seartext = searchView.findViewById(R.id.search_src_text);
    seartext.setTextColor(Color.WHITE);
    seartext.setHintTextColor(Color.GRAY);

答案 15 :(得分:1)

使用where实现这一点非常简单只需了解主题化SearchView与工具栏的主题直接相关,其中styles.xml将其作为menu.xml获取。

  1. 为工具栏创建样式。
  2. 将该样式应用于工具栏上的主题 它改变了相关的观点及其属性。

答案 16 :(得分:1)

更改搜索视图提示文本颜色的解决方案是:

 SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
 searchAutoComplete.setHintTextColor(Color.GRAY);

答案 17 :(得分:1)

您也可以手动在menuItem上设置SearchView,从而完全控制视图。

答案 18 :(得分:-2)

将它放在您的XML文件中:

android:textColorHint="@android:color/white"
相关问题