Coder Social home page Coder Social logo

nescreenadapterdisplaycutout's Introduction

NeScreenAdapterDisplayCutout 刘海屏适配

Android P 刘海屏适配,只有在全屏模式下才需要考虑适配,普通模式无需考虑

适配步骤及源码

1. 设置全屏
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    Window window = getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
2. 判断手机厂商
3. 判断手机是否为刘海屏
    private boolean hasDisplayCutout(Window window) {
        DisplayCutout displayCutout;
        View rootView = window.getDecorView();
        WindowInsets insets = rootView.getRootWindowInsets();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && insets != null) {
            displayCutout = insets.getDisplayCutout();
            if(displayCutout != null) {
                if(displayCutout.getBoundingRects() != null && displayCutout.getBoundingRects().size() > 0
                        && displayCutout.getSafeInsetTop() > 0) {
                    return true;
                }
            }
        }
        return false; 
    }
4. 设置是否让内容区域延伸进刘海
    WindowManager.LayoutParams params = window.getAttributes();
    /**
     * #LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT   全屏模式,内容下移,非全屏不受影响
     * #LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES   允许内容延伸进刘海
     * #LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER     不允许内容延伸至刘海
     */
    params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
    window.setAttributes(params);

    //设置成沉浸式
    int flags = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
    int visibility = window.getDecorView().getSystemUiVisibility();
    visibility |= flags; //追加沉浸式设置
    window.getDecorView().setSystemUiVisibility(visibility);
5. 设置控件是否避开刘海区域
  • 方式一
   Button button = findViewById(R.id.button);
   RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) button.getLayoutParams();
   layoutParams.topMargin = heightForDisplayCutout();
   button.setLayoutParams(layoutParams);
  • 方式二
    RelativeLayout layout = findViewById(R.id.rl_container);
    layout.setPadding(layout.getPaddingLeft(), heightForDisplayCutout(), layout.getPaddingRight(), layout.getPaddingBottom());
6. 获取刘海高度
    //通常情况下,刘海的高就是状态栏的高
    public int heightForDisplayCutout(){
        int resID = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resID > 0){
            return getResources().getDimensionPixelSize(resID);
        }
        return 96;
    }

更多代码可参考 Utils

其他手机厂商刘海屏适配官方地址

nescreenadapterdisplaycutout's People

Contributors

tianyalu avatar

Watchers

James Cloos avatar  avatar

nescreenadapterdisplaycutout's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.