Styling a SearchView in Android Action Bar

后端 未结 11 1574
小鲜肉
小鲜肉 2020-12-02 20:56

I have a search widget in my Action Bar like this:

\"Search

(1) How do I change

相关标签:
11条回答
  • 2020-12-02 21:09

    You would have to set

    searchView.setBackgroundColor(Color.WHITE);
    

    SearchViews are not so customizable.

    0 讨论(0)
  • 2020-12-02 21:09

    Get SearchView in your xxxActivity.java file and then:

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

    Create file 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>
    
    0 讨论(0)
  • 2020-12-02 21:12

    As of Lollipop you can style search view like this from blog post 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>
    
    0 讨论(0)
  • 2020-12-02 21:14

    If you want white text, you should extend from an Action Bar theme that has a black background like Holo.Theme or Theme.AppCompat.Light.DarkActionBar if you're using the support library.
    No other settings necessary.

    0 讨论(0)
  • 2020-12-02 21:19
    @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);
    
        }
    

    Now this is the xml file searchcolor.xml for changing the plate color of search bar

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

    and menu/menu.xml for creating the search in action bar

    <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>
    
    0 讨论(0)
提交回复
热议问题