启动页设置全屏并适配刘海屏

需求:

启动页是一张图片,希望设置为全屏显示,包括状态栏和导航栏(虚拟按键),同时也适应刘海屏

正确的设置方式:

  1. values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/color_Primary</item>
    <item name="colorPrimaryDark">@color/color_PrimaryDark</item>
    <item name="colorAccent">@color/color_Accent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<!-- 启动页设置全屏,并设置背景为启动页图片,防止启动白屏 -->
<style name="AppTheme.NoActionBar.NoActionBarLaunch" >
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@drawable/bg_launch</item>
</style>

2.values-v19/styles.xml

<!-- 启动页设置全屏 -->
<style name="AppTheme.NoActionBar.NoActionBarLaunch"  parent="AppTheme.NoActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@drawable/bg_launch</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

3.values-v21/styles.xml

<!-- 启动页设置全屏 -->
<style name="AppTheme.NoActionBar.NoActionBarLaunch"  parent="AppTheme.NoActionBar">
    <item name="android:windowBackground">@drawable/bg_launch</item>
    <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:navigationBarColor">@android:color/transparent</item>
</style>

4.给LaunchActivity设置Theme,并由于一些刘海屏手机(如华为),如果没有适配的话,系统会将刘海屏区域(状态栏)显示为黑色,虽然我们的styles.xml中设置了全屏也无效,所以:

AndroidManifest.xml中:

<activity
    android:name=".activity.LaunchActivity"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoActionBar.NoActionBarLaunch">
    <!-- 全屏theme -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <!-- 华为刘海屏区域使用 -->
    <meta-data android:name="android.notch_support" android:value="true"/>
</activity>

LaunchActivity 代码中:

/**
 * Android P Google API 使用刘海屏区域
 */
private void setGoogleNotch() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        getWindow().setAttributes(lp);
    }
}

/**
 * 设置使用小米手机刘海屏区域
 */
private void setMIUINotch() {
    int flag = 0x00000100 | 0x00000200 | 0x00000400;
    try {
        Method method = Window.class.getMethod("addExtraFlags", int.class);
        method.invoke(getWindow(), flag);
    } catch (Exception e) {
        Log.i("launch", "addExtraFlags not found.");
    }
}

注意:

  1. values-v21中的styles.xml中,windowFullscreen windowTranslucentNavigation windowTranslucentStatus 这些属性都不要设置,设置了反而会适得其反。比如设置了windowTranslucentNavigation windowTranslucentStatus,反而会有一层浅灰色的背景,直接设置navigationBarColor颜色为透明即可;设置windowFullscreen在vivo刘海屏手机、Nexus 5X刘海屏手机上,状态栏为黑色
  1. 上述为了解决启动白屏给window设置背景为启动页图片,实际使用中有些问题:同一张图片,给window设置为背景,然后给启动页也设为背景,但是同样背景却类似于伸缩模式不同,从window切换到启动页时,图片会缩小一圈,出现跳动现象,所以,如果启动页是纯色背景,可以创建个drawable-shape,给window设置这个纯色背景,如果是其他,直接将图片设置为window的背景,而启动页不设置视图,就是去除setContentView(),只是用来设置style,过渡,跳转,

以上,完成启动页全屏的功能,并适配了刘海屏

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值