简体   繁体   English

SearchView 图标显示两次

[英]SearchView icon shows twice

I've implemented SearchView inside my toolbar, following Android's official instructions.我已经按照 Android 的官方说明在我的工具栏中实现了 SearchView。

SearchView works well itself, but when I tap the search icon, it is shifted to the left instead of showing the Search Hint and Close button, although if I click this "second Search" icon, finally I get the typical search view. SearchView 本身运行良好,但是当我点击搜索图标时,它会向左移动,而不是显示“搜索提示”和“关闭”按钮,尽管如果我单击“第二个搜索”图标,我最终会看到典型的搜索视图。

Here is my xml of the toolbar:这是我的工具栏 xml:

<item android:id="@+id/search"
        android:title="@string/search_view"
    app:showAsAction="collapseActionView|ifRoom"
    android:iconTint="@android:color/white"
        android:icon="@drawable/ic_search_white_24dp"
    app:actionViewClass="android.widget.SearchView" />

And my onCreateOptionsMenu还有我的 onCreateOptionsMenu

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();

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

        return true;
    }

And my manifest.xml还有我的 manifest.xml

<activity
            android:name=".Activities.MainActivity"
            android:label="@string/addWordLabel_Act"
            android:launchMode="singleTop"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH"/>
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>

Finally, here you have some screenshots of the problem: SearchView before tapping最后,这里有一些问题的截图:点击之前的 SearchView

SearchView after first tap, Here's the problem第一次点击后的 SearchView,这就是问题所在

SearchView after second tap, "the normal SearchView"第二次点击后的 SearchView,“正常的 SearchView”

And that's all.就这样。

Note: I haven't implemented an Activity/Fragment to handle the Search yet.注意:我还没有实现一个 Activity/Fragment 来处理搜索。

Androidx solution安卓x解决方案

Adding this answer because my reputation wasn't enough to reply to Wesley Franks添加此答案是因为我的声誉不足以回复 Wesley Franks

Layout布局

app:actionViewClass="androidx.appcompat.widget.SearchView"

Activity (Kotlin)活动(科特林)

menu?.findItem(R.id.search)?.actionView as? androidx.appcompat.widget.SearchView

Activity (Java)活动(Java)

(androidx.appcompat.widget.SearchView) menu.findItem(R.id.search).getActionView();

Finally, I got the solution.最后,我得到了解决方案。 It was easy, but I've spent two days trying to figure it out.这很容易,但我花了两天时间试图弄清楚。

Just, for compatibility issues, use只是,对于兼容性问题,请使用

app:actionViewClass="android.support.v7.widget.SearchView"

instead of app:actionViewClass="android.widget.SearchView"而不是app:actionViewClass="android.widget.SearchView"

and, of course, change当然,改变

SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
       SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();

by经过

SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        android.support.v7.widget.SearchView searchView =
                (android.support.v7.widget.SearchView) menu.findItem(R.id.search).getActionView();

This is best answer for this issue这是这个问题的最佳答案

@Override
    public void onCreateOptionsMenu(Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_main, menu);

        MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
        final androidx.appcompat.widget.SearchView searchView = (androidx.appcompat.widget.SearchView) myActionMenuItem.getActionView();
        searchView.setOnQueryTextListener(new androidx.appcompat.widget.SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                searchView.setQuery("", false);
                searchView.setIconified(true);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                  //Do your stuff
                return true;
            }
        });
    }

If none of above solution works for you as in my case: Just set background color to the SearchView.如果以上解决方案都不适用于我的情况:只需将背景颜色设置为 SearchView。 And you will only see 1 magnifying glass icon.您只会看到 1 个放大镜图标。

Before Calling the Menu Item Clear the Menu First.在调用菜单项之前首先清除菜单。

 menu.clear();

Full Code完整代码

 @Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.main, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.appSearchBar).getActionView();
    searchView.setQueryHint("Search...");
    searchView.setIconified(false);

onCreateOptionsMenu is use because i call it in Fragment.使用 onCreateOptionsMenu 是因为我在 Fragment 中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM