【问题标题】:SearchView is inside overflow menu when using androidx libraries使用 androidx 库时,SearchView 位于溢出菜单内
【发布时间】:2019-01-16 18:27:51
【问题描述】:

您好,由于一些冲突,我最近更改了我的 android 项目以使用 androidx 库。

但是现在,我的 Toolbar 中的 SearchView 显示在溢出菜单中(仅在其中显示“搜索”,单击它时没有任何反应) 使用旧的支持库时情况并非如此。

Click here for an example

我需要旧的图标化搜索视图。请帮忙。

activity_main.xml

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolBar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:logo="@drawable/logo"
    app:layout_scrollFlags="scroll|enterAlways|snap"/>

我的 menu.xml

<item
    android:id="@+id/action_search"
    android:icon="@drawable/search_icon"
    android:showAsAction="always|collapseActionView"
    android:actionViewClass="androidx.appcompat.widget.SearchView"
    android:title="@string/search"/>

在 onCreateOptionsMenu 中

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main_menu,menu);

    SearchView searchView = (SearchView) menu.findItem( R.id.action_search).getActionView();

    return true;
}

如果有帮助,我的活动也是一个 androidx AppCompatActivity

我浏览了 android 开发者网站上的 androidx 文档以及许多 stackoverflow 问题,但没有运气。这个问题似乎是第一个..

【问题讨论】:

    标签: android android-support-library android-toolbar searchview androidx


    【解决方案1】:

    当我点击它时没有任何反应

    没有任何反应,因为您将其设置为 always|collapseActionView,这意味着您应该像这样放置 SearchView

    <item
            android:id="@+id/action_search"
            android:icon="@drawable/abc_ic_search_api_mtrl_alpha" <!-- You can change this.. -->
            android:title="@string/srch"
            app:actionViewClass="androidx.appcompat.widget.SearchView" 
            app:showAsAction="ifRoom|collapseActionView" />
    

    在这里查看我的完整答案:https://stackoverflow.com/a/34825224/4409113

    【讨论】:

    • 这没有像我所说的那样工作,我正在使用 androidx 支持库并在其中,将 searchview 添加到工具栏将其放置在溢出菜单中,尽管只有工具栏中的一项操作。感谢您的回复。
    • 哦,我的错,没有注意到 Androidx 的。更新的答案怎么样?你会尝试看看会发生什么吗?
    • 我在菜单项中将 always 更改为 ifRoom,但这并没有改变任何事情。
    • 点击不打开也不折叠?你必须使用androidxactionViewClass吗?因为我认为它应该与提供的链接一起使用。
    • 是的,因为我也在使用来自 androidx 的其他小部件。您认为这可能是 androidx 库中的错误或其他东西吗?感谢您的帮助!
    猜你喜欢
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多