Android操作栏SearchView更改图标并删除额外的间距

时间:2016-10-19 07:15:04

标签: android android-actionbar searchview

我正在实施SearchView。我已成功添加搜索视图及其所有功能。现在我想让它看起来很棒。

我需要做两件事我尝试了所有事情,但没有任何对我有用:

  1. 我想更改搜索图标。
  2. 我想删除searchView左侧的额外间距。
  3. 这是我的searchView目前的样子:

    enter image description here

    这就是我想做的事情

    enter image description here

    菜单/ menu_search_action.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/location_icon"
        android:orderInCategory="100"
        android:title="@string/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always|collapseActionView" />
    

    SearchCategoriesActivity.java

        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_search_action, menu);
    
    
        MenuItem searchItem = menu.findItem(R.id.action_search);
        searchItem.expandActionView();
        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
    
                return true;
            }
    
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
    
                //DO SOMETHING WHEN THE SEARCHVIEW IS CLOSING
                back();
                return false;
            }
        });
    
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) MenuItemCompat
                .getActionView(searchItem);
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager
                    .getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                        @Override
                        public boolean onQueryTextSubmit(String query) {
                            callSearch(query);
                            return true;
                        }
    
                        @Override
                        public boolean onQueryTextChange(String newText) {
                            callSearch(newText);
                            return true;
                        }
                    });
        }
    
        for (TextView textView : findChildrenByClass(searchView, TextView.class)) {
            textView.setTextColor(Color.WHITE);
        }
        return super.onCreateOptionsMenu(menu);
    
    }
    
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                back();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    public void onBackPressed() {
        back();
    }
    
    private void back() {
        finish();
    }
    
    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;
    }
    

3 个答案:

答案 0 :(得分:4)

如下所示添加单独的<style name="AppTheme.SearchView" parent="Widget.AppCompat.SearchView"> <item name="searchIcon">@mipmap/ic_search_white</item> <item name="closeIcon">@mipmap/ic_clear_white</item> </style> 以更改搜索图标。

Base Theme

<!-- Base application theme. --> <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="searchViewStyle">@style/AppTheme.SearchView</item> </style>

return PostVote.aggregate([
   { "$match": { "post": { "$in": postIds } } },
   { 
        "$group": {
            "_id": "$post",
            "votes": { "$sum": "$vote" },                 
            "userVotes": {
                "$sum": {
                    "$cond": [
                        { "$eq": ["$user", userId] },
                        "$vote",
                        0
                    ]
                }
            }
        }
    }
 ]).execAsync().then(function(votes){
    return votes;
});

答案 1 :(得分:1)

navigationBackButton和SearchView之间的多余空间可以通过添加来删除 app:contentInsetStartWithNavigation="0dp"在您的工具栏中。

<androidx.appcompat.widget.Toolbar 
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:contentInsetStartWithNavigation="0dp" />

答案 2 :(得分:0)

通过编程将MaxWidth设置为onCreateOptionsMenu()中的SearchView

@override 
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_search, menu);
    SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
    searchView.setMaxWidth(Integer.MAX_VALUE);

针对使用Kotlin的人

searchview.maxWidth = Integer.MAX_VALUE