Android 全屏沉浸模式(支持API 19及以上系统)

本文介绍了如何在Android API 19及以上系统实现全屏沉浸模式。通过设置Theme,调整fitsSystemWindows属性,并使用Design Support包创建XML文件,来达到全屏效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Adnroid 4.4(API level 19)中引入为setSystemUiVisibility()引入了一个新标签SYSTEM_UI_FLAG_IMMERSIVE,它可以让应用进入真正的全屏模式。
而现在我实现的方式是通过Theme以及系统提供的控件来实现全屏沉浸模式。
这里写图片描述
这里写图片描述
1.设置主题(Theme)
a./res/values

<style name= "AppTheme.NoActionBar" >
  <item name= "windowActionBar" > false</item>
  <item name= "windowNoTitle" > true</item>
<!--是否显示系统启动应用时默认加载的默认页-->
  <item name= "android:windowDisablePreview" > false</item>
  <!-- Customize your theme here. -->
      <!--主调色彩:主调色彩是用于应用品牌推广的色彩。作为action bar的背景色最近的任务title和其它边缘效果。-->
  <item name= "colorPrimary" > @color/colorPrimary </item>
  <!-- 主调的暗色: Darker作为主调色彩的加深,应用于状态栏 status bar.-->
  <item name= "colorPrimaryDark" > @color/colorPrimaryDark </item>
  <!-- 强调色彩:鲜明的扩展了主调色彩。应用于框架的控制。比如EditText,Switch-->
  <item name= "colorAccent" > @color/colorAccent </item>
  <!--窗口背景颜色-->
  <item name= "android:windowBackground" > @color/window_bg</item>
  <item name= "android:windowNoTitle" > true</item>
</style>

b. /res/values-v19

<style name="AppTheme.NoActionBar">
  <item name= "windowActionBar" >false</item>
  <item name= "windowNoTitle" >true</item>
  <!--状态栏是否透明,API 19才出现的属性-->
  <item name= "android:windowTranslucentStatus" >true</item>
</style>

c. /res/values-v21

<style name= "AppTheme.NoActionBar" >
  <item name= "windowActionBar" > false</item>
  <item name= "windowNoTitle" > true</item>
  <!--状态栏是否透明,API 21才出现的属性-->
  <item name= "android:windowDrawsSystemBarBackgrounds" > true</item>
  <!--设置状态栏颜色,API 21才出现的属性-->
  <item name= "android:statusBarColor" > @android:color/transparent </item>
</style>

2.设置fitsSystemWindows属性值,在5.0之后需要设置为true才能正常显示全屏沉浸式
a./res/values-v19

<bool name= "fitsSystemWindows" >false </bool>

b./res/values-v21

 <bool name= "fitsSystemWindows" >true </bool>

c./res/values

<bool name= "fitsSystemWindows" >false </bool>

3.导入design Support包并创建UI XML文件
a.导入Design Support包

compile 'com.android.support:design:23.1.1'

b.UI创建XML文件

<?xml version="1.0"encoding="utf-8"?>
<android.support.design.internal.ScrimInsetsFrameLayout
  xmlns: android="http://schemas.android.com/apk/res/android"
  android :layout_width="match_parent"
  android :layout_height="match_parent">

  <android.support.design.widget.CoordinatorLayout
     android:id= "@+id/coordinator_layout"
     android:layout_width= "match_parent"
     android:layout_height= "0dp"
     android:layout_weight= "1.0"
     android :fitsSystemWindows= "@bool/fitsSystemWindows" >

  </android.support.design.widget.CoordinatorLayout>

</android.support.design.internal.ScrimInsetsFrameLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值