如何全屏布局?

时间:2020-04-02 19:27:52

标签: android android-layout android-statusbar android-fullscreen

你好StackOverflowers。

我有这样的布局:

 <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"
            >
......
    >

它看起来像这样:

enter image description here

我只是将原色alpha设置为00,状态栏没有颜色。我喜欢的是它全屏显示在状态栏下。

我进行了另一个活动,但是它是一个ConstraintLayout,而不是上面的情况那样的DrawerLayout。我还设置了android:fitsSystemWindows =“ true”。但它不会全屏显示。

代码如下:

  <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>
.......
    <>

这是结果: enter image description here

我也可以使用DrawerLayout达到此效果,但是在该活动中不需要抽屉。 有没有更专业的方法?

2 个答案:

答案 0 :(得分:0)

尝试在onCreate活动中使用它

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

答案 1 :(得分:0)

我找到了答案。 我必须设置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)
    }

即使我活动中的图像视图也位于状态栏下方。这正是我想要的。

相关问题