github上找到一个不错的demo
activity中使用的主题
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<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:orderInCategory="4"
android:title="@string/action_settings"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
<item
android:id="@+id/action_settings"
android:orderInCategory="1"
android:title="@string/action_settings"
app:showAsAction="always" />
</menu>
布局
<LinearLayout 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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame">
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="@+id/id_tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:navigationContentDescription="back"></android.support.v7.widget.Toolbar><!-- ?attr/homeAsUpIndicator-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<android.support.v7.widget.SearchView
android:id="@+id/SearchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/abc_ic_menu_copy_mtrl_am_alpha"
app:goIcon="@drawable/ic_launcher"></android.support.v7.widget.SearchView>
</LinearLayout>
package com.anand.toolbardemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener, View.OnClickListener {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.id_tool_bar);
// getSupportActionBar().setDisplayShowHomeEnabled(true);
// toolbar.setNavigationIcon(R.drawable.ic_launcher);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
SearchView sv= (SearchView) findViewById(R.id.SearchView);
sv.setSubmitButtonEnabled(true);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
private void toast(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView();
sv.setOnSearchClickListener(this);
sv.setOnQueryTextListener(this);
sv.setSubmitButtonEnabled(true);
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) {
toast("action_settings");
} else if (id == android.R.id.home) {
toast("home");
} else if (id == R.id.action_search) {
toast("action_search");
// item.collapseActionView();
//是搜索框默认展开
// SearchView sv = (SearchView) item.getActionView(); //空指针
//
// item.expandActionView();
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onQueryTextSubmit(String query) {
toast("onQueryTextSubmit:" + query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
toast("onQueryTextSubmit:" + newText);
return true;
}
@Override
public void onClick(View v) {
toast("onClick:" + v.getId());
}
private void log(String s) {
Log.d(getClass().getSimpleName() + "->log->>", s);
}
}