Searchview not showing in Toolbar

前端 未结 5 1314
忘了有多久
忘了有多久 2021-02-04 14:42

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

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

相关标签:
5条回答
  • 2021-02-04 15:31

    if you are using theme: .NoActionBar set this in your onCreate()

    setSupportActionBar(toolbar);
    
    0 讨论(0)
  • 2021-02-04 15:31

    As @Apruv mentioned above If you are using NoActionBar in your styles.xml then try to add this line in your onCreate().This will work for sure. I just done this all it works perfect.

    setSupportActionBar(toolbar);
    
    0 讨论(0)
  • 2021-02-04 15:40

    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

    <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 value:

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

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

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

    0 讨论(0)
  • 2021-02-04 15:41

    This is very odd. I'm using build tools 25.0.1 and support lib 25.1.0. For me the accepted answer was not necessary, but the way the menu item was defined needs a small change. I need it like so:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_search_24dp"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.widget.SearchView"/>
    

    Where app:showAsAction is the key. Lint underlines this as red and tells me:

    Should use android:showAsAction when using the appcompat library.

    However if I have it set to android:showAsAction then the search view simply does not appear. Go figure.

    0 讨论(0)
  • 2021-02-04 15:43

    Make sure you have add AppCompat library to your project. Is your activity was extended "AppCompatActivity"?

    And also showAsAction shoud be: showAsAction="always|collapseActionView"

    Hope this help!

    0 讨论(0)
提交回复
热议问题