大致这样的效果
1、首页自定义一个ActionProvider类:ToolBarCustomActionProvider
2、menu的xml中配置actionProviderClass
3、code中配置
具体;
1、ToolBarCustomActionProvider类 继承的是v4下的class
package com.sunstar.cloudseeds.custom;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.sunstar.cloudseeds.R;
/** * Created by louisgeek on 2016/8/16. */
public class ToolBarCustomActionProvider extends ActionProvider {
Context mContext;
TextView id_text_btn;
boolean hasConfigedText=false;
String mTitle;
private static final String TAG = "CustomActionProvider";
/** * Creates a new instance. * * @param context Context for accessing resources. */
public ToolBarCustomActionProvider(Context context) {
super(context);
mContext=context;
}
@Override
public View onCreateActionView() {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view =inflater.inflate(R.layout.toolbar_right_custom_item_action_proider,null);
Log.d(TAG,"onCreateActionView");
id_text_btn= (TextView) view.findViewById(R.id.id_text_btn);
if (id_text_btn!=null) {
id_text_btn.setText(mTitle);
}
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onWrapBtnViewClickLister.OnWrapBtnViewClick(v);
}
});
return view;
}
/** * 调用的时候 会比onCreateActionView先执行 * @param title */
public void setTitle(String title) {
mTitle=title;
}
public interface OnWrapBtnViewClickLister{
void OnWrapBtnViewClick(View wrapBtnView);
}
public void setOnWrapBtnViewClickLister(OnWrapBtnViewClickLister onWrapBtnViewClickLister) {
this.onWrapBtnViewClickLister = onWrapBtnViewClickLister;
}
OnWrapBtnViewClickLister onWrapBtnViewClickLister;
}
对应的xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:tools="http://schemas.android.com/tools" android:paddingRight="12dp" >
<TextView android:id="@+id/id_text_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="xx" android:textSize="15dp" android:layout_gravity="center" android:background="@drawable/selector_action_menu_bg" android:textColor="@color/white" android:gravity="center" />
</FrameLayout>
2、 menu_calculation.xml中配置actionProviderClass
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" >
<!--app 来处理兼容性的v4下的 class-->
<item android:id="@+id/action_calculation" android:orderInCategory="100" app:actionProviderClass="com.sunstar.cloudseeds.custom.ToolBarCustomActionProvider" android:title="计算" app:showAsAction="always"/>
</menu>
3、code中配置,把本来onOptionsItemSelected处理的按钮点击后事件移动到OnWrapBtnViewClickLister里
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_calculation,menu);
//////
MenuItem menuItem=menu.findItem(R.id.action_calculation);
ToolBarCustomActionProvider toolBarCustomActionProvider = (ToolBarCustomActionProvider)MenuItemCompat.getActionProvider(menuItem);
toolBarCustomActionProvider.setTitle(menuItem.getTitle()+"");
toolBarCustomActionProvider.setOnWrapBtnViewClickLister(new ToolBarCustomActionProvider.OnWrapBtnViewClickLister() {
@Override
public void OnWrapBtnViewClick(View wrapBtnView) {
//从onOptionsItemSelected移动到此处
doCalculation();
}
});
return super.onCreateOptionsMenu(menu);
}