Android 全屏适配刘海机型



1、刘海机型说明



众所周知, 市面上的android 机型,屏幕尺寸五花八门,如下图所示:



在这里插入图片描述



像以上两种屏幕,我们都统称为【刘海屏】。对于右侧较小的刘海,业界一般称为【水滴屏】或【美人尖】。



2、刘海机型适配方案



从Android P(9.0)开始,官方提供了适配异形屏的方式。

通过全新的 DisplayCutout 类,可以确定非功能区域的位置和形状,这些区域不应显示内容。 要确定这些凹口屏幕区域是否存在及其位置,请使用 getDisplayCutout() 函数。

Android 允许控制是否在刘海区域内显示内容。

窗口布局属性 layoutInDisplayCutoutMode 控制内容如何呈现在刘海区域中。

可以将 layoutInDisplayCutoutMode 设为以下某个值:

  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT 默认,在竖屏模式下,内容会呈现到刘海区域中;但在横屏模式下,内容会显示黑边。
  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES- 在竖屏模式和横屏模式下,内容都会呈现到刘海区域中。
  • LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER- 内容从不呈现到刘海区域中。


3、刘海机型适配参考



(1)、代码适配实现方式

 // 延伸显示区域到刘海
 WindowManager.LayoutParams lp = window.getAttributes();
 lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
 window.setAttributes(lp);
 // 设置页面全屏显示
 final View decorView = window.getDecorView();
 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
 


(2)、style实现方式

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
     <style name="AppTheme" parent="xxx">
         <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
     </style>
 </resources>


(3)、Dialog 下 完整全屏适配【刘海屏】示例:

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

@SuppressLint("InflateParams")
public class TestatDialog extends Dialog{

    public TestatDialog(Activity context, int dialogFullscreen) {
        super(context, dialogFullscreen);
        this.activity = context;
    }

    protected void onCreate(Bundle paramBundle)
    {
        super.onCreate(paramBundle);

        //全屏适配刘海机型
        Window window = getWindow();
        if (window !=null) {
            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            window.getDecorView().setPadding(0, 0, 0, 0);
            window.getDecorView().setBackgroundColor(Color.WHITE);
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {

                //延伸显示区域到刘海
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                window.setAttributes(lp);

                //设置页面全屏显示
                final View decorView = window.getDecorView();
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
            window.setAttributes(layoutParams);
        }

        setContentView(LayoutInflater.from(this.activity).inflate(R.layout.lanuch_layout_view, null));
    }

    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        if ((paramInt == KeyEvent.KEYCODE_BACK) && (paramKeyEvent.getRepeatCount() == 0)) {
            return true;
        }
        return super.onKeyDown(paramInt, paramKeyEvent);
    }

    @Override
    public void dismiss() {
    
    }
}
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w风雨无阻w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值