134

SearchView 要素には、テキストの色を変更するためのプロパティはありません。デフォルトのテキストの色は黒で、暗い背景では機能しません。ハックに頼らずにテキストの色を変更する方法はありますか?

テキストサイズの変更に関連するこの同様の質問を見つけましたが、これまでのところ、回答がありません: SearchView TextSize を設定するにはどうすればよいですか?

4

36 に答える 36

134

追加

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

親テーマに変更すると、入力したテキストが変更されます。使用することもできます

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

SearchView のヒント テキストの色を変更します。@android:color/white(使用したい適切な値に置き換えることができることに注意してください)

于 2014-11-12T21:35:03.937 に答える
104

次のようなことを試してください: SDK から textview へのハンドルを取得し、それを公開していないため変更します。

int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);
于 2013-01-16T17:29:49.513 に答える
29

@CzarMatt のおかげで、AndroidXのサポートを追加しました。

私にとっては、次の作品です。リンクからのコードを使用しました:サポート ライブラリを使用してアクションバーの検索ヒントのテキストの色を変更します

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

    EditText txtSearch = ((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text));
    txtSearch.setHint(getResources().getString(R.string.search_hint));
    txtSearch.setHintTextColor(Color.LTGRAY);
    txtSearch.setTextColor(Color.WHITE);

アクションバーの検索ビューのヒントテキストの色を変更すると、別の解決策がアドバイスされます。機能しますが、ヒントのテキストと色のみを設定します。

    searchView.setQueryHint(Html.fromHtml("<font color = #ffffff>" + getResources().getString(R.string.search_hint) + "</font>"));
于 2014-10-08T07:18:44.340 に答える
27

私は似たようなことをしたかった。私はついに子供たちのTextView中から見つけなければなりませんでしたSearchView

for (TextView textView : findChildrenByClass(searchView, TextView.class)) {
    textView.setTextColor(Color.WHITE);
}

util メソッドが必要な場合:

public static <V extends View> Collection<V> findChildrenByClass(ViewGroup viewGroup, Class<V> clazz) {

    return gatherChildrenByClass(viewGroup, clazz, new ArrayList<V>());
}

private static <V extends View> Collection<V> gatherChildrenByClass(ViewGroup viewGroup, Class<V> clazz, Collection<V> childrenFound) {

    for (int i = 0; i < viewGroup.getChildCount(); i++)
    {
        final View child = viewGroup.getChildAt(i);
        if (clazz.isAssignableFrom(child.getClass())) {
            childrenFound.add((V)child);
        }
        if (child instanceof ViewGroup) {
            gatherChildrenByClass((ViewGroup) child, clazz, childrenFound);
        }
    }

    return childrenFound;
}
于 2014-05-07T15:54:50.063 に答える
16

これは、カスタム スタイルを使用して行うのが最適です。独自のカスタム スタイルでアクション バー ウィジェット スタイルをオーバーロードします。暗いアクション バーを持つホロ ライトの場合は、これを次のような独自のスタイル ファイルに入れます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>

「ここにリンクの説明を入力してください」で説明されているように、アプリケーションがテーマ カスタム テーマを使用していることを確認してください

于 2014-04-20T16:15:50.223 に答える
15

editTextColorこれを行うには、スタイルで属性を設定します。

<style name="SearchViewStyle" parent="Some.Relevant.Parent">
    <item name="android:editTextColor">@color/some_color</item>
</style>

このスタイルをレイアウトにToolbarまたは に適用SearchViewします。

<android.support.v7.widget.Toolbar
    android:theme="@style/SearchViewStyle">

    <android.support.v7.widget.SearchView />

</android.support.v7.widget.Toolbar>
于 2016-10-14T01:18:43.930 に答える
14

android:themeビューの属性を使用して、デフォルトの色をオーバーライドできます。

Toolbarまたはを使用している場合MaterialToolbar:

<com.google.android.material.appbar.MaterialToolbar
    android:theme="@style/ThemeOverlay.Toobar"
    ...>

また:

<androidx.appcompat.widget.Toolbar
    android:theme="@style/ThemeOverlay.Toobar"
    ...>

マテリアル コンポーネント テーマのテーマ オーバーレイは次のとおりです。

<style name="ThemeOverlay.Toobar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <!-- Text color -->
    <item name="android:editTextColor">@color/....</item>
    <!-- Hint text color -->
    <item name="android:textColorHint">@color/...</item>
</style>

AppCompat テーマの場合:

<style name="ThemeOverlay.Toobar" parent="ThemeOverlay.AppCompat.Light.*">
    <!-- Text color -->
    <item name="android:editTextColor">@color/....</item>
    <!-- Hint text color -->
    <item name="android:textColorHint">@color/...</item>
</style>

ここに画像の説明を入力


レイアウトでa を使用している場合は、SearchView同様のことができます。

   <androidx.appcompat.widget.SearchView
       android:theme="@style/ThemeOverlay.SearchView"

   <style name="ThemeOverlay.SearchView" parent="">
        <!-- Text color -->
        <item name="android:editTextColor">@color/...</item>
        <!-- Hint text color -->
        <item name="android:textColorHint">@color/...</item>
    </style>

