标签:
1.代码
package com.e.acationbar; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.ActionProvider; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.view.View.OnTouchListener; import android.view.WindowManager; import android.view.View; import android.widget.ImageView; import android.widget.PopupWindow; import android.widget.Toast; public class CustomActionProviderWithActionView extends ActionProvider implements OnClickListener, OnTouchListener, OnKeyListener { private final Context context;// 上下文 private View actionView;//action view private PopupWindow mPopupWindow;//popup window 里有个view,view显示的像个menu private View mMenu; //popup window 里的显示控件 private void initPopupMenu(){ LayoutInflater lif = LayoutInflater.from(context); mMenu = lif.inflate(R.layout.popup_window_menu, null); mMenu.setAlpha(1.0f);//内部view的透明度 //不用setBackgroundDrawable的时候必须加这句才能back键和menu键退出 // mMenu.setFocusableInTouchMode(true); // mMenu.setOnKeyListener(this); // mMenu.setOnTouchListener(this); mPopupWindow = new PopupWindow(mMenu, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); /* * setBackgroundDrawable很重要,没有这个函数 back键和menu键关闭popwindow 很麻烦, * 有了它以后,就不用为popwindow内的控件分别设置setOnKeyListener和setOnTouchListener了 * 可以同时支持back返回和点popwindow外面关闭popwindow, */ //0x7f0f0f0f = argb mPopupWindow.setBackgroundDrawable(new ColorDrawable(0xff0f0f0f));//0xff不透明 //bg为透明的图片 //mPopupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg)); mPopupWindow.setFocusable(true); mPopupWindow.setOutsideTouchable(true); mPopupWindow.setAnimationStyle(R.anim.popwindow_anim); mMenu.findViewById(R.id.imgBtn_in_popup_window).setOnClickListener(this); mMenu.findViewById(R.id.btn1_popup_window).setOnClickListener(this); mMenu.findViewById(R.id.btn2_popup_window).setOnClickListener(this); mMenu.findViewById(R.id.btn3_popup_window).setOnClickListener(this); mMenu.findViewById(R.id.btn4_popup_window).setOnClickListener(this); mMenu.findViewById(R.id.btnClose_popup_window).setOnClickListener(this); } public CustomActionProviderWithActionView(Context context) { super(context); // TODO Auto-generated constructor stub this.context = context; } /** * @return 需要返回null, 才能弹出子菜单.否则在actiobar上显示该view */ @Override public View onCreateActionView() { // TODO Auto-generated method stub LayoutInflater lif = LayoutInflater.from(context); actionView = lif.inflate(R.layout.provider_with_av, null); ImageView button = (ImageView) actionView .findViewById(R.id.btn_action_view_for_costomProvider); button.setOnClickListener(this); initPopupMenu(); return actionView;// 不return null不显示子菜单 } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_action_view_for_costomProvider: { mPopupWindow.showAsDropDown(v,-20,10); } return; case R.id.imgBtn_in_popup_window: Toast.makeText(context, "popup window image button clicked", Toast.LENGTH_SHORT).show(); break; case R.id.btn4_popup_window: case R.id.btn3_popup_window: case R.id.btn2_popup_window: case R.id.btn1_popup_window: Toast.makeText(context, "popup window item clicked", Toast.LENGTH_SHORT).show(); break; case R.id.btnClose_popup_window: break; } mPopupWindow.dismiss(); } @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch (keyCode) { case KeyEvent.KEYCODE_BACK: case KeyEvent.KEYCODE_MENU: if (mPopupWindow != null && mPopupWindow.isShowing()) { mPopupWindow.dismiss(); } break; } return true; } @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub mPopupWindow.dismiss(); return false; } }
2.本provider在主菜单中的配置
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="com.e.acationbar.MainActivity" > <item android:id="@+id/action_customProvider_no_actionView" android:actionProviderClass="com.e.acationbar.CustomActionProviderNoActionView" android:icon="@drawable/actionbar_setting_icon" android:showAsAction="always" android:title="@string/custom_provider_no_av"> </item> <item android:id="@+id/action_customProvider_with_actionView" android:actionProviderClass="com.e.acationbar.CustomActionProviderWithActionView" android:icon="@drawable/ic_jog_dial_answer_and_hold" android:showAsAction="always" android:title="@string/custom_provider_av"> </item> ... </menu>
3.provider 的actionView的布局文件 provider_with_av.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/btn_action_view_for_costomProvider" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/selector_img_view_bg" /> </LinearLayout>
4.popup window的view布局文件(模拟菜单):popup_window_menu.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" > <LinearLayout android:id="@+id/LinearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" > <ImageButton android:id="@+id/imgBtn_in_popup_window" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_jog_dial_decline" /> <Button android:id="@+id/btn4_popup_window" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/item" /> </LinearLayout> <Button android:id="@+id/btn3_popup_window" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/item" /> <Button android:id="@+id/btn2_popup_window" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/item" /> <Button android:id="@+id/btn1_popup_window" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/item" /> <Button android:id="@+id/btnClose_popup_window" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="close" /> </LinearLayout>
ActionBar(16)自定义操作提供器ActionProvider之返回ActionView
标签:
原文地址:http://www.cnblogs.com/cocl/p/4522340.html