2016-08-11 10 views
1

複数のフラグメントが表示されているナビゲーションビューとフラグメントコンテナを持っていますナビゲーションビューコードは以下のとおりです。私はツールバーに検索ボタンを表示できませんすべてのヘルプツールバーの検索ボタンを膨らませることができません

@Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     inflater.inflate(R.menu.menu_main, menu); 

     MenuItem item = menu.findItem(R.id.action_search); 

     super.onCreateOptionsMenu(menu, inflater); 
    } 



<LinearLayout 
    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:fitsSystemWindows="true" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary" 
     android:id="@+id/toolbar" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:title="e-Vyapar" /> 

    <android.support.v4.widget.DrawerLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:id="@+id/drawerLayout" 
     > 



     <FrameLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/containerView"> 


     </FrameLayout> 
<android.support.design.widget.NavigationView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:id="@+id/shitstuff" 
      app:itemTextColor="@color/black" 
      app:menu="@menu/drawermenu" 
      android:layout_marginTop="-24dp" 
      android:paddingTop="10dp" 
      /> 
</android.support.v4.widget.DrawerLayout> 
</LinearLayout> 




<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="com.cdac.ayush.MainActivity"> 
    <!-- <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="@string/action_settings" 
     app:showAsAction="never" />--> 


    <item 
     android:id="@+id/action_search" 
     android:icon="@android:drawable/ic_menu_search" 
     android:title="@string/action_search" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:showAsAction="always|collapseActionView" /> 
</menu> 

以下のように検索ボタンが感謝をいただければ幸いです。

+0

menu_main.xmlコードを表示 –

+0

はmenu_main.xmlファイル – user2645941

答えて

0

このようにしてみてください。

menu_main.xmlあなたの活動に比べ

<item 
    android:id="@+id/action_search" 
    android:icon="@android:drawable/ic_menu_search" 
    app:showAsAction="always|collapseActionView" 
    app:actionViewClass="android.support.v7.widget.SearchView" 
    android:title="Search"/> 

onCreateOptionsMenuでこのように実行します。

public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater menuInflater = getMenuInflater(); 
     menuInflater.inflate(R.menu.main_menu, menu); 

     MenuItem searchItem = menu.findItem(R.id.action_search); 

     SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE); 

     SearchView searchView = null; 
     if (searchItem != null) { 
      searchView = (SearchView) searchItem.getActionView(); 
     } 
     if (searchView != null) { 
      searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName())); 
     } 

     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
     @Override 
     public boolean onQueryTextSubmit(String query) { 
      Log.e("SearchOnQueryTextSubmit: " + query); 
      return false; 
     } 
     @Override 
     public boolean onQueryTextChange(String s) { 
      Log.e("SearchOnQueryTextChanged: " + s); 
      return false; 
     } 
    }); 
      return super.onCreateOptionsMenu(menu); 
    } 

を断片ではあなたがオプションメニューを有効にする必要がありますonCreate likeこの

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

で質問を編集しました。検索ボタンを表示できません。 – user2645941

+0

検索はASのメニューのプレビューレイアウトで表示されますか? –

+0

はいこれが表示されています... – user2645941

関連する問題