28

我知道可以通过活动清单中的标记使应用程序全屏显示android:theme="@android:style/Theme.NoTitleBar.Fullscreen"是否可以从应用程序内以编程方式切换到全屏模式?

4

6 回答 6

71

添加两行...

requestWindowFeature(Window.FEATURE_NO_TITLE); 
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
于 2012-05-29T17:36:34.970 回答
15

您可以创建新样式并添加

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

或者您可以以编程方式执行此操作:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
于 2013-08-18T09:50:48.073 回答
10

在 setContentView 之前的 Activity onCreate 中添加:

@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>
于 2012-05-29T17:41:25.570 回答
1

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

最有效的方法是仅在需要时设置 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);
}

}

于 2020-12-26T18:32:12.500 回答
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"/>
于 2016-12-29T12:27:46.713 回答
0

在 onCreate() 方法中写

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

于 2018-02-09T11:46:16.707 回答