在工具栏中搜索行为不端

时间:2017-09-23 10:52:25

标签: java android

单击搜索图标,然后单击3个垂直点,然后单击后退按钮,搜索图标和后退按钮将消失。

此外,当我点击搜索图标,然后单击3个垂直点并点击...之后的任何项目,然后单击返回...搜索图标和后退按钮也会消失

enter image description here

enter image description here

我的meny_main.xml

 <item
    android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    app:showAsAction="never" />

<item
    android:id="@+id/search"
    android:title="@string/action_search"
    android:hint="@string/search_hint"
    android:icon="@drawable/ic_search_white_24dp"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

我的MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(mToolbar);
    if(getSupportActionBar()!=null) {
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()) );
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

我只想要一个简单的功能搜索系统......

0 个答案:

没有答案