1

下面是我的 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(),换句话说,调试器永远不会在断点处停止并且没有日志条目。

4

1 回答 1

4

我发现在 onCreateActionView() 中,您必须返回 null 才能调用 onPrepareSubMenu()。然后,onCreateActionView() 中的所有这些设置可能需要进入 MainActivity 中的 onCreateOptionsMenu()

于 2013-03-29T09:06:36.773 回答