“搜索视图”关闭按钮被隐藏



当在android的默认SearchView中,我将iconifide设置为false时,SearchView的关闭按钮将被隐藏。

要求:Activity启动时,默认情况下SearchView应打开,关闭按钮应可见;

 searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultActivity.class)));
 searchView.setIconifiedByDefault(false);

实现的第一种方法

    SearchView searchView = (SearchView)  
    menu.findItem(R.id.action_search).getActionView();
    searchView.setIconifiedByDefault(true);
    searchView.setFocusable(true);
    searchView.setIconified(false);
    searchView.requestFocusFromTouch();

或者还有另一种方法

     @Override
            public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            super.onCreateOptionsMenu(menu, inflater);
            inflater.inflate(R.menu.search, menu);
            MenuItem searchItem = menu.findItem(R.id.action_search);
            mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
            try {
                Field searchField = SearchView.class.getDeclaredField("mCloseButton");
                searchField.setAccessible(true);
                mSearchCloseButton = (ImageView) searchField.get(mSearchView);
             if (mSearchCloseButton != null) {
                 mSearchCloseButton.setEnabled(false);//  mSearchCloseButton.setEnabled(false);
                 mSearchCloseButton.setImageDrawable(getResources().getDrawable(R.drawable.transparent));
             }
            } catch (Exception e) {
                Log.e(TAG, "Error finding close button", e);
            }
        }

有关链接1的更多详细信息,链接1

相关内容

  • 没有找到相关文章

最新更新