简体   繁体   English

如何使布局全屏?

[英]How to make a layout fullscreen?

Hello StackOverflowers.你好 StackOverflowers。

I have a this layout:我有一个这样的布局:

 <androidx.drawerlayout.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
......
    >

and it looks like this:它看起来像这样:

在此处输入图片说明

I just set the primary color alpha to 00 and the status bar has no color.我只是将主色 alpha 设置为 00,状态栏没有颜色。 What I like is that it goes full screen and under the status bar.我喜欢的是它全屏显示并位于状态栏下方。

I made another activity, but it is a ConstraintLayout not as DrawerLayout as in the above case.我做了另一个活动,但它是一个 ConstraintLayout,不像上面的例子那样 DrawerLayout。 I also set the android:fitsSystemWindows="true".我还设置了 android:fitsSystemWindows="true"。 but it just will not go full screen.但它不会全屏显示。

Here is the code:这是代码:

  <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/splashScreenLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".SplashScreen">

     <ImageView>
.......
    <>

this is the result:这是结果: 在此处输入图片说明

I could also use DrawerLayout to achieve this effect, but I do not need a drawer in that activity.我也可以使用 DrawerLayout 来实现这种效果,但我不需要在该活动中使用抽屉。 Is there a more professional approach?有没有更专业的方法?

try using this in onCreate Activity尝试在 onCreate Activity 中使用它

if (Build.VERSION.SDK_INT < 16) {
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
setContentView(R.layout.activity_main)

I found the answer.我找到了答案。 I had to set android:fitsSystemWindows="false" and I added this function to may activity and called it in on create:我必须设置 android:fitsSystemWindows="false" 并将此函数添加到 may 活动并在创建时调用它:

  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)
    }

Even the image view in my activity goes under the status bar.甚至我活动中的图像视图也位于状态栏下方。 This is exactly what I wanted.这正是我想要的。

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

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