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() では、 onPrepareSubMenu() を呼び出すために null を返す必要があることがわかりました。次に、 onCreateActionView() のこれらすべての設定は、おそらく MainActivity の onCreateOptionsMenu() に入る必要があります

于 2013-03-29T09:06:36.773 に答える