以下是我的ActionProvider类
import java.util.Locale;
import android.content.Context;
import android.util.Log;
import android.view.ActionProvider;
import android.view.LayoutInflater;
import android.view.SubMenu;
import android.view.View;
import android.widget.ImageButton;
public class LocaleActionProvider extends ActionProvider {
private static final String TAG = "LocaleActionProvider";
Context mCtx;
public LocaleActionProvider(Context context) {
super(context);
mCtx = context;
}
@Override
@Deprecated
public View onCreateActionView() {
// Inflate the action view to be shown on the action bar.
LayoutInflater layoutInflater = LayoutInflater.from(mCtx);
View view = layoutInflater.inflate(R.layout.action_provider_locale,
null);
ImageButton button = (ImageButton) view.findViewById(R.id.localeButton);
// Set up flag icon for specific locale
Locale locale = Locale.getDefault();
String locLan = locale.getLanguage();
if (locLan.equals(Locale.CHINESE.getLanguage())) {
button.setImageResource(R.drawable.insready);
} else if (locLan.equals(Locale.ENGLISH.getLanguage())) {
button.setImageResource(R.drawable.ic_action_locale_en);
} else if (locLan.equals(Locale.FRENCH.getLanguage())) {
button.setImageResource(R.drawable.ic_action_search);
}
/*
* button.setOnClickListener(new View.OnClickListener() {
*
* @Override public void onClick(View v) { // Do something... } });
*/
return view;
}
@Override
public boolean hasSubMenu() {
return true;
}
@Override
public void onPrepareSubMenu(SubMenu subMenu) {
Log.i(TAG, "onPrepareSubMenu()");
// Clear since the order of items may change.
subMenu.clear();
subMenu.add(0, 0, 0, "简体中文");
subMenu.add(0, 1, 1, "English");
subMenu.add(0, 2, 2, "French");
}
@Override
public boolean onPerformDefaultAction() {
Log.i(this.getClass().getSimpleName(), "onPerformDefaultAction");
return super.onPerformDefaultAction();
}
}
我尝试使用调试器或Log.i()来监视onPrepareSubMenu()函数。但是从不调用onPrepareSubMenu(),换句话说,调试器永远不会在断点处停止并且没有日志条目。
答案 0 :(得分:4)
我发现在onCreateActionView()中,你必须返回null才能调用onPrepareSubMenu()。然后,onCreateActionView()中的所有这些设置都需要在MainActivity中进入onCreateOptionsMenu()