ここに画像の説明を入力

于 2020-07-18T08:31:18.393 に答える
6

If you're using the android.support.v7.widget.SearchView, it's possible without having to use reflection.

Here's how I'm doing it in my app:

EditText text = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
ImageView searchCloseIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);

if (searchPlate != null) {
    searchPlate.setBackgroundResource(R.drawable.search_background);
}

if (text != null){
    text.setTextColor(resources.getColor(R.color.white));
    text.setHintTextColor(getResources().getColor(R.color.white));

    SpannableStringBuilder magHint = new SpannableStringBuilder("  ");
    magHint.append(resources.getString(R.string.search));

    Drawable searchIcon = getResources().getDrawable(R.drawable.ic_action_view_search);
    int textSize = (int) (text.getTextSize() * 1.5);
    searchIcon.setBounds(0, 0, textSize, textSize);
    magHint.setSpan(new ImageSpan(searchIcon), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Set the new hint text
    text.setHint(magHint);

}

if (searchCloseIcon != null){
    searchCloseIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_close));
}

They don't expose the ids publicly for the non appcompat SearchView, but they do for AppCompat if you know where to look. :)

于 2014-12-16T05:21:18.160 に答える
5

私はこの問題を抱えていましたが、これは私にとってはうまくいきます。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.customer_menu, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView       = (SearchView) menu.findItem(R.id.menu_customer_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        searchView.setOnQueryTextListener(this);

        //Applies white color on searchview text
        int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) searchView.findViewById(id);
        textView.setTextColor(Color.WHITE);

        return true;
}
于 2013-03-01T14:41:28.970 に答える
4

ホロ テーマに基づいてアプリのテーマを作成すると、SearchView に黒のテキストではなく白のテキストが表示されます

<style name="Theme.MyTheme" parent="android:Theme.Holo">

汚いハックを使用せずに検索ビューのテキストカラーを変更する他の方法は見つかりませんでした。

于 2012-10-17T09:21:21.350 に答える
4

はい、以下の方法で可能です。

public static EditText setHintEditText(EditText argEditText, String argHintMessage, boolean argIsRequire) {
    try {
        if (argIsRequire) {
            argHintMessage = "   " + argHintMessage;
            //String text = "<font color=#8c8c8c>"+argHintMessage+"</font> <font color=#cc0029>*</font>";
            String text = "<font color=#8c8c8c>" + argHintMessage + "</font>";
            argEditText.setHint(Html.fromHtml(text));
        } else {
            argEditText.setHint(argHintMessage);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return argEditText;
}

このメソッドの呼び出しは次のようになります。

metLoginUserName=(EditText)this.findViewById(R.id.etLoginUserName);
    metLoginPassword=(EditText)this.findViewById(R.id.etLoginPassword);

    /**Set the hint in username and password edittext*/
    metLoginUserName=HotSpotStaticMethod.setHintEditText(metLoginUserName, getString(R.string.hint_username),true);
    metLoginPassword=HotSpotStaticMethod.setHintEditText(metLoginPassword, getString(R.string.hint_password),true);

それを使用して、このメソッドを使用してヒントに赤色の * マークを正常に追加しました。要件に応じて、このメソッドを変更する必要があります。お役に立てば幸いです....:)

于 2012-12-31T08:23:21.883 に答える
4

最もクリーンな方法は次のとおりです。

ツールバーはテーマ ThemeOverlay.AppCompat.Dark.Actionbar を使用します。

次のように子を作成します。

toolbarStyle 親 "ThemeOverlay.AppCompat.Dark.Actionbar"

このアイテムをそのスタイルに追加

アイテム名「android:editTextColor">yourcolor」

終わり。

もう 1 つの非常に重要なことは、ツールバーに layout_height ="?attr/actionbarSize" を配置することです。デフォルトではwrap_contentです。私にとっては、検索ビューにテキストさえ表示されなかったので、その問題が修正されました。

于 2017-08-07T11:37:12.087 に答える
3

これは私のために働いています。

final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);

searchView.setOnQueryTextListener(this);   
searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setTextColor(getResources().getColor(R.color.white));
searchEditText.setHintTextColor(getResources().getColor(R.color.white));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) 
{
    searchEditText.setBackgroundColor(getResources().getColor(R.color.c_trasnparent));
    searchEditText.setGravity(Gravity.CENTER);
    searchEditText.setCompoundDrawables(null,null,R.drawable.ic_cross,null);
}
于 2016-08-26T07:31:38.747 に答える
3

これを使ってください、そうです。:D

AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(R.id.abs__search_src_text);
searchText.setHintTextColor(getResources().getColor(color.black));
searchText.setTextColor(getResources().getColor(color.black));
于 2013-01-28T10:27:15.900 に答える
2
TextView textView = (TextView) searchView.findViewById(R.id.search_src_text);
textView.setTextColor(Color.BLACK);
于 2014-03-07T22:51:42.410 に答える
1

