如何更改SearchView元素的颜色?

问题描述 投票:12回答:7

我想在我的项目中使用SearchView,但我的元素颜色有问题。让我告诉你:它的fragmentDialog,我有我的SearchView You can see what I mean here

我不需要改变bg颜色。下一张图片就是例如。我想看看没有黑色bg的SearchView元素。 The color of the elements is white

我试过了

  • 改变主题
  • 改变风格
  • 改变色彩

但没有任何工作。搜索图标和anoter元素仍然是白色的。也许我失去了什么?我可以用XML做到这一点吗?请帮我。

android android-theme android-styles android-search
7个回答
21
投票

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

在您的搜索视图中尝试使用此样式,

在扩展searchview ---> enter image description here之前

扩展searchview后--->

enter image description here

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

31
投票

在这个答案我假设,SearchView被添加到ToolbarAppThemeTheme.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" />

5
投票
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);

4
投票

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

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

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

1
投票

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

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


1
投票

支持库附带的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=""
    >

0
投票

您可以在`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" />
© www.soinside.com 2019 - 2024. All rights reserved.