【问题标题】:Styling a SearchView in Android Action Bar在 Android 操作栏中设置 SearchView 的样式
【发布时间】:2012-07-16 16:15:51
【问题描述】:

我的操作栏中有一个搜索小部件,如下所示:

(1) 如何更改“iPhone”文字的颜色?

(2) 另外,如果您注意到灰色 X —— 整个搜索小部件在图标位置时也是该颜色。我正在使用 Holo.Theme.Light 并使用我自己的模组。

如何在我的 styles.xml 文件中更改小部件的这两种样式(假设您在其中对搜索小部件进行了更改)?

【问题讨论】:

  • 1. 在图标位置是什么意思? 2. 你真正想在 (2) 中改变什么? X的颜色?下划线颜色?两者都有?
  • 嘿,这是旧的,我解决了它 - 有点。我把答案放在下面。我主要想改变文本和 X 的颜色。在图标位置只是操作栏中的放大镜。你点击它,它就会像上面看到的那样打开。
  • stackoverflow.com/posts/12942258/edit 这是一个肮脏的黑客,但它有效
  • @KickingLettuce 你能告诉我你使用自定义操作栏布局吗?你如何在左侧显示图标图像?
  • 终于!!在 Xamarin.Android 上完美运行 @Pongpat 谢谢

标签: android search styles searchview


【解决方案1】:

我为此花费了很多时间,但最终::-)

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

【讨论】:

    【解决方案2】:

    如果您使用的是 appcompat 库,那么该解决方案与 Jerome 的回答有些不同。这是我的解决方案

    @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);
    }
    

    【讨论】:

    • 我们很懒惰,不喜欢点击链接。请在答案中包含解决方案,而不仅仅是链接。
    • 其中大部分来自 appcompat 中的 abc_search_view.xml 文件。希望有人对此有所帮助。
    • 正是我需要的。
    • 在哪里可以实际更改search_mag_icon?除了那个,我可以改变一切。
    【解决方案3】:

    您可以更改搜索视图的样式。这是我用来调整编辑文本、语音图标的一些代码...

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

    [编辑] 更完整的答案在这里:Changing the background drawable of the searchview widget

    【讨论】:

    • 我怎样才能找到这些ID?我想将放大镜图标和“x”更改为深色。
    • +1。这对我有帮助。但是,在 ABS 的情况下,您可以只使用 ID,而不是使用 getIdentifier。 searchView.findViewById(R.id.abs__search_plate).setBackgroundResource(R.drawable.myBackground);同样你可以使用提交区域和搜索src文本也
    【解决方案4】:

    你必须设置

    searchView.setBackgroundColor(Color.WHITE);
    

    SearchViews 不是那么可定制的。

    【讨论】:

    • 很有趣,需要试试这个。几周前我确实用一种解决方法解决了它......但这很高兴知道。点赞。
    【解决方案5】:

    从 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”>
        <item name=”searchViewStyle”>@style/MySearchViewStyle</item>
    </style>
    <style name=”MySearchViewStyle” parent=”Widget.AppCompat.SearchView”>
        <!-- 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>
    

    【讨论】:

    • 有没有办法在棒棒糖前使用搜索视图的样式?比如像 KitKat?
    • 它是 AppCompat - 确实适用于 Pre-Lollipop 设备。
    【解决方案6】:
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search));
    searchView.setSubmitButtonEnabled(true); // to enable submit button
    
    ImageView b = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_go_btn);
    b.setImageResource(android.R.drawable.ic_menu_search); //to change submit button icon
    
    TextView a=(TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    a.setTextColor(...); //to change color of search text
    

    【讨论】:

      【解决方案7】:

      我解决这个问题的方法是使用 Holo.Light.DarkActionBar 主题。我不相信您可以轻松更改搜索小部件。

      【讨论】:

      • 其实可以在SearchView中设置EditText的文字颜色。但是,正如您所提到的,它不像使用 XML 样式定义那么容易。您甚至可以设置下划线的颜色。见my answer 到其他关于SearchView 定制。
      • 我认为这是最好的方法。
      【解决方案8】:

      onCreateOptionsMenu :

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

      【讨论】:

        【解决方案9】:

        如果您想要白色文本,您应该从具有黑色背景的操作栏主题扩展,例如 Holo.ThemeTheme.AppCompat.Light.DarkActionBar(如果您正在使用支持库)。
        无需其他设置。

        【讨论】:

          【解决方案10】:
          @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>
          

          【讨论】:

            【解决方案11】:

            在您的 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>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-03-17
              • 2013-09-20
              • 2012-03-01
              • 2014-04-16
              • 1970-01-01
              • 2012-12-09
              相关资源
              最近更新 更多