<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_search" android:title="@string/menu_search" android:icon="@drawable/ic_menu_search" android:showAsAction="ifRoom|collapseActionView" android:actionViewClass="android.widget.SearchView" /> </menu>在android:showAsAction添加collapseActionView的效果是点击menu_search项,则能显示其余的action item(menu item),并且左边按钮有向上返回的功能,
II). 可以处理SerachView
src/com/wind/actionbar/HelloActionBar.java
@Override public boolean onCreateOptionsMenu(Menu menu) { this.getMenuInflater().inflate(R.menu.menu, menu); mSearchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); //配置搜索信息并添加事件监听器 });2). 使用android:actionLayout
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item1" android:title="111" android:icon="@drawable/ic_about" android:showAsAction="ifRoom|collapseActionView" android:actionLayout="@layout/custom_action_view"> </item> </menu> II).res/layout/custom_action_view.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/action_layout" android:orientation="horizontal" > <EditText android:id="@+id/edit_number" android:hint="input number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" > <requestFocus /> </EditText> <Button android:id="@+id/button_delete" android:text="Del" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>III). 为ActonView增加事件监听
package com.wind.actionbar; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.MenuItem.OnActionExpandListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.SearchView; import android.widget.TextView; public class HelloActionBarActivity extends Activity{ private static final String TAG = "HelloActionBarActivity"; TextView mTextView; SearchView mSearchView; MenuItem mSearchMenu; MenuItem mCustomMenu; private View mCustomMenuView; private Button mQuit; private OnClickListener mQuitListener; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v(TAG, "onCreate"); setContentView(R.layout.main); mTextView = new TextView(this); mTextView.setText("AAA"); mQuitListener = new OnClickListener() { @Override public void onClick(View v) { mCustomMenu.collapseActionView(); } }; } @Override public boolean onCreateOptionsMenu(Menu menu) { this.getMenuInflater().inflate(R.menu.menu, menu); mSearchMenu = menu.findItem(R.id.menu_search); mCustomMenu = menu.findItem(R.id.item1); mCustomMenuView = menu.findItem(R.id.item1).getActionView(); mQuit = (Button) mCustomMenuView.findViewById(R.id.button_delete); mQuit.setOnClickListener(mQuitListener); mSearchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); //配置搜索信息并添加事件监听器 return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { //响应menu item或者action item的点击 Log.v(TAG, "onOptionsItemSelected"); switch(item.getItemId()){ case R.id.item1: Log.v(TAG, "item1 pressed"); break; default: Log.v(TAG, "default"); } return super.onOptionsItemSelected(item); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { Log.v(TAG, "onKeyDown - keyCode is " + keyCode); if(keyCode == KeyEvent.KEYCODE_SEARCH){//测试的时候4.2版本在按下软键盘的Search键时没有看到报这个键值 Log.v(TAG, "KEYCODE_SEARCH is pressed"); //TODO process Search event mSearchMenu.collapseActionView(); } return super.onKeyDown(keyCode, event); } }