简体   繁体   English

使用全屏活动

[英]Using full screen Activity

I am making a simple game and so far I've been using the Blank Activity. 我正在制作一个简单的游戏,到目前为止我一直在使用空白活动。 Now I want it to cover the entire screen, Will I need to Recode the entire thing using a FullScreen Activity? 现在我希望它覆盖整个屏幕,我是否需要使用FullScreen活动重新编码整个内容? I've tried looking for something online but every thing i came across had adding this bit:​ 我试过在网上找东西,但我遇到的每件事都添加了这个:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);

Which causes the app to crash as soon as it is launched on a device. 这导致应用程序在设备上启动后立即崩溃。 SO please if anyone can show me my error. 如果有人能告诉我我的错误,那么请。

Here is a link to the logcat output as well as the game code 这是logcat输出的链接以及游戏代码

Logcat and game code Logcat和游戏代码

Try this to set activity to fullscreen: 尝试将此活动设置为全屏:

getWindow().getDecorView().setSystemUiVisibility(
  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

You can put this code in onCreate() method 您可以将此代码放在onCreate()方法中

You can try following code. 您可以尝试以下代码。

style.xml: style.xml:

<style name="AppTheme.NoTitle" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

AndroidManifest.xml: AndroidManifest.xml中:

<activity
    android:name=".FullScreenActivity"
    android:theme="@style/AppTheme.NoTitle"
    android:screenOrientation="portrait"
    android:launchMode="singleTop">
</activity>

None of the answers above works correctly; 以上答案都没有正常工作; they have problems with the onResume() method, and end up showing the soft keys. 他们有onResume()方法的问题,并最终显示软键。

The correct way to do it is pretty straightforward. 正确的方法是非常简单的。 Override this method in the Activity that should be fullscreen: 在应该是全屏的Activity中覆盖此方法:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
}

That's if you want "Sticky Immersion". 如果你想要“Sticky Immersion”就好了。 Check out the full doc here , and decide what is better for your use case. 在这里查看完整的文档,并确定哪些更适合您的用例。

What you wanted is called Imersive mode, which works on Android 4.4 and Above 您想要的是Imersive模式,适用于Android 4.4及更高版本

getWindow().getDecorView().setSystemUiVisibility(
  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

Official Documentation can be found here 官方文档可以在这里找到

In AndroidManifest.xml file AndroidManifest.xml文件中

<activity
       android:name=".Launch"
       android:label="@string/app_name"
       android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > <!-- This line is important -->

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

and your class should extends Activity not AppCompatActivity... 并且你的类应该扩展Activity而不是AppCompatActivity ...

你可以简单地转到你的清单文件,并根据你的要求将android:theme="@android:style/Theme.NoTitleBar.Fullscreen"添加到你的Manifest文件中的<activity /><application />标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM