【问题标题】:Change appcompat's SearchView text and hint color更改 appcompat 的 SearchView 文本和提示颜色
【发布时间】:2013-08-18 01:21:54
【问题描述】:

有人知道如何更改 appcompat SearchView 的文本颜色吗?我花了 2 个小时尝试了一些 SO 的建议,但没有一个可行。

这是我的代码:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:espacios="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/layer_switcher_button"
        android:icon="@drawable/b_categorias"
        android:title="@string/menu_layer_switcher_title"
        espacios:showAsAction="ifRoom|collapseActionView"/>
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/b_buscar"
        android:title="@string/menu_search_title"
        espacios:actionViewClass="android.support.v7.widget.SearchView"
        espacios:showAsAction="ifRoom|collapseActionView"/>

</menu>

在我的活动中,我有这个:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    menuItem layerSwitcher = menu.findItem(R.id.layer_switcher_button);
    layerSwitcher.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
        onLayerSwitcherButtonClicked();
            return false;
        }
    });

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    LinearLayout ll = (LinearLayout) searchView.getChildAt(0);
    TextView textView = (TextView) ll.getChildAt(0);

    textView.setTextColor(R.color.white);
}

【问题讨论】:

  • 您尝试过使用 SearchableInfo 吗?

标签: android styles android-actionbar searchview


【解决方案1】:
SearchView searchView = new SearchView(getContext());
SearchView.SearchAutoComplete theTextArea = (SearchView.SearchAutoComplete)searchView.findViewById(R.id.search_src_text);
theTextArea.setTextColor(Color.WHITE);//or any color that you want

【讨论】:

  • 使用android.support.v7.appcompat.R.id.search_src_text 与使用R.id.search_src_text 相比有效:stackoverflow.com/a/18813109/950427
  • @Jared Burrows 你的加入很有帮助!
【解决方案2】:

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));
// set the text color
autoComplete.setTextColor(Color.BLUE);

此方法适用于任何主题。此外,您可以更改 SearchView 层次结构中其他小部件的外观,例如保存搜索查询的 EditText

【讨论】:

  • 嘿,JfCartier,希望你能做出回应,问题是,我得到的最后一个孩子(自动完成..)它显示为 ....SearchView$SearchAutoComplete 类型...无法转换正常...SearchView.SearchAutoComplete...(这些美元符号是什么意思?
  • 美元符号表示它是一个内部类,所以它确实是SearchView.SearchAutoComplete。你遇到了什么错误?
  • 好吧,它只是不会投射到搜索自动完成......无法投射等......!好像它是另一个对象!
  • 可能是android.widget.SearchView vs android.support.v7.widget.SearchView的问题。确保在您投射的代码和菜单 XML 文件中保持一致。
  • 一个更可靠的方法就是做findById(R.id.search_src_text)。 AppCompat 的 SearchView 可以有任意的特定布局,但文本字段必须由 R.id.search_src_text 标识。
【解决方案3】:

没有一个答案对我有用。最终起作用的是在工具栏上设置主题并在那里指定 textColorHint。

主题(在 styles.xml 中):

 <style name="ToolbarTheme">
    <item name="android:textColorHint">@color/white_opacity_60</item>
</style>

工具栏(任何布局)

<android.support.v7.widget.Toolbar 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ToolbarTheme" />

之所以有效,是因为当您将主题设置为 ViewGroup 时,其属性也会应用于 ViewGroup 的所有子视图。这是主题和样式之间的区别之一:)

