簡體   English   中英

操作欄上的搜索視圖未在Android中完全展開

[英]Search view on Action bar not expanding completely in Android

我將搜索視圖添加到操作欄,並始終以展開模式打開它。 我總是不知道為什么總是要從正確中獲取利潤。 我嘗試了許多解決方案,但仍然沒有成功。 請幫我解決這個問題。

這是菜單代碼

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

在此處輸入圖片說明

嘗試這個:

注釋此行,如下所示:

//searchView.setSubmitButtonEnabled(true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM