android toolbar代替actionbar使用

github上找到一个不错的demo

activity中使用的主题

 
 
     
     
 

布局

 
     
     

     

     
 

 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);
     }

 }

你可能感兴趣的:(android toolbar代替actionbar使用)