Searchview未显示在工具栏中 [英] Searchview not showing in Toolbar

查看:186
本文介绍了Searchview未显示在工具栏中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Support AppCompat v7库24.0.0上的Searchview实际上有问题.

I have actually a problem with my Searchview on the Support AppCompat v7 lib 24.0.0.

SearchView没有显示任何文本和输入文本(看屏幕截图)

The SearchView is not shown up no text and input text (look screenshot)

searchquery工作完美.

The searchquery work perfect.

那是我的菜单

<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=".MainActivity">
    <item
        android:title="@string/search"
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search_24dp"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

这是我的onCreateOptionsMenu;

And here my onCreateOptionsMenu;

menu.clear();
        inflater.inflate(R.menu.menu_search, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //Perform the final search

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //Text has changed, apply filtering?

                return false;
            }
        });

我希望有人能帮助我. :)

I hope someone can help me. :)

塞巴斯蒂安

推荐答案

我也遇到了这个问题,我已经将工具栏高度更改为绝对值,而不是wrap_content并解决了问题.我不知道为什么,但是我认为这个问题与CoordinatorLayout和工具栏的高度有关,某些东西打破了SearchView的高度.如果我使用LinearLayout而不是CoordinatorLayout和AppBarLayout可以工作.

i have this problem too, i've changed Toolbar height to absolute value instead of wrap_content and problem solved. i don't know why but i think this issue is related to CoordinatorLayout and height of toolbar, something breaks the SearchView height. if i use LinearLayout instead of CoordinatorLayout and AppBarLayout it works.

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search_white_24dp"
        android:orderInCategory="100"
        android:title="@string/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always|collapseActionView" />
</menu>

layout.xml

layout.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar_height"
            android:paddingTop="@dimen/toolbar_top_padding"
            android:background="?attr/colorPrimary"
            app:title="@string/drawer_item_publisher_customization"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            app:tabGravity="fill"
            style="@style/DefaultTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

toolbar_height值:

toolbar_height value:

<dimen name="toolbar_height">75dp</dimen>

在绝对高度值之前(layout_height:"wrap_content"):

Before absolute height value (layout_height:"wrap_content"):

绝对高度值后(layout_height:"75dp"):

After absolute height value (layout_height:"75dp"):

这篇关于Searchview未显示在工具栏中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