Full screen layout for Android

Time:2021-9-15

This example shares the specific code for Android to realize full screen layout for your reference. The specific contents are as follows

preface

Similar to the launcher, the layout you want to occupy is spread all over the full screen to adjust the color style of the status bar and virtual keys.

No more nonsense. The last case:

1、 Effect preview

2、 Case realization

1. New Android project
2. Styles style added

Add the following styles to the styles.xml of the values Directory:


<style name="FullTheme" parent="@style/BaseFullTheme">
</style>
<style name="BaseFullTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
  <item name="android:windowBackground">@android:color/transparent</item>
  <item name="android:colorBackgroundCacheHint">@null</item>
  <item name="android:windowShowWallpaper">true</item>
  <item name="android:windowNoTitle">true</item>
</style>

Add the following styles to the styles.xml of the values-v19 Directory:


<style name="FullTheme" parent="@style/BaseFullTheme">
  <item name="android:windowTranslucentStatus">true</item>
  <item name="android:windowTranslucentNavigation">true</item>
</style>

Add the following styles to the styles.xml of the values-v21 Directory:


<style name="FullTheme" parent="@style/BaseFullTheme">
  <item name="android:windowTranslucentStatus">false</item>
  <item name="android:windowTranslucentNavigation">false</item>
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  <item name="android:statusBarColor">#00000000</item>
  <item name="android:navigationBarColor">#00000000</item>
</style>

Add the following styles to the styles.xml of the values-v29 Directory:


<style name="FullTheme" parent="@style/BaseFullTheme">
  <item name="android:colorBackgroundCacheHint">@null</item>
  <item name="android:colorEdgeEffect">#FF757575</item>
  <item name="android:windowActionBar">false</item>
  <item name="android:windowBackground">@android:color/transparent</item>
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowShowWallpaper">true</item>
  <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
  <item name="android:enforceStatusBarContrast">false</item>
  <item name="android:enforceNavigationBarContrast">false</item>


  <item name="android:windowTranslucentStatus">false</item>
  <item name="android:windowTranslucentNavigation">false</item>
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  <item name="android:statusBarColor">#00000000</item>
  <item name="android:navigationBarColor">#00000000</item>
</style>

3. Layout

Create activity in layout directory_ main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/holo_blue_bright"<!--  Color of test settings -- >
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        Android: text = "test"
        >
    </Button>
</LinearLayout>

4. Use

New mainactivity.java

package com.demo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideStatusBarNavigationBar();
        setContentView(R.layout.activity_main);
    }

   //Key methods
    private void hideStatusBarNavigationBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        }
    }
}

Androidmanifest.xml declaration


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/FullTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

finish

3、 Pit filling: pit of fitssystemwindows

In activity_ Android: fitssystemwindows = “true” is added to the root layout in main.xml. If this attribute is not added, the layout of the sub view will start from the top. If you are interested, you can try to modify it.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

The coding platform Vue project is continuously integrated into Tencent cloud Linux server

The coding platform Vue project is continuously integrated into Tencent cloud Linux server key word:coding Vue Docker 1、 Foreword Why do I need continuous integration? Let me first say that I do not use the usage scenario before continuous integration: After writing the code, use NPM build to edit it into a dist folder Open […]