【讨论】:

    【解决方案4】:

    可以使用 appcompat v7 库自定义搜索视图。我使用了 appcompat v7 库并为其定义了自定义样式。 在drawable文件夹中放置bottom_border.xml文件,如下所示:

     <?xml version="1.0" encoding="utf-8"?>
     <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
     <item>
      <shape >
          <solid android:color="@color/blue_color" />
      </shape>
      </item>
     <item android:bottom="0.8dp"
      android:left="0.8dp"
      android:right="0.8dp">
      <shape >
          <solid android:color="@color/background_color" />
      </shape>
     </item>
    
     <!-- draw another block to cut-off the left and right bars -->
     <item android:bottom="2.0dp">
      <shape >
          <solid android:color="@color/main_accent" />
      </shape>
      </item>
     </layer-list>
    

    在值文件夹styles_myactionbartheme.xml中:

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
      <style name="AppnewTheme" parent="Theme.AppCompat.Light">
        <item name="android:windowBackground">@color/background</item>
        <item name="android:actionBarStyle">@style/ActionBar</item>
        <item name="android:actionBarWidgetTheme">@style/ActionBarWidget</item>
      </style> 
      <!-- Actionbar Theme -->
      <style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/main_accent</item>
        <!-- <item name="android:icon">@drawable/abc_ic_ab_back_holo_light</item> -->
      </style> 
      <style name="ActionBarWidget" parent="Theme.AppCompat.Light">
        <!-- SearchView customization-->
         <!-- Changing the small search icon when the view is expanded -->
        <!-- <item name="searchViewSearchIcon">@drawable/ic_action_search</item> -->
         <!-- Changing the cross icon to erase typed text -->
      <!--   <item name="searchViewCloseIcon">@drawable/ic_action_remove</item> -->
         <!-- Styling the background of the text field, i.e. blue bracket -->
        <item name="searchViewTextField">@drawable/bottom_border</item>
         <!-- Styling the text view that displays the typed text query -->
        <item name="searchViewAutoCompleteTextView">@style/AutoCompleteTextView</item>        
      </style>
    
     <style name="AutoCompleteTextView" parent="Widget.AppCompat.Light.AutoCompleteTextView">
        <item name="android:textColor">@color/text_color</item>
      <!--   <item name="android:textCursorDrawable">@null</item> -->
        <!-- <item name="android:textColorHighlight">@color/search_view_selected_text</item> -->
     </style>
     </resources>
    

    我定义了用于显示菜单的 custommenu.xml 文件:

      <menu xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:com.example.actionbartheme="http://schemas.android.com/apk/res-auto" >  
    
      <item android:id="@+id/search"
          android:title="@string/search_title"
          android:icon="@drawable/search_buttonn"
          com.example.actionbartheme:showAsAction="ifRoom|collapseActionView"
          com.example.actionbartheme:actionViewClass="android.support.v7.widget.SearchView"/>        
      </menu>
    

    您的活动应该扩展 ActionBarActivity 而不是 Activity。 这是 onCreateOptionsMenu 方法。

      @Override
      public boolean onCreateOptionsMenu(Menu menu) 
      {
          // Inflate the menu; this adds items to the action bar if it is present.
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.custommenu, menu);
      }
    

    在清单文件中:

      <application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppnewTheme" >
    

    欲了解更多信息,请参阅此网址:
    这里http://www.jayway.com/2014/06/02/android-theming-the-actionbar/

    【讨论】:

      【解决方案5】:

      我已经创建了 kotlin 扩展函数

      提示颜色:

      fun SearchView.setHintTextColor(@ColorInt color: Int) {
          findViewById<EditText>(R.id.search_src_text).setHintTextColor(color)
      }
      

      查询颜色:

      fun SearchView.setTextColor(@ColorInt color: Int) {
          findViewById<EditText>(R.id.search_src_text).setTextColor(color)
      }
      

      【讨论】:

        【解决方案6】:

        在 Appcompat v21 中更改 SearchView 的样式

        1) 创建一个可搜索的配置文件。该文件传统命名为 searchable.xml,必须保存在 res/xml/ 项目目录中。

        <?xml version="1.0" encoding="utf-8"?>
        <searchable xmlns:android="http://schemas.android.com/apk/res/android"
            android:label="@string/app_label"
            android:hint="@string/search_hint" >
        </searchable>
        

        2) 在 AndroidManifest.xml 中将元数据和属性添加到您的活动和操作以进行意图过滤

        <activity
                android:name=".activity.YourActivity"
                android:screenOrientation="portrait"
                android:theme="@style/Theme.App.Base">
                <meta-data android:name="android.app.searchable"
                           android:resource="@xml/searchable"/>
                <intent-filter>
                    <action android:name="android.intent.action.SEARCH" />
                    <category android:name="android.intent.category.DEFAULT"/>
                </intent-filter>
            </activity>
        

        3) 为指示器、图标、提示和文本颜色创建样式。

        <style name="ActionBarThemeOverlay" parent="">
                <item name="android:textColorPrimary">@color/action_bar_text</item>
                <item name="colorControlNormal">@color/action_bar_text</item>
                <item name="colorControlHighlight">@color/body_text_2</item>
            </style>
        
        <style name="Widget.App.SearchView" parent="Widget.AppCompat.Light.SearchView">
            <item name="closeIcon">@drawable/ic_action_cancel</item>
            <item name="voiceIcon">@drawable/ic_action_voice</item>
        </style>
        

        4) 将 Widget.App.Search 添加到 AndroidManifest 中添加的 BaseAppTheme 作为 Activity 样式

         <style name="Theme.App.Base" parent="Theme">
            name="searchViewStyle">@style/Widget.App.SearchView</item>
        </style>
        

        将 ActionBarThemeOverlay 添加到您的工具栏初始化

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_actionbar"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar_height"
            android:background="@color/color_primary"
            app:theme="@style/ActionBarThemeOverlay"
            app:popupTheme="@style/ActionBarPopupThemeOverlay"
            app:subtitleTextAppearance="@style/Theme.ActionBar.SubtitleTextStyle"
            app:titleTextAppearance="@style/Theme.ActionBar.TitleTextStyle"/>
        

        【讨论】:

        • 这是实现 SearchView 主题的一种很棒的方式。只需创建一个样式并将其应用到工具栏!像魅力一样工作!
        【解决方案7】:

        另一种解决方案:

         searchView_SA = (SearchView) findViewById(R.id.searcvViewSA);
         EditText searchEditText = (EditText)searchView_SA.findViewById(android.support.v7.appcompat.R.id.search_src_text);
            searchEditText.setTextColor(getResources().getColor(R.color.black));
            searchEditText.setHintTextColor(getResources().getColor(R.color.black));
        

        【讨论】:

          【解决方案8】:

          检查一下: Link

          这对我很有用:

          Android.Widget.SearchView searchView = (Android.Widget.SearchView)FindViewById(Resource.Id.TxtSearchMaterial);
          LinearLayout linearLayout1 = (LinearLayout)searchView.GetChildAt(0);
          LinearLayout linearLayout2 = (LinearLayout)linearLayout1.GetChildAt(2);
          LinearLayout linearLayout3 = (LinearLayout)linearLayout2.GetChildAt(1);
          AutoCompleteTextView autoComplete = (AutoCompleteTextView)linearLayout3.GetChildAt(0);
          autoComplete.SetTextColor(Color.White);
          

          【讨论】:

            猜你喜欢
            • 2021-08-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-06
            • 2017-07-08
            • 2016-01-10
            相关资源
            最近更新 更多