试图为我的活动强制全屏(没有导航栏) - 按钮点击错误

时间:2015-02-13 16:37:20

标签: android button fullscreen

我正在开发一款针对Android的游戏,我希望我的Activity全屏(甚至删除某些设备中保持在底部的导航栏),所以我在我的onResume()上写了这个Activity

 super.onResume();
        View decorView = getWindow().getDecorView();
        // Hide both the navigation bar and the status bar.
        // SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
        // a general rule, you should design your app to hide the status bar whenever you
        // hide the navigation bar.
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                      | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

但奇怪的是,这导致我的Activity仅在第二次点击后检测到按钮点击。我甚至尝试在活动标签内的清单中使用android:theme="@android:style/Theme.NoTitleBar.Fullscreen",但这并没有解决按钮错误。有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

此问题与"Immersive mode failing: Is it the code, or the virtual device?"中的问题非常相似。主要问题似乎是,在某些设备上,View.SYSTEM_UI_FLAG_HIDE_NAVIGATION仅导致每隔一个ACTION_DOWN收到触摸事件。

不幸的是,没有报道任何解决方案。但是,您可以通过阅读其他问题及其包含的代码来获得更多理解。

顺便说一句,toggleHideyBar()使用^=来切换导航用户界面。您将使用|=代替,因为您要设置标志,而不是切换它们。另外,请注意toggleHideyBar()不会替换所有现有标志(就像您在代码中所做的那样),但只修改它关心的特定标志 - 这样更安全,因为您不想意外更改任何其他标志标志。

答案 1 :(得分:0)

我基于InmersiveMode SDK示例创建了下一个类,您可以为您的requeriments改进此类:

/**
 * Created by clarkxp on 10-02-15.
 */
public class SystemUIController {

    public FragmentActivity actionBarActivity;

    public static final int FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES = 0x1;


    public static final int FLAG_FULLSCREEN = 0x2;


    public static final int FLAG_HIDE_NAVIGATION = FLAG_FULLSCREEN | 0x4;

    protected int mFlags;

    private int mShowFlags;

    /**
     * Flags for {@link View#setSystemUiVisibility(int)} to use when hiding the
     * system UI.
     */
    private int mHideFlags;

    /**
     * Flags to test against the first parameter in
     * {@link android.view.View.OnSystemUiVisibilityChangeListener#onSystemUiVisibilityChange(int)}
     * to determine the system UI visibility state.
     */
    private int mTestFlags;

    /**
     * Whether or not the system UI is currently visible. This is cached from
     * {@link android.view.View.OnSystemUiVisibilityChangeListener}.
     */
    private boolean mVisible = true;
    private View mAnchorView;

    private OnInmersiveModeListener onInmersiveModeListener;


    public SystemUIController(ActionBarActivity actionBarActivity) {
        this.actionBarActivity = actionBarActivity;
        onInmersiveModeListener = (OnInmersiveModeListener) actionBarActivity;
        mShowFlags = View.SYSTEM_UI_FLAG_VISIBLE;
        mHideFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
        mTestFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE;
        if ((mFlags & FLAG_FULLSCREEN) != 0) {
            // If the client requested fullscreen, add flags relevant to hiding
            // the status bar. Note that some of these constants are new as of
            // API 16 (Jelly Bean). It is safe to use them, as they are inlined
            // at compile-time and do nothing on pre-Jelly Bean devices.
            mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
        }

        if ((mFlags & FLAG_HIDE_NAVIGATION) != 0) {
            // If the client requested hiding navigation, add relevant flags.
            mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
            mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            mTestFlags |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }
    }



    public boolean isVisible() {
        return mVisible;
    }

    public void toggleHideyBar() {

        // BEGIN_INCLUDE (get_current_ui_flags)
        // The UI options currently enabled are represented by a bitfield.
        // getSystemUiVisibility() gives us that bitfield.
        int uiOptions = actionBarActivity.getWindow().getDecorView().getSystemUiVisibility();
        int newUiOptions = uiOptions;
        // END_INCLUDE (get_current_ui_flags)
        // BEGIN_INCLUDE (toggle_ui_flags)


        // Navigation bar hiding:  Backwards compatible to ICS.
        if (Build.VERSION.SDK_INT >= 14) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }

        // Status bar hiding: Backwards compatible to Jellybean
        if (Build.VERSION.SDK_INT >= 16) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
        }

        // Immersive mode: Backward compatible to KitKat.
        // Note that this flag doesn't do anything by itself, it only augments the behavior
        // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
        // all three flags are being toggled together.
        // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
        // Sticky immersive mode differs in that it makes the navigation and status bars
        // semi-transparent, and the UI flag does not get cleared when the user interacts with
        // the screen.
        if (Build.VERSION.SDK_INT >= 18) {
            newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        }

        actionBarActivity.getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
        //END_INCLUDE (set_ui_flags)
        boolean isImmersiveModeEnabled =
                ((newUiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == newUiOptions);
        if (isImmersiveModeEnabled) {
            // Log.i(TAG, "Turning immersive mode mode off. ");
            mVisible = false;
        } else {
            mVisible = true;
            // Log.i(TAG, "Turning immersive mode mode on.");
        }
        onInmersiveModeListener.inmersiveChanged(isVisible());

    }

    public interface OnInmersiveModeListener{
        public void inmersiveChanged(boolean visible);
    }
}

要在您的活动中使用此代码:

public class ActivityExample extends ActionBarActivity implements SystemUIController.OnInmersiveModeListener {

    private SystemUIController systemUIController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myLayout);
        systemUIController = new SystemUIController(this);
        Button button = (Button) findViewById(R.id.myButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                systemUIController.toggleHideyBar();
            }
        });
    }

    @Override
    public void inmersiveChanged(boolean visible) {
        Toast.makeText(this,"Is Visible? "+visible,Toast.LENGTH_SHORT).show();
    }
}