2017-05-23 133 views
0

我正在嘗試在活動中帶有工具欄的片段中將搜索按鈕添加爲菜單項。這是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_24px" 
    android:title="Search" 
    android:orderInCategory="1" 
    app:showAsAction="always|collapseActionView" /> 

在我的片段I類已在onCreateView方法中添加setHasOptionsMenu(true);。這是片段類中的onCreateOptionsMenu方法。

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.menu, menu); 
    mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); 
    mSearchView.setOnQueryTextListener(this); 
    super.onCreateOptionsMenu(menu, inflater); 
} 

到處搜索並嘗試一切,但搜索按鈕從不出現。我可能會錯過什麼?

回答

0

而不是onPrepareOptionsMenu

@Override 
public void onPrepareOptionsMenu(Menu menu) { 
    MenuItem mSearchMenuItem = menu.findItem(R.id.mi_search); 
    SearchView searchView = (SearchView) mSearchMenuItem.getActionView(); 
} 
+0

仍然沒有工作 – theanilpaudel

+0

@theanilpaudel嘗試添加里面的超級方法在最後。我忘了補充那一個。 –

1
<?xml version="1.0" encoding="utf-8"?> 
<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:title="Search" 
    android:icon="@drawable/abc_ic_search_api_mtrl_alpha" 
    app:showAsAction="ifRoom|collapseActionView" 
    app:actionViewClass="android.support.v7.widget.SearchView" /> 

</menu> 

在活動

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.menu_dash_board, menu); 
    // Retrieve the SearchView and plug it into SearchManager 
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); 
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); 
    searchView.setQueryHint("Search"); 

    return true; 
} 
@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    final SearchView searchView = (SearchView) MenuItemCompat 
      .getActionView(menu.findItem(R.id.action_search)); 
} 
+0

它的工作完美 –

0

使用下面的代碼。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 
} 

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.menu, menu); 
    mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); 
    mSearchView.setOnQueryTextListener(this); 
    super.onCreateOptionsMenu(menu, inflater); 
} 
0

你的菜單

<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:id="@+id/action_search" 
     android:title="@string/search" 
     android:orderInCategory="2" 
     android:actionLayout="@layout/seacrhlayout" 
     android:icon="@drawable/cart" 
     app:showAsAction="always" 
     /> 
</menu> 

你的動作佈局

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:padding="5dp"> 

    <RelativeLayout 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="10dp" 
     android:id="@+id/main"> 
     <ImageView 
      android:id="@+id/seacrh" 
      android:src="@drawable/search" 
      android:layout_width="14dp" 
      android:layout_height="14dp" 
      android:layout_centerInParent="true" 
      /> 
    </RelativeLayout> 
</FrameLayout> 

你oncreatemenuoption

@Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 

      getMenuInflater().inflate(R.menu.main_menu, menu); 
      MenuItem item = menu.findItem(R.id.action_search); 
      MenuItemCompat.setActionView(item, R.layout.seacrhlayout); 
      View view = (View) MenuItemCompat.getActionView(item); 
      ImageView search=view.findViewById(R.id.seacrh); 
      //do whatever you want to don on search button 

     return true; 
    } 

// please check your tool bar color and the icon color of the search. may be due to color its not visible