35

アクションバーに次のような検索ウィジェットがあります。

アクションバーの検索ウィジェット

(1) 「iPhone」というテキストの色を変更するにはどうすればよいですか?

(2) また、灰色の X に気付いた場合は、アイコンの位置にある場合、検索ウィジェット全体もその色になります。私は Holo.Theme.Light を使用しており、独自の mod を使用しています。

ウィジェットのこれら 2 つのスタイルを、styles.xml ファイルで変更するにはどうすればよいですか (ここで検索ウィジェットを変更すると仮定します)。

4

11 に答える 11

50

私はこれに多くの時間を費やしてきましたが、ついに: :-)

  1. テキストの色を変更するには:
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE);

またはこれはAndroidX用です:

((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setTextColor(Color.WHITE);
  1. テキストヒントを変更するには:
((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE);

またはこれはAndroidX用です:

((EditText)searchView.findViewById(androidx.appcompat.R.id.search_src_text)).setHintTextColor(Color.WHITE);
于 2014-02-04T10:15:40.477 に答える
15

appcompat ライブラリを使用している場合、解決策はジェロームの答えとは少し異なります。これが私の解決策です

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.main_menu, menu);
    restoreActionBar();

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchAutoComplete.setHintTextColor(Color.WHITE);
    searchAutoComplete.setTextColor(Color.WHITE);

    View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    searchplate.setBackgroundResource(R.drawable.texfield_searchview_holo_light);

    ImageView searchCloseIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchCloseIcon.setImageResource(R.drawable.clear_search);

    ImageView voiceIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn);
    voiceIcon.setImageResource(R.drawable.abc_ic_voice_search);

    ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
    searchIcon.setImageResource(R.drawable.abc_ic_search);


    return super.onCreateOptionsMenu(menu);
}
于 2014-01-22T18:36:13.047 に答える
13

検索ビューのスタイルを変更できます。これは、編集テキスト、音声アイコンを微調整するために使用したコードです...

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
        searchView.findViewById(searchPlateId).setBackgroundResource(R.drawable.textfield_search_selected);

        int voiceSearchPlateId = searchView.getContext().getResources().getIdentifier("android:id/submit_area", null, null);
        searchView.findViewById(voiceSearchPlateId).setBackgroundResource(R.drawable.textfield_search_right_selected);

        // change hint color
        int searchTextViewId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView searchTextView = (TextView) searchView.findViewById(searchTextViewId);
        searchTextView.setHintTextColor(getResources().getColor(R.color.light_grey));

[編集] より完全な回答がここにあります:検索ビュー ウィジェットの背景ドローアブルの変更

于 2013-06-24T21:27:36.710 に答える
9

設定する必要があります

searchView.setBackgroundColor(Color.WHITE);

SearchViewsはそれほどカスタマイズ可能ではありません。

于 2012-12-17T18:54:04.793 に答える
9

Lollipop の時点で、ブログ投稿http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.htmlからこのような検索ビューのスタイルを設定できます

values/themes.xml:
<style name=”Theme.MyTheme” parent=”Theme.AppCompat”&gt;
    <item name=”searchViewStyle”&gt;@style/MySearchViewStyle</item>
</style>
<style name=”MySearchViewStyle” parent=”Widget.AppCompat.SearchView”&gt;
    <!-- Background for the search query section (e.g. EditText) -->
    <item name="queryBackground">...</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">...</item>
    <!-- Commit icon shown in the query suggestion row -->
    <item name="commitIcon">...</item>
    <!-- Layout for query suggestion rows -->
    <item name="suggestionRowLayout">...</item>
</style>
于 2015-09-15T07:13:55.417 に答える
3

これを解決する方法は、Holo.Light.DarkActionBar テーマを使用することでした。そうでなければ、検索ウィジェットを簡単に変更できるとは思いません。

于 2012-07-26T16:02:45.167 に答える
2

onCreateOptionsMenu :

int id = searchView.getContext()
                   .getResources()
                   .getIdentifier("android:id/search_src_text", null, null);
    TextView textView = (TextView) searchView.findViewById(id);
    textView.setTextColor(Color.WHITE);
于 2014-10-31T19:35:44.160 に答える
0
@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        super.onCreateOptionsMenu(menu);

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        // Associate search configuration with the SearchView
        SearchManager searchManager =
               (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setQueryHint("Client/DOB/PPSN");
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));

        // Setting the textview default behaviour properties
        int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        TextView textView = (TextView) searchView.findViewById(id);
        textView.setTextColor(Color.WHITE);
        textView.setHintTextColor(Color.WHITE);

        // Set the search plate color to white
        int linlayId = getResources().getIdentifier("android:id/search_plate", null, null);
        View view = searchView.findViewById(linlayId);
        Drawable drawColor = getResources().getDrawable(R.drawable.searchcolor);
        view.setBackground( drawColor );
        return super.onCreateOptionsMenu(menu);

    }

これは、検索バーのプレートの色を変更するための xml ファイル searchcolor.xml です。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item>
      <shape >
          <solid android:color="#ffffff" />
      </shape>
  </item>

  <!-- main color -->
  <item android:bottom="1.5dp"
      android:left="1.5dp"
      android:right="1.5dp">
      <shape >
          <solid android:color="#2c4d8e" />
      </shape>
  </item>

  <!-- draw another block to cut-off the left and right bars -->
  <item android:bottom="10.0dp">
      <shape >
          <solid android:color="#2c4d8e" />
      </shape>
  </item>
</layer-list>

アクションバーで検索を作成するための menu/menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/search"
          android:icon="@drawable/search"
          android:showAsAction="ifRoom"
          android:actionViewClass="android.widget.SearchView" />

</menu>
于 2014-12-30T14:44:10.983 に答える
0

xxxActivity.java ファイルで SearchView を取得してから、次の操作を行います。

SearchView searchView = (SearchView)menu.findItem(R.id.my_search_view).getActionView();
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);

// Getting the 'search_plate' LinearLayout.
View searchPlate = searchView.findViewById(searchPlateId);
searchPlate.setBackgroundResource(R.drawable.textfield_searchview);

ファイルを作成res/drawable/texfield_searchview.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"
        android:drawable="@drawable/textfield_search_selected_holo_light" />
    <item android:drawable="@drawable/textfield_search_default_holo_light" />
</selector>
于 2015-12-09T14:23:15.917 に答える