1

Я использую пользовательскую панель действий для своего приложения. Это код:android-Как добавить поиск в пользовательскую панель действий

private void actionbar() { 
    ActionBar actionBar = getSupportActionBar(); 
    LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View mCustomView = layoutInflater.inflate(R.layout.actionbar, null); 

    ImageView nDrawer=(ImageView)mCustomView.findViewById(R.id.imageView4); 
    nDrawer.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      DrawerActivity.open();    
     } 
    }); 

    ///search 

    actionBar.setCustomView(mCustomView); 
    actionBar.setDisplayShowCustomEnabled(true); 
    getSupportActionBar().setDisplayShowCustomEnabled(true); 
    actionBar.setDisplayShowTitleEnabled(false); 
    actionBar.setDisplayHomeAsUpEnabled(true); 
    actionBar.setDisplayUseLogoEnabled(false); 
    actionBar.setDisplayShowHomeEnabled(false); 
    actionBar.setHomeButtonEnabled(true); 
    actionBar.setIcon(null); 
} 

это файл XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/menubg" 
    android:paddingLeft="3dp" 
    android:paddingRight="3dp" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/ic_action_search" /> 
</RelativeLayout> 

Я хочу использовать SearchView как точки зрения поиска андроида по умолчанию (я имею в виду, как значок поиска в меню, когда нажмите на нем, он превращается в edittext)

Как добавить поисковое окно в свою панель действий?

ответ

-1

Используйте этот виджет в пользовательском макете

<android.support.v7.widget.SearchView 
     android:suggestionRowLayout="@layout/suggestion_row_layout" 
     android:id="@+id/search_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:iconifiedByDefault="false" 
     android:inputType="textFilter"/> 

и чем:

ActionBar actionBar = getSupportActionBar(); 
    if (actionBar != null) { 
     mSearchModeOn = true; 
     mSearchMenu.setVisible(false); 
     final View searchViewContainer = LayoutInflater.from(actionBar.getThemedContext()) 
       .inflate(R.layout.custom_action_bar, null); 
     actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp); 
     actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); 
     mSearchView = (SearchView) searchViewContainer.findViewById(R.id.search_view); 

     mSearchView.setIconifiedByDefault(true); 
     mSearchView.setQueryHint(getString(R.string.btSearch)); 
     mSearchView.setIconified(false); 

     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     mSearchView.setOnQueryTextListener(this); 
     mSearchView.setOnCloseListener(this); 
     mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 

     actionBar.setCustomView(searchViewContainer, 
       new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, 
         ActionBar.LayoutParams.WRAP_CONTENT)); 
     actionBar.setDisplayShowCustomEnabled(true); 
+0

Ваше решение не работает. –