标签:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/blue_light</item> <item name="colorPrimaryDark">@color/blue_light</item> <item name="colorAccent">@color/blue_light</item> </style>2、修改AndroidManifest.xml,把application节点的android:theme属性值改为第一步定义的风格,如android:theme="@style/AppBaseTheme"
<android.support.v7.widget.Toolbar android:id="@+id/tl_head" android:layout_width="match_parent" android:layout_height="wrap_content" />4、Activity代码改为继承AppCompatActivity,注意早期的v7包是没有AppCompatActivity的,所以前面才说要先把v7包更新到最新版本。
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
//显示OverflowMenu的Icon public static void setOverflowIconVisible(int featureId, Menu menu) { //ActionBar的featureId是8,Toolbar的featureId是108 if (featureId%100 == Window.FEATURE_ACTION_BAR && menu != null) { if (menu.getClass().getSimpleName().equals("MenuBuilder")) { try { Method m = menu.getClass().getDeclaredMethod( "setOptionalIconsVisible", Boolean.TYPE); m.setAccessible(true); m.invoke(menu, true); } catch (Exception e) { Log.d(TAG, e.getMessage()); } } } }
import java.util.Date; import com.example.exmtoolbar.util.Utils; import android.support.v4.view.MenuItemCompat; 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.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.TextView; import android.widget.Toast; import android.app.SearchManager; import android.app.SearchableInfo; import android.content.ComponentName; import android.content.Context; import android.graphics.Color; import android.os.Bundle; public class MainActivity extends AppCompatActivity { private final static String TAG = "MainActivity"; private TextView tv_desc; private String[] mFormatArray = {"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "yyyy年MM月dd日HH时mm分ss秒", "yyyy年MM月dd日"}; private String mFormat = mFormatArray[0]; private Date mNowTime = new Date(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_desc = (TextView) findViewById(R.id.tv_desc); Toolbar tl_head = (Toolbar) findViewById(R.id.tl_head); tl_head.setBackgroundResource(R.color.blue_light); tl_head.setLogo(R.drawable.ic_launcher); tl_head.setTitle("标题"); tl_head.setSubtitle("副标题"); tl_head.setNavigationIcon(R.drawable.ic_back); setSupportActionBar(tl_head); } private void initSearchView(Menu menu) { MenuItem menuItem = menu.findItem(R.id.menu_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem); if(searchView == null){ Log.d(TAG, "Fail to get SearchView."); } else { //新旧SearchView公用代码开始 searchView.setIconifiedByDefault(true); searchView.setSubmitButtonEnabled(true); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); ComponentName cn = new ComponentName(this, SearchResultActvity.class); SearchableInfo info = searchManager.getSearchableInfo(cn); if(info == null){ Log.d(TAG, "Fail to get SearchResultActvity."); } searchView.setSearchableInfo(info); //新旧SearchView公用代码结束 sac_text = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text); sac_text.setTextColor(Color.WHITE); sac_text.setHintTextColor(Color.WHITE); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { doSearch(newText); return true; } }); Bundle bundle = new Bundle(); bundle.putString("hi", "hello"); searchView.setAppSearchData(bundle); } } private SearchView.SearchAutoComplete sac_text; private String[] hintArray = {"ab", "abc", "abcde", "abHtp", "aaeet", "aab"}; private void doSearch(String text) { if (text.indexOf("a") == 0) { ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, R.layout.list_auto, hintArray); sac_text.setAdapter(adapter); sac_text.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView tv_item = (TextView) view; sac_text.setText(tv_item.getText()); } }); } } @Override public boolean onMenuOpened(int featureId, Menu menu) { //显示菜单项左侧的图标 Utils.setOverflowIconVisible(featureId, menu); return super.onMenuOpened(featureId, menu); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); //对搜索框做初始化 initSearchView(menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { finish(); } else if (id == R.id.menu_refresh) { mNowTime = new Date(); tv_desc.setText("当前刷新时间: "+Utils.getFormatDateTime(mNowTime, mFormat)); return true; } else if (id == R.id.menu_about) { Toast.makeText(this, "这个是工具栏的演示demo", Toast.LENGTH_LONG).show(); return true; } else if (id == R.id.menu_quit) { finish(); } return super.onOptionsItemSelected(item); } }
标签:
原文地址:http://blog.csdn.net/aqi00/article/details/52242759