【问题标题】:Searchview in toolbar not filling all available space工具栏中的搜索视图未填充所有可用空间
【发布时间】:2020-04-25 05:50:19
【问题描述】:

我想让我的工具栏内的搜索视图占据所有可用空间。到目前为止,它并没有,而是在后退按钮旁边留下一片未使用的空间。我希望它占用更多的空间。

这是一张图片:

这是我的菜单 XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/ticker_searchview"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="@string/tickersearch"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="always" />

</menu>

这是我的工具栏 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/White"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Light"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light">

</androidx.appcompat.widget.Toolbar>

这里是相关的java代码:

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {

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

        SearchView tickerSearchView = (SearchView) menu.findItem(R.id.ticker_searchview).getActionView();

        tickerSearchView.setQueryHint("Ticker Symbol or Name");

        tickerSearchView.setIconifiedByDefault(false);

        tickerSearchView.setMaxWidth(Integer.MAX_VALUE);

        TextView tickerSearchViewTextView = tickerSearchView.findViewById(androidx.appcompat.R.id.search_src_text);

        tickerSearchViewTextView.setTypeface(font);

        tickerSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            final Intent toStockInfo = new Intent(Search.this, stockinfo.class);

            @Override
            public boolean onQueryTextSubmit(String query) {

                toStockInfo.putExtra("STOCKTICKER", query.toUpperCase());
                startActivity(toStockInfo);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                newSearchUpdateRecyclerContent(newText.toUpperCase());

                return true;
            }
        });

        return true;
    }

【问题讨论】:

  • @ShubhamPanchal 这就是我拥有的 SearchView 的所有 XML 代码。除非您希望我提供更多 Search.java 代码,否则与它无关。

标签: android android-studio android-toolbar android-search android-developer-api


【解决方案1】:

您的工具栏 XML 文件是什么?

在您的工具栏.xml 中执行此操作

    android:layout_width="match_parent"

【讨论】:

    猜你喜欢
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多