片段可见时,Android ActionBar溢出菜单选项不起作用

时间:2013-12-08 03:20:16

标签: android android-layout android-fragments android-dialogfragment

我的应用程序中有一个动态可见的透明ActionBar(使用v4兼容性),当用户长按屏幕时,它会出现并消失。这工作正常,我可以使用ActionBar通过public boolean onOptionsItemSelected(MenuItem item)

执行我的操作

但是,当我在屏幕上显示DialogFragment时,只有可见(非溢出的)ActionBar操作会触发onOptionsItemSelected.溢出菜单会像以前一样出现,但我无法点击它们。他们表现得像是残疾人。

设置DialogFragments以便触发背景动作事件

final Window w = getDialog().getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

我的ActionBar xml并不太复杂,但现在是。

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:showAsAction="ifRoom" android:id="@+id/settings" android:title="@string/settings" android:icon="@android:drawable/ic_menu_preferences"/>
    <item android:showAsAction="ifRoom" android:id="@+id/history" android:title="@string/menu_history" android:icon="@drawable/ic_menu_archive"/>
    <item android:showAsAction="ifRoom" android:id="@+id/new_game" android:title="@string/new_game" android:icon="@android:drawable/ic_menu_add"/>
    <item android:showAsAction="ifRoom" android:id="@+id/stats" android:title="@string/menu_stats" android:icon="@android:drawable/ic_menu_agenda"/>
    <item android:showAsAction="ifRoom" android:id="@+id/help" android:title="@string/help" android:icon="@android:drawable/ic_menu_help"/>
</menu>

除此之外,我在我的应用程序中没有做任何太奇怪的事情。当显示DialogFragment时溢出菜单无法工作的任何想法?

我的DialogFragment类调用setHasOptionsMenu(true),我可以看到onCreateOptionsMenu(Menu menu, MenuInflater inflater)被调用,但我的onOptionsItemSelected没有被调用溢出项。

修改 我决定扔掉ActionBar实现并为我的应用程序使用Navigation Drawer滑块。在我实现了所有内容后,我遇到了同样的问题。没有DialogFragments可见,我可以使用导航抽屉,并进行选择。但是,当我的DialogFragment可见时,我可以将抽屉拖入和拖出,但我无法在ListView上进行任何选择。

这是我的子对话框的代码。 public class CChildDialog extends DialogFragment {

public interface DialogDismissHandler {
    abstract public void onDismiss(CChildDialog dlg);
}
protected Game m_game;
protected DialogDismissHandler m_dismissListener;

public CChildDialog() {
    super();
}

public boolean isDialogShowing() {
    Dialog d = getDialog();
    if (d != null)
        return d.isShowing();
    else
        return false;
}

public void setGame(Game g) {
    m_game = g;
}

public void setOnDismissHandler(DialogDismissHandler dismissListener) {
    m_dismissListener = dismissListener;
}

public void onDismiss(DialogInterface dlg) {
    super.onDismiss(dlg);

    if (m_dismissListener != null) {
        m_dismissListener.onDismiss(this);
        m_dismissListener = null;
    }

    m_game = null;
}

public void onPreferencesChanged(Game game) {

}

@Override
public void onStart() {
    final Window w = getDialog().getWindow();

    w.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

    WindowManager.LayoutParams lp = w.getAttributes();

    if (m_game.isAdVersion()) {
        // move it up 25 pixels
        lp.y = -25;         
    }
    // check for super small screens
    if (UIUtil.GetWindowSize(m_game.getWindowManager()).y < 400) {
        lp.y -= 30; // move it up a little more
    }

    super.onStart();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setStyle(DialogFragment.STYLE_NO_TITLE,R.style.Theme_CustomDialog);
}

public interface DialogDismissHandler { abstract public void onDismiss(CChildDialog dlg); } protected Game m_game; protected DialogDismissHandler m_dismissListener; public CChildDialog() { super(); } public boolean isDialogShowing() { Dialog d = getDialog(); if (d != null) return d.isShowing(); else return false; } public void setGame(Game g) { m_game = g; } public void setOnDismissHandler(DialogDismissHandler dismissListener) { m_dismissListener = dismissListener; } public void onDismiss(DialogInterface dlg) { super.onDismiss(dlg); if (m_dismissListener != null) { m_dismissListener.onDismiss(this); m_dismissListener = null; } m_game = null; } public void onPreferencesChanged(Game game) { } @Override public void onStart() { final Window w = getDialog().getWindow(); w.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); WindowManager.LayoutParams lp = w.getAttributes(); if (m_game.isAdVersion()) { // move it up 25 pixels lp.y = -25; } // check for super small screens if (UIUtil.GetWindowSize(m_game.getWindowManager()).y < 400) { lp.y -= 30; // move it up a little more } super.onStart(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(DialogFragment.STYLE_NO_TITLE,R.style.Theme_CustomDialog); }

这是我的自定义主题

1 个答案:

答案 0 :(得分:1)

答案是添加FLAG_NOT_FOCUSABLE LayoutParams标志。添加此选项可在DialogFragment可见时完全使用溢出菜单。