2015-04-27 17 views
7

Próbuję dodać przycisk wyszukiwania po lewej stronie okna ustawień, ale nie mogę dodać paska narzędzi.Jak dodać przycisk wyszukiwania na pasku narzędzi w materiale projektowym

Oto mój kod menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.example.materialtheme.MainActivity" > 

    <item 
     android:id="@+id/action_search" 
     android:icon="@drawable/ic_launcher" 
     android:menuCategory="secondary" 
     android:showAsAction="ifRoom" 
     android:title="@string/action_search"/> 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:showAsAction="never" 
     android:title="@string/action_settings"/> 

</menu> 

i tu jest moja toolbar.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:minHeight="?attr/actionBarSize" 
    android:elevation="4dp" 
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

jestem coraz pasek ale nie dostaję przycisk wyszukiwania?

+0

Proszę korzystać z tego prosty otoki;) https://gist.github.com/ademar111190/7d31dab71502e6a85b8a –

Odpowiedz

4

Zastosowanie app:showAsAction="always" w menu.xml z xmlns:app="http://schemas.android.com/apk/res-auto"> w tagu menu

+0

thanks to działa – nanoweb

19

Dodaj menu_search.xml w folderze menu takiego.

<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:orderInCategory="200" 
    android:title="Search" 
    app:actionViewClass="android.support.v7.widget.SearchView" 
    android:icon="@android:drawable/ic_menu_search" 
    app:showAsAction="always"/> 

i zastąpić tę metodę w swojej klasie Aktywny

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_search, menu); 

    // Associate searchable configuration with the SearchView 
    // SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); 
    // searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
    searchView.setOnQueryTextListener(this); 
    return super.onCreateOptionsMenu(menu); 
} 
+0

searchView.setOnQueryTextListener (this); tej linii nie można usunąć – nanoweb

+0

usunąć tę linię. –

+2

Jeśli chcesz ustawić detektor zmiany tekstu zapytania, to implementuje SearchView.OnQueryTextListener z klasą aktywności. I zastąp tę metodę. –

Powiązane problemy