Android设置Activity全屏和沉浸式的几种方式

全屏一,代码方式

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //set no title bar 需要在setContentView之前调用
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //如果上面的不起作用,可以换成下面的。
        if (getSupportActionBar()!=null) getSupportActionBar().hide();
        if (getActionBar()!=null) getActionBar().hide();
        //no status bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_camera_view);
        initView();
    }

全屏二,style

    <style name="AppTheme.FullScreen" parent="Theme.AppCompat.Light.NoActionBar">
        "windowNoTitle">true
        "android:windowFullscreen">true
    style>

沉浸式,TranslucentTheme

为页面指定主题样式

        <activity
            android:name="LoginActivity"
            android:screenOrientation="portrait"
            android:theme="@style/TranslucentTheme" />

Android设置Activity全屏和沉浸式的几种方式_第1张图片在这里插入图片描述

style.xml

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"/>

style.xml(v19)

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        "colorPrimary">@color/colorPrimary
        "colorPrimaryDark">@color/colorPrimaryDark
        "colorAccent">@color/colorAccent

        "android:windowTranslucentStatus">true
        "android:windowTranslucentNavigation">true
    style>

resources>

style.xml(v21)

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        "colorPrimary">@color/colorPrimary
        "colorPrimaryDark">@color/colorPrimaryDark
        "colorAccent">@color/colorAccent

        "android:windowTranslucentStatus">true
        "android:windowTranslucentNavigation">true
        
        "android:statusBarColor">@android:color/transparent
    style>
resources>

你可能感兴趣的:(Android设置Activity全屏和沉浸式的几种方式)