操作栏上的搜索视图未在Android中完全展开

时间:2017-07-12 07:28:37

标签: android margin searchview

我将搜索视图添加到操作栏并始终以展开模式打开它。它始终从右边获取利润,我不知道为什么。我尝试了很多解决方案,但仍未成功。请帮我解决这个问题。

这是菜单代码

 <?xml version="1.0" encoding="utf-8"?>
    <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="com.don.offers.activities.FunHandlerSearchActivity"
        >
        <item
            android:id="@+id/action_search"
            android:icon="@drawable/ic_magnify_grey600_24dp"
            app:showAsAction="collapseActionView|never"
            android:title="@string/search_title"
            app:actionViewClass="android.support.v7.widget.SearchView"
            android:imeOptions="actionSearch"
            />

    </menu>

可搜索的代码

 <?xml version="1.0" encoding="utf-8"?>

    <searchable
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/search_for_friend"
        android:imeOptions="actionSearch">
    </searchable>

活动代码

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.fun_handler_search_menu, menu);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.action_search).getActionView();

    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    searchMenuItem.expandActionView();

    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(this);

    try {
        ((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.action_bar_grey_color));
        ((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setTextColor(getResources().getColor(R.color.action_bar_text_grey_color));

    } catch (Resources.NotFoundException e) {
        e.printStackTrace();
    }

    try {
        ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
        searchClose.setImageResource(R.drawable.ic_clear_24dp);
    } catch (Exception e) {
        e.printStackTrace();
    }

    MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            finish();
            overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
            return true;
        }
    });
    return true;
}

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个:

评论此行如下:

//searchView.setSubmitButtonEnabled(true);