我知道可以通过活动清单中的标签将应用程序设置为全屏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
是否可以以编程方式从应用程序内部切换到全屏模式?
添加两行...
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
您可以创建新样式并添加
<item name="android:windowFullscreen">true</item>
或者您可以通过编程方式完成:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
在 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>
如果您需要全屏沉浸式模式(用于游戏、画廊等),这是我使用和推荐的解决方案。
最有效的方法是仅在需要时设置 SystemUIVisibility:
公共类 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);
}
}
试试这个,
// 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"/>
在onCreate()方法中写入
requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
对于 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>