Answers:
您可以通过编程方式执行此操作:
public class ActivityName extends Activity {
@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"/>
编辑:
如果您使用的是AppCompatActivity,则需要添加新主题
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
然后使用它。
<activity android:name=".ActivityName"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"/>
@android:style/Theme.Holo.Light.NoActionBar.Fullscreen
AppCompatActivity
,你需要把requestWindowFeature
之前super.onCreate
。否则,您将获得:android.util.AndroidRuntimeException: requestFeature() must be called before adding content
如果@android:style/Theme.NoTitleBar.Fullscreen
由于已经使用了自己的主题而不想使用该主题,则可以使用android:windowFullscreen
。
在AndroidManifest.xml中:
<activity
android:name=".ui.activity.MyActivity"
android:theme="@style/MyTheme">
</activity>
在styles.xml中:
<style name="MyTheme" parent="your parent theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
在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>
</activity>
或使用Java代码:
protected void onCreate(Bundle savedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
尝试使用来自的appcompat style.xml
。它为所有平台提供支持。
<!-- Application theme. -->
<style name="AppTheme.FullScreen" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar" />
使用Android Studio(当前版本为2.2.2)非常容易添加全屏活动。
请参阅步骤:
做完了!
现在,您可以轻松进行全屏活动(请参阅java类和活动布局以了解事情的工作原理)!
对于那些使用AppCompact ... style.xml的用户
<style name="Xlogo" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
然后把名字放在清单中...
首先,您必须使用“ NoActionBar”设置应用主题,如下所示
<!-- Application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar" />
然后将这些行添加到全屏活动中。
public class MainActiviy extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
它会在全屏活动中隐藏操作栏/工具栏和状态栏
AndroidManifest.xml
<activity ...
android:theme="@style/FullScreenTheme"
>
</activity>
用于隐藏ActionBar / StatusBar
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
</style>
<style name="FullScreenTheme" parent="AppTheme">
<!--this property will help hide the ActionBar-->
<item name="windowNoTitle">true</item>
<!--currently, I don't know why we need this property since use windowNoTitle only already help hide actionbar. I use it because it is used inside Theme.AppCompat.Light.NoActionBar (you can check Theme.AppCompat.Light.NoActionBar code). I think there are some missing case that I don't know-->
<item name="windowActionBar">false</item>
<!--this property is used for hiding StatusBar-->
<item name="android:windowFullscreen">true</item>
</style>
隐藏系统导航栏
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
setContentView(R.layout.activity_main)
...
}
}
用于隐藏ActionBar / StatusBar
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>
<style name="FullScreenTheme" parent="AppTheme">
<!--don't need any config for hide ActionBar because our apptheme is NoActionBar-->
<!--this property is use for hide StatusBar-->
<item name="android:windowFullscreen">true</item> //
</style>
隐藏系统导航栏
类似像Theme.AppCompat.Light.DarkActionBar
。
感谢您的答复@Cristian我遇到了错误
android.util.AndroidRuntimeException:在添加内容之前必须调用requestFeature()
我用这个解决了
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
-----
-----
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
之前super.onCreate(savedInstanceState);
吗?
显示完全沉浸式:
private void askForFullScreen()
{
getActivity().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 // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
退出完全沉浸式模式:
private void moveOutOfFullScreen() {
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
我想使用自己的主题,而不是使用@android:style / Theme.NoTitleBar.Fullscreen。但这并不像这里的某些帖子所提到的那样有效,所以我做了一些调整来弄清楚。
在AndroidManifest.xml中:
<activity
android:name=".ui.activity.MyActivity"
android:theme="@style/MyTheme">
</activity>
在styles.xml中:
<style name="MyTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
</style>
注意:在我的情况下,必须使用name="windowActionBar"
代替name="android:windowActionBar"
它,然后才能正常工作。因此,我只是使用了两者来确保以后需要移植到新的Android版本。
这是示例代码。您可以打开/关闭标志来隐藏/显示特定部分。
public static void hideSystemUI(Activity activity) {
View decorView = activity.getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
然后,您重置为默认状态:
public static void showSystemUI(Activity activity) {
View decorView = activity.getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
您可以通过以下命令调用上述函数onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.course_activity);
UiUtils.hideSystemUI(this);
}
科特琳
遵循google doc,有一种简单的方法:
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) hideSystemUI() }
private fun hideSystemUI() {
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN) }
// Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
private fun showSystemUI() {
window.decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN) }
提示:使用getWindow()。setLayout()可能会破坏您的全屏显示!请注意此方法的文档说:
设置窗口的宽度和高度布局参数...您可以将其更改为...以使窗口不全屏的绝对值。
http://developer.android.com/reference/android/view/Window.html#setLayout%28int,%20int%29
出于我的目的,我发现必须使用带有绝对参数的setLayout才能正确调整全屏窗口的大小。在大多数情况下,这可以正常工作。它由onConfigurationChanged()事件调用。但是,出现了打ic。如果用户退出应用程序,更改方向并重新进入,则将导致触发包含setLayout()的代码。在此重新输入时间窗口内,将重新显示我的状态栏(被清单隐藏),但在任何其他时间,setLayout()都不会导致此情况!解决方案是在具有硬值的那之后添加一个额外的setLayout()调用,如下所示:
public static void setSize( final int width, final int height ){
//DO SOME OTHER STUFF...
instance_.getWindow().setLayout( width, height );
// Prevent status bar re-appearance
Handler delay = new Handler();
delay.postDelayed( new Runnable(){ public void run() {
instance_.getWindow().setLayout(
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT );
}}, FILL_PARENT_ON_RESIZE_DELAY_MILLIS );
}
然后正确调整窗口大小,并且无论触发该事件的事件如何,状态栏都不会重新出现。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
adjustFullScreen(newConfig);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
adjustFullScreen(getResources().getConfiguration());
}
}
private void adjustFullScreen(Configuration config) {
final View decorView = getWindow().getDecorView();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
decorView.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);
} else {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}
内部styles.xml ...
<!-- No action bar -->
<style name="NoActonBar" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Theme customization. -->
<item name="colorPrimary">#000</item>
<item name="colorPrimaryDark">#444</item>
<item name="colorAccent">#999</item>
<item name="android:windowFullscreen">true</item>
</style>
这对我有用。希望对您有帮助。
使用Kotlin,这就是我的方法:
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_FULLSCREEN
}
}
沉浸模式
沉浸模式适用于用户将与屏幕进行大量交互的应用。例如游戏,查看画廊中的图像或阅读分页内容,例如书中的幻灯片或演示文稿中的幻灯片。为此,只需添加以下行:
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
粘性沉浸式
在常规沉浸式模式下,每当用户从边缘滑动时,系统都会负责显示系统栏-您的应用甚至不会意识到手势的发生。因此,如果用户实际上可能需要作为主要应用程序体验的一部分从屏幕边缘滑动(例如,在玩需要大量滑动的游戏或使用绘图应用程序时),则应启用“粘性”沉浸式模式。
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
有关更多信息:启用全屏模式
如果您使用键盘,有时会在键盘显示时显示StatusBar。在那种情况下,我通常将此添加到我的样式xml中
styles.xml
<style name="FullScreen" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
</style>
这行到我的清单
<activity
android:name=".ui.login.LoginActivity"
android:label="@string/title_activity_login"
android:theme="@style/FullScreen">
它为我工作。
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash_screen);
getSupportActionBar().hide();
}
经过很多时间没有成功之后,我想到了自己的解决方案,该解决方案与另一位开发人员退出了。所以如果有人需要她,我的问题是调用后系统导航栏没有隐藏。同样,在我的情况下,我需要景观,以防万一注释该行以及所有内容。首先创造风格
<style name="FullscreenTheme" parent="AppTheme">
<item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
</style>
这是我的清单文件
<activity
android:name=".Splash"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|screenSize"
android:label="@string/app_name"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|screenSize"
android:screenOrientation="landscape"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme">
</activity>
这是我的活动
public class Splash extends Activity {
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 2000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash_creen);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
这是我主要的全屏活动。onSystemUiVisibilityChange thi方法非常重要,否则调用后android主导航栏将停留并且不再消失。确实令人讨厌的问题,但是此功能解决了该问题。
公共类MainActivity扩展了AppCompatActivity {
private View mContentView;
@Override
public void onResume(){
super.onResume();
mContentView.setSystemUiVisibility(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);
setContentView(R.layout.fullscreen2);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
actionBar.hide();
}
mContentView = findViewById(R.id.fullscreen_content_text);
mContentView.setSystemUiVisibility(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);
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener()
{
@Override
public void onSystemUiVisibilityChange(int visibility)
{
System.out.println("print");
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
{
mContentView.setSystemUiVisibility(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);
}
else
{
mContentView.setSystemUiVisibility(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);
}
}
});
}
}
这是我的初始屏幕布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/splashscreen" android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:src="@drawable/splash"
android:layout_gravity="center"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, splash"/>
</LinearLayout>
This is my fullscreen layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
>
<TextView
android:id="@+id/fullscreen_content_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="@string/dummy_content2"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
</FrameLayout>
我希望这能帮到您
https://developer.android.com/training/system-ui/immersive.html
活动内容:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
decorView.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);
}
}
AndroidManifests:
<activity android:name=".LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_login"
android:theme="@style/FullscreenTheme"
></activity>
创建一个空活动,并在中添加两行onCreate
。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// full screen activity
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
}
...
}
在onCreate()中的setContentView之后使用此方法,并通过getWindow()传递Window对象。
public void makeActivityFullScreen(Window window){
View decorView = window.getDecorView();
// int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
decorView.setSystemUiVisibility(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
);
}
此代码也可用于陷波屏。要全屏检查陷波,您需要使用android P,但是如果您有陷波显示电话,请转到设置->显示设置->应用程序显示比例--->选择您的应用程序 --->这里有两个安全的选项是显示和全屏模式,请选择全屏模式并运行应用程序,即使没有android Pie,您也可以在屏幕上看到全屏模式
要使您的活动全屏显示,请执行以下操作:
// add following lines before setContentView
// to hide toolbar
if(getSupportActionBar()!=null)
getSupportActionBar().hide();
//to hide status bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
这将隐藏工具栏和状态栏。
但是在某些情况下,您可能希望使用透明背景显示状态栏,在这种情况下,请执行以下操作:
// add following lines before setContentView
// to hide toolbar
if(getSupportActionBar()!=null)
getSupportActionBar().hide();
// to make status bar transparent
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
其他一些隐藏工具栏,而不是
getSupportActionBar().hide()
:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
对于Kotlin爱好者,为什么不使用扩展功能:
对于第一种情况:
fun AppCompatActivity.makeItFullScreenStatusBarVisible(){
supportActionBar?.hide()
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}
并在此之前致电setContentView
:
makeItFullScreenStatusBarVisible()
对于第二个:
fun AppCompatActivity.makeItFullScreenStatusBarHidden(){
supportActionBar?.hide()
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
并在之前致电setContentView
:
makeItFullScreenStatusBarHidden()
通过切口或切口区域显示内容。这可以从文档中提供帮助:
LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES-内容以纵向和横向模式渲染到剪切区域中。
对我而言,关键是活动风格中的这一行:
// Important to draw through the cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
对于我来说,我想以沉浸式模式显示图像。当我单击它时,我希望显示系统UI(状态和导航栏)。
这是我的解决方案:
1-在“活动”中,一些方法用于显示/隐藏系统UI(状态/导航栏)
private fun hideSystemUI() {
sysUIHidden = true
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // Hide nav bar
or View.SYSTEM_UI_FLAG_FULLSCREEN // Hide status bar
)
}
private fun showSystemUI() {
sysUIHidden = false
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // layout Behind nav bar
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // layout Behind status bar
)
}
2-确保在您的xml布局的根视图中
android:fitsSystemWindows="false"
3-全屏活动的样式将在状态/导航栏显示时为半透明背景:
<style name="FullscreenTheme" parent="AppTheme">
<item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowBackground">@null</item>
<item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
<item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
<item name="android:statusBarColor">#50000000</item>
<item name="android:navigationBarColor">#50000000</item>
// Important to draw behind cutouts
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/sysTransparent</item>
</style>