【问题标题】:How to change SearchView elements' color?如何更改 SearchView 元素的颜色?
【发布时间】:2016-11-24 12:15:34
【问题描述】:

我想在我的项目中使用 SearchView,但是元素的颜色有问题。让我告诉你:它的 fragmentDialog 我有我的 SearchView

我不需要更改背景颜色。例如下一张图。我想看到没有黑色背景的 SearchView 元素。

我试过了

  • 更改主题
  • 改变风格
  • 改变色调

但没有任何效果。搜索图标和其他元素仍然是白色的。也许我正在失去一些东西?我可以在 XML 中执行此操作吗?请帮帮我。

【问题讨论】:

  • 是否要动态设置图标和背景颜色????
  • 不,我不知道。我想用 XML 设置它,如果可能的话,不要再碰它了

标签: android android-theme android-styles android-search


【解决方案1】:

在这个答案中,我假设SearchView 被添加到Toolbar 中,而AppThemeTheme.AppCompat.Light.NoActionBar 的孩子

<style name="AppTheme.Toolbar" parent="AppTheme">
    <!--This line changes the color of text in Toolbar-->
    <item name="android:textColorPrimary">@color/green</item>
    <!--This line changes the color of icons in toolbar (back, overflow menu icons)-->
    <item name="android:textColorSecondary">@color/green</item>
</style>

现在使用AppTheme.Toolbar 作为工具栏的主题。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:theme="@style/AppTheme.Toolbar" />

【讨论】:

  • 但是如果我使用 SearchView 不在工具栏中呢?会成功吗?
  • 如果SearchViewToolbar 中显示的菜单项,它会起作用。
  • 谢谢兄弟。但我尝试以编程方式但没有工作
  • 经过这么长时间的研究,这应该是公认的答案!感谢您的解决方案!
【解决方案2】:

你需要使用android.support.v7.widget.SearchView

在你的搜索视图中尝试这种风格,

在展开搜索视图之前 --->

展开搜索视图后--->

<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/search_view"
            style="@style/SearchViewStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_gravity="end" />

<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
    <!-- Gets rid of the search icon -->
    <item name="searchIcon">@drawable/ic_search</item>
    <!-- Gets rid of the "underline" in the text -->
    <item name="queryBackground">@color/white</item>
    <!-- Gets rid of the search icon when the SearchView is expanded -->
    <item name="searchHintIcon">@null</item>
    <!-- The hint text that appears when the user has not typed anything -->
    <item name="queryHint">@string/search_hint</item>
</style>

【讨论】:

  • 我认为你没有使用 android.support.v7.widget.SearchView
  • 是的,你是对的。 A 在您回复前 2 小时发现了它:D stackoverflow.com/a/40791256/7138308
  • 文本颜色最终变为白色。
【解决方案3】:

您可以在 `styles.xml' 中使用服装样式修改搜索视图:

<style name="AppSearchView" parent="Widget.AppCompat.SearchView" >
    <!-- Text Size -->
    <item name="android:textSize">@dimen/textSizePrimary</item>
    <!-- Query Text Color -->
    <item name="android:textColorPrimary">@color/textColorPrimary</item>
    <!-- Hint Color -->
    <item name="android:textColorHint">@color/textColorDisabled</item>
    <!-- Icon Color -->
    <item name="android:tint">@color/textColorPrimary</item>
</style>

然后在您的搜索视图中使用此样式:

<android.support.v7.widget.SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...
    app:theme="@style/AppSearchView" />

【讨论】:

    【解决方案4】:
    int searchiconId = view.getContext().getResources().getIdentifier("android:id/search_button",null,null);
    ImageView imgView = (ImageView)findViewById(searchiconId);
    Drawable whiteIcon = img.getDrawable();
    whiteIcon.setTint(Color.RED); //Whatever color you want it to be
    img.setImageDrawable(whiteIcon);
    

    【讨论】:

      【解决方案5】:

      尝试将这些线条添加到样式中。

      android:textColorPrimary 更改用户在 EditText 中输入的文本。 android:textColorSecondary 更改后退箭头,提示前的“删除文本”十字和小搜索图标。

      <item name="android:textColorPrimary">@color/white</item>
      <item name="android:textColorSecondary">@color/white</item>
      

      【讨论】:

        【解决方案6】:

        支持库自带的Searchview具有更换图标的功能

        <androidx.appcompat.widget.SearchView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:searchIcon="@drawable/ic_search"
                    app:closeIcon="@drawable/ic_close_black"
                    app:commitIcon=""
                    app:goIcon=""
                    app:searchHintIcon=""
                    app:voiceIcon=""
            >
        

        【讨论】:

          【解决方案7】:

          那么,我的问题是什么。我使用了 android.widget.SearchView 而不是 android.support.v7.widget.SearchView。我更改了 SearchView,之后提出的解决方案开始起作用。

          如果有人可以解释为什么样式和不同的东西不起作用或者可以给我一个链接,我将不胜感激。

          【讨论】:

            【解决方案8】:

            如果您想在搜索视图中更改放大镜的颜色,这将对您有所帮助。 如果你使用 Material 主题而不是这个

             ImageView icon = album_search.findViewById(com.google.android.material.R.id.search_button);
                    Drawable whiteIcon = icon.getDrawable();
                    whiteIcon.setTint(Color.BLACK); //Whatever color you want it to be
                    icon.setImageDrawable(whiteIcon);
            

            如果 Appcompact 主题不仅仅是将 com.google.android.material.R.id.search_button 更改为 android.support.v7.appcompat.R.id.search_button

            就是这样

            【讨论】:

              【解决方案9】:

              在这个答案中,我更改了搜索图标、搜索提示图标、关闭图标、盘子、查询提示颜色、查询颜色的颜色

              view!!.findViewById<EditText>(
                          searchView.context.resources.getIdentifier(
                              "android:id/search_src_text",
                              null,
                              null
                          )
                      ).apply {
                          setTextColor(ContextCompat.getColor(context!!, R.color.darkThemeIconColor))
                          setHintTextColor(ContextCompat.getColor(context!!, R.color.colorAccent))
                      }
              
                      view!!.findViewById<ImageView>(
                          searchView.context.resources.getIdentifier(
                              "android:id/search_button",
                              null, null
                          )
                      ).imageTintList = ColorStateList.valueOf(
                          ContextCompat.getColor(context!!, R.color.darkThemeIconColor)
                      )
                      val mDrawable = SearchView::class.java.getDeclaredField("mSearchHintIcon")
                      mDrawable.isAccessible = true
                      val drawable = mDrawable.get(searchView) as Drawable
                      drawable.colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
                          ContextCompat.getColor(context!!, R.color.darkThemeIconColor),
                          BlendModeCompat.SRC_ATOP
                      )
                      view!!.findViewById<ImageView>(
                          searchView.context.resources.getIdentifier(
                              "android:id/search_close_btn",
                              null, null
                          )
                      ).imageTintList = ColorStateList.valueOf(
                          ContextCompat.getColor(context!!, R.color.darkThemeIconColor)
                      )
                      view!!.findViewById<View>(
                          searchView.context.resources.getIdentifier(
                              "android:id/search_plate",
                              null, null
                          )
                      ).backgroundTintList = ColorStateList.valueOf(
                          ContextCompat.getColor(context!!, R.color.darkThemeIconColor)
                      )
              

              【讨论】:

                猜你喜欢
                • 2016-09-06
                • 2016-01-22
                • 1970-01-01
                • 1970-01-01
                • 2020-10-31
                • 1970-01-01
                • 2019-07-05
                • 1970-01-01
                相关资源
                最近更新 更多