2015-04-27 57 views
7

Tôi đang cố gắng thêm nút tìm kiếm ở bên trái của cài đặt bật lên nhưng tôi không thể thêm vào thanh công cụ.Cách thêm nút tìm kiếm trên thanh công cụ trong thiết kế material design

Đây là mã của tôi về 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> 

và đây là toolbar.xml tôi:

<?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" /> 

Tôi nhận được thanh công cụ nhưng không nhận được nút tìm kiếm?

+0

Vui lòng sử dụng wrapper đơn giản này;) https://gist.github.com/ademar111190/7d31dab71502e6a85b8a –

Trả lời

4

Sử dụng app:showAsAction="always" trong menu.xml của bạn với xmlns:app="http://schemas.android.com/apk/res-auto"> trong thẻ menu

+0

nhờ nó đang làm việc – nanoweb

19

Thêm menu_search.xml trong thư mục thực đơn của bạn như thế này.

<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"/> 

Và ghi đè phương pháp này trong lớp học Hoạt động của bạn

@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); dòng này không thể được giải quyết – nanoweb

+0

xóa dòng này. –

+2

Nếu bạn muốn đặt trên trình xử lý thay đổi văn bản truy vấn, sau đó triển khai SearchView.OnQueryTextListener với lớp hoạt động của bạn. Và ghi đè phương thức của nó. –

Các vấn đề liên quan