【问题标题】:Styling android search view and drop down list in actionbar在操作栏中设置 android 搜索视图和下拉列表的样式
【发布时间】:2015-01-05 23:16:53
【问题描述】:

在我的应用程序中,我有一个显示搜索建议的搜索视图。我希望下拉/微调器和文本是特定的颜色。目前我唯一能做的改变是通过以下方式输入文本的文本颜色:

   <style name="MyApp.AutoCompleteTextView" parent="Widget.AppCompat.Light.AutoCompleteTextView">
    <item name="android:textColor">@color/white</item>
    <item name="android:textCursorDrawable">@null</item>
    <item name="android:background">@color/myBackgroundColor</item>
  </style>

应用程序当前显示搜索视图结果如下:

我想知道的是如何更改搜索提示颜色、下拉背景和下拉项目文本颜色等部分?

我的目标是 API 19+

【问题讨论】:

    标签: android android-actionbar android-styles searchview android-toolbar


    【解决方案1】:

    在您的styles.xml 中,将以下内容设置为您的AppTheme

    <style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
        <item name="android:dropDownItemStyle">@style/myDropDownItemStyle</item>
    </style>
    
    <style name="myDropDownItemStyle" parent="Widget.AppCompat.DropDownItem.Spinner">
        <item name="android:textColor">@color/secondary_text_default_material_light</item>
    </style>
    

    这可以改变下拉项的文本颜色。试一试。

    【讨论】:

    • 没有 AppCompat 怎么样?
    • @Yar 将 myDropDownItemStyle 的父样式更改为非 AppCompat 样式。你的基本主题是什么?
    • 嗨。我父母的主要风格是 android:style/Theme.Holo.Light.DarkActionBar。现在我尝试使用样式 @style/MyDropDownItemTextAppearance@android:color/black 但它肯定行不通。而不是 android:style/Widget.Holo.DropDownItem 我尝试在其中使用 Spinner 来模仿你的风格,但它也不起作用。
    • 似乎 Theme.Holo.Light 与 AppCompat 不同。我在Theme.Holo.Light中找到了 @style/TextAppearance.Holo.Light.SearchResult.Title 。您可以查找主题源代码以找到解决方案。 @Yar
    【解决方案2】:

    所以经过大量搜索,我能找到的最接近的解决方案如下(感谢GZ95's answer!):

    SearchView searchView = new SearchView(context);
    LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
    LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
    LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
    AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);
    //Set the input text color
    autoComplete.setTextColor(Color.WHITE);
    // set the hint text color
    autoComplete.setHintTextColor(Color.WHITE);
    //Some drawable (e.g. from xml)
    autoComplete.setDropDownBackgroundResource(R.drawable.drop_down_bg);
    

    这不是最优雅的,但它对我有用。我唯一未解决的问题是如何更改建议的项目文本颜色。我很高兴听到这个问题的更多最佳解决方案

    【讨论】:

    • jedikv 在这里尝试更清洁的解决方案:SearchView.SearchAutoComplete autoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text); autoComplete.setDropDownBackgroundResource(R.drawable.drop_down_bg);
    • 这是唯一对我有用的解决方案。谢谢
    【解决方案3】:

    如果您使用的是 android.support.v7.widget.SearchView,您可以在 res/styles.xml 中定义以下样式。

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="autoCompleteTextViewStyle">@style/myAutoCompleteTextViewStyle</item>
        <item name="textAppearanceSearchResultTitle">@style/mySearchResult</item>
    </style>
    
    <style name="myAutoCompleteTextViewStyle" parent="Widget.AppCompat.Light.AutoCompleteTextView">
        <item name="android:popupBackground">#ffffff</item>
    </style>
    
    <style name="mySearchResult" parent="TextAppearance.AppCompat.SearchResult.Title">
        <item name="android:textColor">#000000</item>
    </style>
    

    如果你使用默认的 SearchView,属性应该是“android:autoCompleteTextViewStyle”和“android:textAppearanceSearchResultTitle”。我写了一个post 描述了这方面的细节。

    【讨论】:

    • 唯一适用于 Android 4.2 的解决方案。谢谢!
    • 为我工作,博客文章简洁明了,谢谢!
    【解决方案4】:

    通过样式,您可以更改下拉菜单的背景和项目文本颜色

    <!-- ToolBar -->
    <style name="ToolBarStyle" parent="Theme.AppCompat">
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:textColorSecondary">@android:color/white</item>
        <item name="actionMenuTextColor">@android:color/white</item>
        <item name="android:dropDownItemStyle">@style/myDropDownItemStyle</item>
        <item name="android:dropDownListViewStyle">@style/myDropDownListViewStyle</item>
    </style>
    
    
    <style name="myDropDownItemStyle" parent="Widget.AppCompat.DropDownItem.Spinner">
        <item name="android:textColor">@color/secondary_text_default_material_light</item>
        <item name="android:textSize">14dp</item>
    </style>
    
    <style name="myDropDownListViewStyle" parent="Widget.AppCompat.ListView.DropDown">
        <item name="android:background">#FFF</item>
    </style>
    

    【讨论】:

      【解决方案5】:

      你可以在themes.xml中定义suggestRowLayout:

      <resources>
      <style name="AppTheme" parent="AppTheme.Base">
          <item name="searchViewStyle">@style/AppTheme.SearchView</item>
      </style>
      
      <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="colorPrimary">@color/bluegrey_500</item>
          <item name="colorPrimaryDark">@color/bluegrey_900</item>
          <item name="colorAccent">@color/teal_500</item>
          <item name="android:windowNoTitle">true</item>
          <item name="windowActionBar">false</item>
      </style>
      
      <style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView">
          <!-- The layout for the search view. Be careful. -->
          <!--<item name="layout">...</item>-->
          <!-- Background for the search query section (e.g. EditText) -->
          <!--<item name="queryBackground">@color/bluegrey_400</item>-->
          <!-- Background for the actions section (e.g. voice, submit) -->
          <!--<item name="submitBackground">...</item>-->
          <!-- Close button icon -->
          <!--<item name="closeIcon">...</item>-->
          <!-- Search button icon -->
          <!--<item name="searchIcon">...</item>-->
          <!-- Go/commit button icon -->
          <!--<item name="goIcon">...</item>-->
           <!--Voice search button icon-->
          <!--<item name="voiceIcon">@drawable/abc_ic_voice_search_api_mtrl_alpha</item>-->
          <!-- Commit icon shown in the query suggestion row -->
          <!--<item name="commitIcon">...</item>-->
          <!-- Layout for query suggestion rows -->
          <item name="suggestionRowLayout">@layout/item_suggestion_place</item>
      </style>
      

      【讨论】:

        【解决方案6】:

        我遇到了同样的问题(白色背景建议中的白色文本),但所有答案都不适用于我。所以我终于解决了这个问题,将 TextViewtextColor 属性更改为 adapter,附加到 AutoCompleteTextView

        代码:

        AutoCompleteTextView autoComplete = ...;
        List<String> suggestions = Arrays.asList("a", "b", "c");
        ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.dropdown_item, suggestions);
        autoComplete.setAdapter(adapter);
        

        dropdown_item.xml:

        <?xml version="1.0" encoding="utf-8"?>
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:textColor="#000000"
              ...
        />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多