以编程方式在 Android 中使应用程序全屏

问题描述 投票:0回答:7

我知道可以通过活动清单中的标签将应用程序设置为全屏

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
是否可以以编程方式从应用程序内部切换到全屏模式?

android android-layout android-activity
7个回答
72
投票

添加两行...

requestWindowFeature(Window.FEATURE_NO_TITLE); 
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);

15
投票

您可以创建新样式并添加

<item name="android:windowFullscreen">true</item>

或者您可以通过编程方式完成:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

10
投票

在 Activity onCreate 中 setContentView 之前添加此内容:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // remove title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);
}

在 AndroidManifest.xml 文件中:

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
</activity>

3
投票

如果您需要全屏沉浸式模式(用于游戏、画廊等),这是我使用和推荐的解决方案。

最有效的方法是仅在需要时设置 SystemUIVisibility:

  1. 在你的onCreate()中
  2. 全屏模式丢失时使用 OnSystemUIChangeListener

公共类 BaseActivity 扩展 Activity {

@SuppressLint("InlinedApi")
private static final int UI_OPTIONS = View.SYSTEM_UI_FLAG_LOW_PROFILE
        | View.SYSTEM_UI_FLAG_FULLSCREEN
        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;        

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Step 1
    hideSystemUI();

    // Step 2
    getWindow().getDecorView()
               .setOnSystemUiVisibilityChangeListener(new View
               .OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                hideSystemUI();
            }
        }
    });
}

private void hideSystemUI() {
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) actionBar.hide();
    getWindow().getDecorView().setSystemUiVisibility(UI_OPTIONS);
}

}


0
投票

试试这个,

// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
                     setContentView(R.layout.main);

<activity android:name=".ActivityName"
 android:label="@string/app_name"
 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>

0
投票

在onCreate()方法中写入

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


0
投票

对于 targetSdk 35 您应该在 Activity 类中调用:

override fun onCreate(savedInstanceState: Bundle?) {
    enableEdgeToEdge()
    super.onCreate(savedInstanceState)

    //If you whant to control insets by yourself, you can collect it by
    ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { _, windowInsets ->
        //consume insets here
        WindowInsetsCompat.CONSUMED
    }
}

然后你可以在任何地方打电话:

WindowInsetsControllerCompat(window, window.decorView)
    .hide(WindowInsetsCompat.Type.statusBars())

您还可以使用以下方法扩展您的 theme.xml:

<item name="android:windowSoftInputMode">adjustResize</item>
<item name="android:windowLayoutInDisplayCutoutMode" tools:targetApi="27">always</item>
<item name="android:enforceNavigationBarContrast" tools:targetApi="29">false</item>
© www.soinside.com 2019 - 2024. All rights reserved.