-
menu 代码
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!--
android:actionViewClass="" 添加View 如果是系统提供的控件(包名.类名)
!!!!!showAsAction如果为never将失去效果!!!!!
android:actionLayout="@layout/layout" 添加自定义的View
-->
<item
android:id="@+id/action_search"
android:orderInCategory="100"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"
android:title="搜索"/>
<item
android:id="@+id/action_spinner"
android:orderInCategory="100"
android:showAsAction="collapseActionView"
android:actionLayout="@layout/layout"
android:title="spinner"/> </menu> -
MainActivity XMl布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="openClick"
android:text="展开"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="closeClick"
android:text="折叠"
/> </LinearLayout> -
一个自定义View(用于填充到menu中)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </LinearLayout> -
MainActivity 代码:
package com.qf.day15_actionbar_demo02; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnActionExpandListener;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { //SearchView searchView; private TextView tvSHow; private Menu menu; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tvSHow = (TextView) findViewById(R.id.tv_show);
} //打开
public void openClick(View v){
MenuItem item = menu.findItem(R.id.action_spinner);
if(!item.isActionViewExpanded()){//判断是否展开
item.expandActionView();//展开
} }
//关闭
public void closeClick(View v){
MenuItem item = menu.findItem(R.id.action_spinner);
if(item.isActionViewExpanded()){//判断是否展开
item.collapseActionView();//关闭
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu); this.menu = menu;
//找出菜单项
MenuItem item = menu.findItem(R.id.action_search); //从菜单项里找出View
SearchView searchView = (SearchView) item.getActionView(); //设置查找的监听
searchView.setOnQueryTextListener(new OnQueryTextListener() { //点击搜索按钮时 执行此方法
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "=onQueryTextSubmit="+query, 0).show();
return false;
} //文本改变时 执行此方法
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "=onQueryTextChange="+newText, 0).show();
return false;
}
}); //找出菜单项
MenuItem spinnerItem = menu.findItem(R.id.action_spinner);
//找出layout布局
View view = spinnerItem.getActionView();
//从布局中找控件
Spinner spinner = (Spinner) view.findViewById(R.id.spinner); String[] items = {"c","c++","php","java","android"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, items); spinner.setAdapter(adapter); //展开和折叠的监听
spinnerItem.setOnActionExpandListener(new OnActionExpandListener() { @Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub tvSHow.setText("==onMenuItemActionExpand===展开====");
return true;//返回false 不会折叠
} @Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO Auto-generated method stub tvSHow.setText("==onMenuItemActionCollapse===折叠的====");
return true;
}
}); return true;
} }