appcompat v7 ライブラリを使用して、searchview をカスタマイズすることができます。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>

アクティビティは、Activity ではなく ActionBarActivity を拡張する必要があります。これが 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" >

詳細については、この URL を参照してください
: http://www.jayway.com/2014/06/02/android-theming-the-actionbar/

于 2014-09-22T10:57:38.053 に答える
1

searchView を使用した appcompat-v7 ツールバーの場合 (MenuItemCompat 経由で提供):

ツールバーのテーマを @style/ThemeOverlay.AppCompat.Light に設定すると、ヒント テキストと入力されたテキストに暗い色 (黒) が生成されますが、カーソル* の色には影響しません。したがって、ツールバーのテーマを @style/ThemeOverlay.AppCompat.Dark に設定すると、ヒント テキストと入力されたテキストが明るい色 (白) になり、カーソル* はとにかく白になります。

上記のテーマのカスタマイズ:

android:textColorPrimary --> 入力したテキストの色

editTextColor --> 入力されたテキストの色 (設定されている場合、android:textColorPrimary の影響をオーバーライドします)

android:textColorHint --> ヒントの色

*注: カーソルの色を制御する方法をまだ決定していません (反射ソリューションを使用せずに)。

于 2015-01-23T01:31:25.890 に答える
1

これを使用することで、検索ビューで入力した色のテキストを変更できました

AutoCompleteTextView typed_text = (AutoCompleteTextView) inputSearch.findViewById(inputSearch.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
typed_text.setTextColor(Color.WHITE);
于 2016-05-22T15:31:46.050 に答える
1

SearchViewオブジェクトは から拡張されるためLinearLayout、他のビューを保持します。秘訣は、ヒント テキストを保持しているビューを見つけて、プログラムで色を変更することです。ID でビューを見つけようとする際の問題は、ID がアプリケーションで使用されているテーマに依存していることです。そのため、使用するテーマによっては、findViewById(int id)メソッドが を返す場合がありnullます。すべてのテーマで機能するより良いアプローチは、ビュー階層をトラバースし、ヒント テキストを含むウィジェットを見つけることです。

// 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など、階層内の他のウィジェットの外観を変更することもできます。EditTextGoogle がSearchViewまもなくビュー階層を変更することを決定しない限り、しばらくの間はこの方法でウィジェットの外観を変更できるはずです。

于 2014-03-11T04:13:17.710 に答える
0

それは私と一緒に動作します

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem searchItem = menu.findItem(R.id.action_search);
    EditText searchEditText = (EditText) searchView.getActionView().findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchEditText.setTextColor(getResources().getColor(R.color.white));
    searchEditText.setHintTextColor(getResources().getColor(R.color.white));
    return super.onPrepareOptionsMenu(menu);
}
于 2016-04-19T13:47:01.800 に答える
0

ブログ投稿から解決策を見つけました。ここを参照してください。

基本的に、searchViewAutoCompleteTextView のスタイルを設定し、android:actionBarWidgetTheme にスタイルを継承させます。

于 2014-10-05T15:38:27.297 に答える
0

Kotlin 言語の場合

    searchView = view.findViewById(R.id.searchView_Contacts)
    // change color
    val id = searchView.context.resources
        .getIdentifier("android:id/search_src_text", null, null)

    val textView = searchView.findViewById<View>(id) as TextView

    textView.setTextColor(Color.WHITE)
于 2020-05-05T12:34:04.997 に答える
0
searchView = (SearchView) view.findViewById(R.id.searchView);

SearchView.SearchAutoComplete searchText = (SearchView.SearchAutoComplete) searchView
      .findViewById(org.holoeverywhere.R.id.search_src_text);
searchText.setTextColor(Color.BLACK);

Holoeverywhere ライブラリを使用しています。org.holoeverywhere.R.id.search_src_textに注意してください

于 2014-03-01T07:44:52.197 に答える
0

何が私のために働いた

((EditText)searchView.findViewById(R.id.search_src_text)).setTextColor(getResources().getColor(R.color.text));
于 2018-08-02T07:36:19.920 に答える
-2

こちらです :)

View searchPlate = searchView.findViewById(searchPlateId);
if (searchPlate!=null) {
    searchPlate.setBackgroundColor(Color.DKGRAY);
    int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
    if (searchText != null) {
        searchText.setTextColor(Color.WHITE);
        searchText.setHintTextColor(Color.WHITE);
    }
}
return ;
}
于 2018-03-10T07:23:56.457 に答える
-2

私はこれに対する1つの解決策を見つけようとしました。私はそれがあなたを助けると思う..

searchView.setBackgroundColor(Color.WHITE);

ここに画像の説明を入力

于 2012-08-15T07:38:14.510 に答える
-3

SearchViewTextViewにキャストすると、 TextViewのメソッドを使用して色を変更できます。

((TextView) searchView).setTextColor(Color.WHITE);
((TextView) searchView).setHintTextColor(Color.WHITE);
于 2015-07-07T09:47:58.863 に答える